source: git/src/vector3.cc @ 89a6a1e

RELEASE/1.1RELEASE/1.2debug-cidebug-ci-sanitisersfaster-cavernloglog-selectstereostereo-2025walls-datawalls-data-hanging-as-warningwarn-only-for-hanging-survey svn-merged-from-1.0
Last change on this file since 89a6a1e was 27b8b59, checked in by Olly Betts <olly@…>, 23 years ago

Sync with 1.0 branch.

git-svn-id: file:///home/survex-svn/survex/branches/survex-1_1@2208 4b37db11-9a0c-4f06-9ece-9ab7cdaee568

  • Property mode set to 100644
File size: 2.4 KB
RevLine 
[5809313]1//
[27b8b59]2//  vector3.cc
[5809313]3//
4//  C++ class for 3-element vectors
5//
6//  Copyright (C) 2000-2001, Mark R. Shinwell.
[97d93e1]7//  Copyright (C) 2002 Olly Betts
[5809313]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, write to the Free Software
21//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22//
23
24#include "vector3.h"
25
26#include <math.h>
27
28Vector3::Vector3() : x(0.0), y(0.0), z(0.0)
29{
30}
31
32Vector3::Vector3(double a, double b, double c)
33{
34    x = a;
35    y = b;
36    z = c;
37}
38
39Vector3::~Vector3()
40{
41}
42
43void Vector3::normalise()
44{
45    double mag = magnitude();
[f8e4c56]46    if (mag != 0.0) {
47        x /= mag;
48        y /= mag;
49        z /= mag;
50    }
[5809313]51}
52
53double dot(const Vector3& left, const Vector3& right)
54{
55    return left.x*right.x + left.y*right.y + left.z*right.z;
56}
57
58Vector3& Vector3::operator=(const Vector3& r)
59{
60    x = r.x;
61    y = r.y;
62    z = r.z;
63
64    return *this;
65}
66
67void Vector3::set(double nx, double ny, double nz)
68{
69    x = nx;
70    y = ny;
71    z = nz;
72}
73
74Vector3& Vector3::operator*=(const double f)
75{
76    x *= f;
77    y *= f;
78    z *= f;
79
80    return *this;
81}
82
83Vector3& Vector3::operator/=(const double f)
84{
85    x /= f;
86    y /= f;
87    z /= f;
88
89    return *this;
90}
91
92Vector3 operator*(const double f, const Vector3& v)
93{
94    Vector3 o;
95    o.x = v.x * f;
96    o.y = v.y * f;
97    o.z = v.z * f;
98
99    return o;
100}
101
102Vector3 operator*(const Vector3& v1, const Vector3& v2)
103{
104    // Cross product.
105
106    Vector3 o;
107    o.x = v1.y*v2.z - v1.z*v2.y;
108    o.y = v1.z*v2.x - v1.x*v2.z;
109    o.z = v1.x*v2.y - v1.y*v2.x;
110
111    return o;
112}
113
114Vector3 operator+(const Vector3& v1, const Vector3& v2)
115{
116    Vector3 o;
117    o.x = v1.x + v2.x;
118    o.y = v1.y + v2.y;
119    o.z = v1.z + v2.z;
120
121    return o;
122}
[f8e4c56]123
124Vector3 operator-(const Vector3& v1, const Vector3& v2)
125{
126    Vector3 o;
127    o.x = v1.x - v2.x;
128    o.y = v1.y - v2.y;
129    o.z = v1.z - v2.z;
130
131    return o;
132}
Note: See TracBrowser for help on using the repository browser.