source: git/src/vector3.cc @ 8a7804fb

Last change on this file since 8a7804fb was 0b99107, checked in by Olly Betts <olly@…>, 3 weeks ago

Eliminate old FSF addresses

Update GPL/LGPL licence files and boilerplate to direct people who
didn't receive the licence text to the FSF website, as the current
versions of the FSF licence texts now do, rather than giving a postal
address.

  • Property mode set to 100644
File size: 2.2 KB
Line 
1//
2//  vector3.cc
3//
4//  C++ class for 3-element vectors
5//
6//  Copyright (C) 2000-2001, Mark R. Shinwell.
7//  Copyright (C) 2002-2003 Olly Betts
8//
9//  This program is free software; you can redistribute it and/or modify
10//  it under the terms of the GNU General Public License as published by
11//  the Free Software Foundation; either version 2 of the License, or
12//  (at your option) any later version.
13//
14//  This program is distributed in the hope that it will be useful,
15//  but WITHOUT ANY WARRANTY; without even the implied warranty of
16//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17//  GNU General Public License for more details.
18//
19//  You should have received a copy of the GNU General Public License
20//  along with this program; if not, see
21//  <https://www.gnu.org/licenses/>.
22//
23
24#include <config.h>
25
26#include "vector3.h"
27
28#include <math.h>
29
30void Vector3::normalise()
31{
32    double mag = magnitude();
33    if (mag != 0.0) {
34        x /= mag;
35        y /= mag;
36        z /= mag;
37    }
38}
39
40double dot(const Vector3& left, const Vector3& right)
41{
42    return left.x*right.x + left.y*right.y + left.z*right.z;
43}
44
45Vector3& Vector3::operator*=(const double f)
46{
47    x *= f;
48    y *= f;
49    z *= f;
50
51    return *this;
52}
53
54Vector3& Vector3::operator/=(const double f)
55{
56    x /= f;
57    y /= f;
58    z /= f;
59
60    return *this;
61}
62
63Vector3& Vector3::operator+=(const Vector3 &v)
64{
65    x += v.x;
66    y += v.y;
67    z += v.z;
68
69    return *this;
70}
71
72Vector3& Vector3::operator-=(const Vector3 &v)
73{
74    x -= v.x;
75    y -= v.y;
76    z -= v.z;
77
78    return *this;
79}
80
81Vector3 operator*(const double f, const Vector3& v)
82{
83    Vector3 o;
84    o.x = v.x * f;
85    o.y = v.y * f;
86    o.z = v.z * f;
87
88    return o;
89}
90
91Vector3 operator*(const Vector3& v1, const Vector3& v2)
92{
93    // Cross product.
94
95    Vector3 o;
96    o.x = v1.y*v2.z - v1.z*v2.y;
97    o.y = v1.z*v2.x - v1.x*v2.z;
98    o.z = v1.x*v2.y - v1.y*v2.x;
99
100    return o;
101}
102
103Vector3 operator+(const Vector3& v1, const Vector3& v2)
104{
105    Vector3 o;
106    o.x = v1.x + v2.x;
107    o.y = v1.y + v2.y;
108    o.z = v1.z + v2.z;
109
110    return o;
111}
112
113Vector3 operator-(const Vector3& v1, const Vector3& v2)
114{
115    Vector3 o;
116    o.x = v1.x - v2.x;
117    o.y = v1.y - v2.y;
118    o.z = v1.z - v2.z;
119
120    return o;
121}
Note: See TracBrowser for help on using the repository browser.