1 // Copyright (C) 2006 Erik Dahlberg
3 // This file is part of LSystem3d.
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.
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.
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
30 * @param x the x-coordinate
31 * @param y the y-coordinate
32 * @param z the z-coordinate
34 Vector::Vector(double x
, double y
, double z
)
51 * Rotate around another vector
52 * @param angle rotation angle
53 * @param vector arbitrary vector to rotate around
55 void Vector::rotate(double angle
, Vector vector
)
57 double c
= cos(angle
);
58 double s
= sin(angle
);
61 _x
* (vector
._x
* vector
._x
* (1.0 - c
) + c
) +
62 _y
* (vector
._x
* vector
._y
* (1.0 - c
) + vector
._z
* s
) +
63 _z
* (vector
._x
* vector
._z
* (1.0 - c
) - vector
._y
* s
);
66 _x
* (vector
._y
* vector
._x
* (1.0 - c
) - vector
._z
* s
) +
67 _y
* (vector
._y
* vector
._y
* (1.0 - c
) + c
) +
68 _z
* (vector
._y
* vector
._z
* (1.0 - c
) + vector
._x
* s
);
71 _x
* (vector
._z
* vector
._x
* (1.0 - c
) + vector
._y
* s
) +
72 _y
* (vector
._z
* vector
._y
* (1.0 - c
) - vector
._x
* s
) +
73 _z
* (vector
._z
* vector
._z
* (1.0 - c
) + c
);
79 // TODO: call it from outside?
88 void Vector::normalize()
90 double length
= sqrt(_x
* _x
+ _y
* _y
+ _z
* _z
);
103 * Get scalar product of the vectors
104 * @param vector arbitrary vector
105 * @return the scalar product
107 double Vector::getScalarProduct(Vector vector
)
109 double scalarProduct
= getX() * vector
.getX() +
110 getY() * vector
.getY() +
111 getZ() * vector
.getZ();
113 return scalarProduct
;
119 * Get cross product of the vectors
120 * @param vector arbitrary vector
121 * @return the cross product
123 Vector
Vector::getCrossProduct(Vector vector
)
125 Vector
crossProduct(getY() * vector
.getZ() - getZ() * vector
.getY(),
126 getZ() * vector
.getX() - getX() * vector
.getZ(),
127 getX() * vector
.getY() - getY() * vector
.getX());
135 * Get angle between the vectors
136 * @param vector the second (normalized) vector
137 * @return the angle, in degrees
139 double Vector::getAngle(Vector vector
)
141 double scalarProduct
= getScalarProduct(vector
);
142 double angle
= (acos(scalarProduct
) / (M_PIl
* 2.0)) * 360.0;