54 return (b.
y-a.
y) * (px-a.
x) / (b.
x-a.
x) + a.
y;
62 if ( p.
x < a.
x && p.
x < b.
x && p.
x < c.
x )
return false;
63 if ( p.
x > a.
x && p.
x > b.
x && p.
x > c.
x )
return false;
64 if ( p.
y < a.
y && p.
y < b.
y && p.
y < c.
y )
return false;
65 if ( p.
y > a.
y && p.
y > b.
y && p.
y > c.
y )
return false;
67 if ( a.
x > b.
x )
Swap(a,b);
68 if ( a.
x > c.
x )
Swap(a,c);
69 if ( c.
x < a.
x )
Swap(c,a);
70 if ( c.
x < b.
x )
Swap(c,b);
74 if ( (n>p.
y||p.
y>m) && (n<p.
y||p.
y<m) )
return false;
78 if ( (n>p.
y||p.
y>m) && (n<p.
y||p.
y<m) )
return false;
96 b.
x = a.
x*cosf(angle) - a.
y*sinf(angle);
97 b.
y = a.
x*sinf(angle) + a.
y*cosf(angle);
112 float x = p.
x*cosf(angle) - p.
y*sinf(angle);
113 float y = p.
x*sinf(angle) + p.
y*cosf(angle);
125 float x = dist*cosf(angle);
126 float y = dist*sinf(angle);
137 inline void RotatePoint(
float cx,
float cy,
float angle,
float &px,
float &py)
144 ax = px*cosf(angle) - py*sinf(angle);
145 ay = px*sinf(angle) + py*cosf(angle);
166 b.
x = p.
x*cosf(angleH) - p.
z*sinf(angleH);
167 b.
y = p.
z*sinf(angleV) + p.
y*cosf(angleV);
168 b.
z = p.
x*sinf(angleH) + p.
z*cosf(angleH);
188 a.
x = p.
x*cosf(angleH) - p.
z*sinf(angleH);
190 a.
z = p.
x*sinf(angleH) + p.
z*cosf(angleH);
194 b.
y = a.
z*sinf(angleV) + a.
y*cosf(angleV);
195 b.
z = a.
z*cosf(angleV) - a.
y*sinf(angleV);
203 if (x == 0.0f && y == 0.0f)
return 0.0f;
209 if (x > y)
return atanf(y/x);
210 else return PI*0.5f - atanf(x/y);
214 if (x > -y)
return PI*2.0f + atanf(y/x);
215 else return PI*1.5f - atanf(x/y);
222 if (-x > y)
return PI*1.0f + atanf(y/x);
223 else return PI*0.5f - atanf(x/y);
227 if (-x > -y)
return PI*1.0f + atanf(y/x);
228 else return PI*1.5f - atanf(x/y);
247 float a1 = asinf((p1.
y - center.
y) /
Distance(p1, center));
248 float a2 = asinf((p2.
y - center.
y) /
Distance(p2, center));
250 if (p1.
x < center.
x) a1 =
PI - a1;
251 if (p2.
x < center.
x) a2 =
PI - a2;
274 float length = view.
Length();
275 assert(!
IsZero(length) );
337 float nearPlane = 1.0f,
float farPlane = 1000.0f)
339 assert(fabs(farPlane - nearPlane) >= 0.01f);
340 assert(fabs(sin(fov / 2)) >= 0.01f);
342 float f = cosf(fov / 2.0f) / sinf(fov / 2.0f);
346 mat.
m[0 ] = f / aspect;
348 mat.
m[10] = (nearPlane + farPlane) / (nearPlane - farPlane);
350 mat.
m[14] = (2.0f * farPlane * nearPlane) / (nearPlane - farPlane);
361 float zNear = -1.0f,
float zFar = 1.0f)
365 mat.
m[0 ] = 2.0f / (right - left);
366 mat.
m[5 ] = 2.0f / (top - bottom);
367 mat.
m[10] = -2.0f / (zFar - zNear);
369 mat.
m[12] = - (right + left) / (right - left);
370 mat.
m[13] = - (top + bottom) / (top - bottom);
371 mat.
m[14] = - (zFar + zNear) / (zFar - zNear);
408 mat.
m[5 ] = cosf(angle);
409 mat.
m[6 ] = sinf(angle);
410 mat.
m[9 ] = -sinf(angle);
411 mat.
m[10] = cosf(angle);
422 mat.
m[0 ] = cosf(angle);
423 mat.
m[2 ] = -sinf(angle);
424 mat.
m[8 ] = sinf(angle);
425 mat.
m[10] = cosf(angle);
436 mat.
m[0 ] = cosf(angle);
437 mat.
m[1 ] = sinf(angle);
438 mat.
m[4 ] = -sinf(angle);
439 mat.
m[5 ] = cosf(angle);
450 float cos = cosf(angle);
451 float sin = sinf(angle);
456 mat.
m[0 ] = (v.
x * v.
x) * (1.0f - cos) + cos;
457 mat.
m[1 ] = (v.
x * v.
y) * (1.0f - cos) - (v.
z * sin);
458 mat.
m[2 ] = (v.
x * v.
z) * (1.0f - cos) + (v.
y * sin);
460 mat.
m[4 ] = (v.
y * v.
x) * (1.0f - cos) + (v.
z * sin);
461 mat.
m[5 ] = (v.
y * v.
y) * (1.0f - cos) + cos ;
462 mat.
m[6 ] = (v.
y * v.
z) * (1.0f - cos) - (v.
x * sin);
464 mat.
m[8 ] = (v.
z * v.
x) * (1.0f - cos) - (v.
y * sin);
465 mat.
m[9 ] = (v.
z * v.
y) * (1.0f - cos) + (v.
x * sin);
466 mat.
m[10] = (v.
z * v.
z) * (1.0f - cos) + cos;
498 return sqrtf( (a.
x-b.
x)*(a.
x-b.
x) +
499 (a.
z-b.
z)*(a.
z-b.
z) );
525 return p1 + direction * dist;
537 float d = -(n.
x*a.
x + n.
y*a.
y + n.
z*a.
z);
539 return fabs(n.
x*p.
x + n.
y*p.
y + n.
z*p.
z + d);
552 if ( fabs(n1.
x-n2.
x) > 0.1f ||
553 fabs(n1.
y-n2.
y) > 0.1f ||
554 fabs(n1.
z-n2.
z) > 0.1f )
557 float dist =
DistanceToPlane(plane1[0], plane1[1], plane1[2], plane2[0]);
568 float d1 = (d.
x-a.
x)*((b.
y-a.
y)*(c.
z-a.
z)-(c.
y-a.
y)*(b.
z-a.
z)) -
569 (d.
y-a.
y)*((b.
x-a.
x)*(c.
z-a.
z)-(c.
x-a.
x)*(b.
z-a.
z)) +
570 (d.
z-a.
z)*((b.
x-a.
x)*(c.
y-a.
y)-(c.
x-a.
x)*(b.
y-a.
y));
572 float d2 = (d.
x-e.
x)*((b.
y-a.
y)*(c.
z-a.
z)-(c.
y-a.
y)*(b.
z-a.
z)) -
573 (d.
y-e.
y)*((b.
x-a.
x)*(c.
z-a.
z)-(c.
x-a.
x)*(b.
z-a.
z)) +
574 (d.
z-e.
z)*((b.
x-a.
x)*(c.
y-a.
y)-(c.
x-a.
x)*(b.
y-a.
y));
579 i.
x = d.
x + d1/d2*(e.
x-d.
x);
580 i.
y = d.
y + d1/d2*(e.
y-d.
y);
581 i.
z = d.
z + d1/d2*(e.
z-d.
z);
590 float d = (b.
x-a.
x)*(c.
z-a.
z) - (c.
x-a.
x)*(b.
z-a.
z);
591 float d1 = (p.
x-a.
x)*(c.
z-a.
z) - (c.
x-a.
x)*(p.
z-a.
z);
592 float d2 = (b.
x-a.
x)*(p.
z-a.
z) - (p.
x-a.
x)*(b.
z-a.
z);
597 p.
y = a.
y + d1/d*(b.
y-a.
y) + d2/d*(c.
y-a.
y);
void LoadRotationXMatrix(Math::Matrix &mat, float angle)
Loads a rotation matrix along the X axis.
Definition: geometry.h:405
void LoadIdentity()
Loads the identity matrix.
Definition: matrix.h:133
float DistanceToPlane(const Math::Vector &a, const Math::Vector &b, const Math::Vector &c, const Math::Vector &p)
Returns the distance between given point and a plane.
Definition: geometry.h:533
void LoadScaleMatrix(Math::Matrix &mat, const Math::Vector &scale)
Loads a scaling matrix fom given vector.
Definition: geometry.h:392
void LoadOrthoProjectionMatrix(Math::Matrix &mat, float left, float right, float bottom, float top, float zNear=-1.0f, float zFar=1.0f)
Loads an othogonal projection matrix.
Definition: geometry.h:360
bool IsZero(float a, float tolerance=Math::TOLERANCE)
Compares a to zero within tolerance.
Definition: func.h:47
void LoadRotationZXYMatrix(Math::Matrix &mat, const Math::Vector &angles)
Calculates the matrix to make three rotations in the order Z, X and Y.
Definition: geometry.h:483
void LoadZero()
Loads the zero matrix.
Definition: matrix.h:126
float x
X coord.
Definition: point.h:53
Math::Vector Transform(const Math::Matrix &m, const Math::Vector &p)
Transforms the point p by matrix m.
Definition: geometry.h:615
float MidPoint(const Math::Point &a, const Math::Point &b, float px)
Returns py up on the line a - b.
Definition: geometry.h:45
float Distance(const Point &a, const Point &b)
Returns the distance between two points.
Definition: point.h:190
Point struct and related functions.
4x4 matrix
Definition: matrix.h:65
Vector Normalize(const Math::Vector &v)
Convenience function for getting normalized vector.
Definition: vector.h:243
void Normalize()
Normalizes the vector.
Definition: vector.h:101
bool IsInsideTriangle(Math::Point a, Math::Point b, Math::Point c, Math::Point p)
Tests whether the point p is inside the triangle (a,b,c)
Definition: geometry.h:58
float x
X - 1st coord.
Definition: vector.h:56
Math::Vector NormalToPlane(const Math::Vector &p1, const Math::Vector &p2, const Math::Vector &p3)
Returns the normal vector to a plane.
Definition: geometry.h:506
Math::Matrix MultiplyMatrices(const Math::Matrix &left, const Math::Matrix &right)
Convenience function for multiplying a matrix.
Definition: matrix.h:429
void LoadRotationMatrix(Math::Matrix &mat, const Math::Vector &dir, float angle)
Loads a rotation matrix along the given axis.
Definition: geometry.h:448
float y
Y coord.
Definition: point.h:55
float DotProduct(const Math::Vector &left, const Math::Vector &right)
Convenience function for calculating dot product.
Definition: vector.h:251
const float PI
PI.
Definition: const.h:48
bool IntersectY(const Math::Vector &a, const Math::Vector &b, const Math::Vector &c, Math::Vector &p)
Calculates the intersection of the straight line passing through p (x, z)
Definition: geometry.h:588
void LoadViewMatrix(Math::Matrix &mat, const Math::Vector &from, const Math::Vector &at, const Math::Vector &worldUp)
Loads view matrix from the given vectors.
Definition: geometry.h:267
bool Intersect(const Math::Vector &a, const Math::Vector &b, const Math::Vector &c, const Math::Vector &d, const Math::Vector &e, Math::Vector &i)
Calculates the intersection "i" right "of" the plane "abc" (TODO: ?)
Definition: geometry.h:565
const float HUGE_NUM
Huge number.
Definition: const.h:45
bool IsEqual(float a, float b, float tolerance=Math::TOLERANCE)
Compares a and b within tolerance.
Definition: func.h:41
float RotateAngle(float x, float y)
Returns the angle between point (x,y) and (0,0)
Definition: geometry.h:201
Math::Vector MatrixVectorMultiply(const Math::Matrix &m, const Math::Vector &v, bool wDivide=false)
Calculates the result of multiplying m * v.
Definition: matrix.h:447
void LoadRotationXZYMatrix(Math::Matrix &mat, const Math::Vector &angles)
Calculates the matrix to make three rotations in the order X, Z and Y.
Definition: geometry.h:470
Namespace for (new) math code.
Definition: device.h:39
void LoadTranslationMatrix(Math::Matrix &mat, const Math::Vector &trans)
Loads a translation matrix from given vector.
Definition: geometry.h:379
void Swap(int &a, int &b)
Swaps two integers.
Definition: func.h:114
Matrix struct and related functions.
2D point
Definition: point.h:50
void RotatePoint2(const Math::Vector center, float angleH, float angleV, Math::Vector &p)
Rotates a point around a center in space.
Definition: geometry.h:181
Math::Point RotatePoint(const Math::Point ¢er, float angle, const Math::Point &p)
Rotates a point around a center.
Definition: geometry.h:89
float z
Z - 3rd coord.
Definition: vector.h:60
Math::Vector RotateView(Math::Vector center, float angleH, float angleV, float dist)
Calculates point of view to look at a center two angles and a distance.
Definition: geometry.h:634
float Length() const
Returns the vector length.
Definition: vector.h:95
void LoadRotationZMatrix(Math::Matrix &mat, float angle)
Loads a rotation matrix along the Z axis.
Definition: geometry.h:433
void LoadRotationYMatrix(Math::Matrix &mat, float angle)
Loads a rotation matrix along the Y axis.
Definition: geometry.h:419
Constants used in math functions.
Vector struct and related functions.
bool PointsEqual(const Point &a, const Point &b, float tolerance=TOLERANCE)
Checks if two vectors are equal within given tolerance.
Definition: point.h:174
Math::Vector SegmentPoint(const Math::Vector &p1, const Math::Vector &p2, float dist)
Returns a point on the line p1 - p2, in dist distance from p1.
Definition: geometry.h:519
Math::Vector LookatPoint(const Math::Vector &eye, float angleH, float angleV, float length)
Calculates the end point.
Definition: geometry.h:603
Vector CrossProduct(const Math::Vector &left, const Math::Vector &right)
Convenience function for calculating cross product.
Definition: vector.h:257
3D (3x1) vector
Definition: vector.h:53
Math::Vector Projection(const Math::Vector &a, const Math::Vector &b, const Math::Vector &p)
Calculates the projection of the point p on a straight line a to b.
Definition: geometry.h:625
bool IsSamePlane(const Math::Vector(&plane1)[3], const Math::Vector(&plane2)[3])
Checks if two planes defined by three points are the same.
Definition: geometry.h:547
float y
Y - 2nd coord.
Definition: vector.h:58
float m[16]
Matrix values in column-major order.
Definition: matrix.h:68
float DistanceProjected(const Math::Vector &a, const Math::Vector &b)
Returns the distance between projections on XZ plane of two vectors.
Definition: geometry.h:496
void LoadProjectionMatrix(Math::Matrix &mat, float fov=Math::PI/2.0f, float aspect=1.0f, float nearPlane=1.0f, float farPlane=1000.0f)
Loads a perspective projection matrix.
Definition: geometry.h:336