]>
Commit | Line | Data |
---|---|---|
15c82487 | 1 | // Copyright (C) 2006 Erik Dahlberg |
2 | // | |
1a372e99 | 3 | // This file is part of LSystem3D. |
15c82487 | 4 | // |
5 | // LSystem3D is free software; you can redistribute it and/or | |
6 | // modify it under the terms of the GNU General Public License | |
7 | // as published by the Free Software Foundation; either version 2 | |
8 | // of the License, or (at your option) any later version. | |
9 | // | |
10 | // LSystem3D is distributed in the hope that it will be useful, | |
11 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | // GNU General Public License for more details. | |
14 | // | |
15 | // You should have received a copy of the GNU General Public License | |
16 | // along with LSystem3D; if not, write to the Free Software | |
17 | // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
18 | ||
19 | ||
20 | ||
21 | ||
22 | #include <cmath> | |
23 | ||
24 | #include "vector.h" | |
25 | ||
26 | ||
27 | ||
28 | /** | |
29 | * Constructor | |
30 | * @param x the x-coordinate | |
31 | * @param y the y-coordinate | |
32 | * @param z the z-coordinate | |
33 | */ | |
34 | Vector::Vector(double x, double y, double z) | |
35 | { | |
36 | setXYZ(x, y, z); | |
37 | } | |
38 | ||
39 | ||
40 | ||
41 | /** | |
42 | * Destructor | |
43 | */ | |
44 | Vector::~Vector() | |
45 | { | |
46 | } | |
47 | ||
48 | ||
49 | ||
68a7b857 | 50 | /** |
51 | * Normalize vector | |
52 | */ | |
53 | void Vector::normalize() | |
54 | { | |
55 | double length = sqrt(_x * _x + _y * _y + _z * _z); | |
56 | ||
57 | if (length != 0) | |
58 | { | |
59 | _x /= length; | |
60 | _y /= length; | |
61 | _z /= length; | |
62 | } | |
63 | } | |
64 | ||
65 | ||
66 | ||
15c82487 | 67 | /** |
68 | * Rotate around another vector | |
69 | * @param angle rotation angle | |
70 | * @param vector arbitrary vector to rotate around | |
71 | */ | |
72 | void Vector::rotate(double angle, Vector vector) | |
73 | { | |
74 | double c = cos(angle); | |
75 | double s = sin(angle); | |
76 | ||
77 | double x = | |
78 | _x * (vector._x * vector._x * (1.0 - c) + c) + | |
79 | _y * (vector._x * vector._y * (1.0 - c) + vector._z * s) + | |
80 | _z * (vector._x * vector._z * (1.0 - c) - vector._y * s); | |
81 | ||
82 | double y = | |
83 | _x * (vector._y * vector._x * (1.0 - c) - vector._z * s) + | |
84 | _y * (vector._y * vector._y * (1.0 - c) + c) + | |
85 | _z * (vector._y * vector._z * (1.0 - c) + vector._x * s); | |
86 | ||
87 | double z = | |
88 | _x * (vector._z * vector._x * (1.0 - c) + vector._y * s) + | |
89 | _y * (vector._z * vector._y * (1.0 - c) - vector._x * s) + | |
90 | _z * (vector._z * vector._z * (1.0 - c) + c); | |
91 | ||
92 | _x = x; | |
93 | _y = y; | |
94 | _z = z; | |
95 | ||
96 | // TODO: call it from outside? | |
97 | normalize(); | |
98 | } | |
99 | ||
100 | ||
101 | ||
15c82487 | 102 | /** |
526db675 | 103 | * Get scalar product of two vectors |
15c82487 | 104 | * @param vector arbitrary vector |
105 | * @return the scalar product | |
106 | */ | |
107 | double Vector::getScalarProduct(Vector vector) | |
108 | { | |
109 | double scalarProduct = getX() * vector.getX() + | |
110 | getY() * vector.getY() + | |
111 | getZ() * vector.getZ(); | |
112 | ||
113 | return scalarProduct; | |
114 | } | |
115 | ||
116 | ||
117 | ||
118 | /** | |
526db675 | 119 | * Get cross product of two vectors |
15c82487 | 120 | * @param vector arbitrary vector |
121 | * @return the cross product | |
122 | */ | |
123 | Vector Vector::getCrossProduct(Vector vector) | |
124 | { | |
125 | Vector crossProduct(getY() * vector.getZ() - getZ() * vector.getY(), | |
126 | getZ() * vector.getX() - getX() * vector.getZ(), | |
127 | getX() * vector.getY() - getY() * vector.getX()); | |
128 | ||
129 | return crossProduct; | |
130 | } | |
131 | ||
132 | ||
133 | ||
134 | /** | |
526db675 | 135 | * Get angle between two vectors |
15c82487 | 136 | * @param vector the second (normalized) vector |
137 | * @return the angle, in degrees | |
138 | */ | |
139 | double Vector::getAngle(Vector vector) | |
140 | { | |
141 | double scalarProduct = getScalarProduct(vector); | |
142 | double angle = (acos(scalarProduct) / (M_PIl * 2.0)) * 360.0; | |
143 | ||
144 | return angle; | |
145 | } |