53 return (b.
y-a.
y) * (px-a.
x) / (b.
x-a.
x) + a.
y;
61 if ( p.
x < a.
x && p.
x < b.
x && p.
x < c.
x )
return false;
62 if ( p.
x > a.
x && p.
x > b.
x && p.
x > c.
x )
return false;
63 if ( p.
y < a.
y && p.
y < b.
y && p.
y < c.
y )
return false;
64 if ( p.
y > a.
y && p.
y > b.
y && p.
y > c.
y )
return false;
66 if ( a.
x > b.
x )
Swap(a,b);
67 if ( a.
x > c.
x )
Swap(a,c);
68 if ( c.
x < a.
x )
Swap(c,a);
69 if ( c.
x < b.
x )
Swap(c,b);
73 if ( (n>p.
y||p.
y>m) && (n<p.
y||p.
y<m) )
return false;
77 if ( (n>p.
y||p.
y>m) && (n<p.
y||p.
y<m) )
return false;
95 b.
x = a.
x*cosf(angle) - a.
y*sinf(angle);
96 b.
y = a.
x*sinf(angle) + a.
y*cosf(angle);
111 float x = p.
x*cosf(angle) - p.
y*sinf(angle);
112 float y = p.
x*sinf(angle) + p.
y*cosf(angle);
124 float x = dist*cosf(angle);
125 float y = dist*sinf(angle);
136 inline void RotatePoint(
float cx,
float cy,
float angle,
float &px,
float &py)
143 ax = px*cosf(angle) - py*sinf(angle);
144 ay = px*sinf(angle) + py*cosf(angle);
165 b.
x = p.
x*cosf(angleH) - p.
z*sinf(angleH);
166 b.
y = p.
z*sinf(angleV) + p.
y*cosf(angleV);
167 b.
z = p.
x*sinf(angleH) + p.
z*cosf(angleH);
187 a.
x = p.
x*cosf(angleH) - p.
z*sinf(angleH);
189 a.
z = p.
x*sinf(angleH) + p.
z*cosf(angleH);
193 b.
y = a.
z*sinf(angleV) + a.
y*cosf(angleV);
194 b.
z = a.
z*cosf(angleV) - a.
y*sinf(angleV);
202 if (x == 0.0f && y == 0.0f)
return 0.0f;
208 if (x > y)
return atanf(y/x);
209 else return PI*0.5f - atanf(x/y);
213 if (x > -y)
return PI*2.0f + atanf(y/x);
214 else return PI*1.5f - atanf(x/y);
221 if (-x > y)
return PI*1.0f + atanf(y/x);
222 else return PI*0.5f - atanf(x/y);
226 if (-x > -y)
return PI*1.0f + atanf(y/x);
227 else return PI*1.5f - atanf(x/y);
246 float a1 = asinf((p1.
y - center.
y) /
Distance(p1, center));
247 float a2 = asinf((p2.
y - center.
y) /
Distance(p2, center));
249 if (p1.
x < center.
x) a1 =
PI - a1;
250 if (p2.
x < center.
x) a2 =
PI - a2;
273 float length = view.
Length();
274 assert(!
IsZero(length) );
336 float nearPlane = 1.0f,
float farPlane = 1000.0f)
338 assert(fabs(farPlane - nearPlane) >= 0.01f);
339 assert(fabs(sin(fov / 2)) >= 0.01f);
341 float f = cosf(fov / 2.0f) / sinf(fov / 2.0f);
345 mat.
m[0 ] = f / aspect;
347 mat.
m[10] = (nearPlane + farPlane) / (nearPlane - farPlane);
349 mat.
m[14] = (2.0f * farPlane * nearPlane) / (nearPlane - farPlane);
360 float zNear = -1.0f,
float zFar = 1.0f)
364 mat.
m[0 ] = 2.0f / (right - left);
365 mat.
m[5 ] = 2.0f / (top - bottom);
366 mat.
m[10] = -2.0f / (zFar - zNear);
368 mat.
m[12] = - (right + left) / (right - left);
369 mat.
m[13] = - (top + bottom) / (top - bottom);
370 mat.
m[14] = - (zFar + zNear) / (zFar - zNear);
407 mat.
m[5 ] = cosf(angle);
408 mat.
m[6 ] = sinf(angle);
409 mat.
m[9 ] = -sinf(angle);
410 mat.
m[10] = cosf(angle);
421 mat.
m[0 ] = cosf(angle);
422 mat.
m[2 ] = -sinf(angle);
423 mat.
m[8 ] = sinf(angle);
424 mat.
m[10] = cosf(angle);
435 mat.
m[0 ] = cosf(angle);
436 mat.
m[1 ] = sinf(angle);
437 mat.
m[4 ] = -sinf(angle);
438 mat.
m[5 ] = cosf(angle);
449 float cos = cosf(angle);
450 float sin = sinf(angle);
455 mat.
m[0 ] = (v.
x * v.
x) * (1.0f - cos) + cos;
456 mat.
m[1 ] = (v.
x * v.
y) * (1.0f - cos) - (v.
z * sin);
457 mat.
m[2 ] = (v.
x * v.
z) * (1.0f - cos) + (v.
y * sin);
459 mat.
m[4 ] = (v.
y * v.
x) * (1.0f - cos) + (v.
z * sin);
460 mat.
m[5 ] = (v.
y * v.
y) * (1.0f - cos) + cos ;
461 mat.
m[6 ] = (v.
y * v.
z) * (1.0f - cos) - (v.
x * sin);
463 mat.
m[8 ] = (v.
z * v.
x) * (1.0f - cos) - (v.
y * sin);
464 mat.
m[9 ] = (v.
z * v.
y) * (1.0f - cos) + (v.
x * sin);
465 mat.
m[10] = (v.
z * v.
z) * (1.0f - cos) + cos;
497 return sqrtf( (a.
x-b.
x)*(a.
x-b.
x) +
498 (a.
z-b.
z)*(a.
z-b.
z) );
524 return p1 + direction * dist;
536 float d = -(n.
x*a.
x + n.
y*a.
y + n.
z*a.
z);
538 return fabs(n.
x*p.
x + n.
y*p.
y + n.
z*p.
z + d);
551 if ( fabs(n1.
x-n2.
x) > 0.1f ||
552 fabs(n1.
y-n2.
y) > 0.1f ||
553 fabs(n1.
z-n2.
z) > 0.1f )
556 float dist =
DistanceToPlane(plane1[0], plane1[1], plane1[2], plane2[0]);
567 float d1 = (d.
x-a.
x)*((b.
y-a.
y)*(c.
z-a.
z)-(c.
y-a.
y)*(b.
z-a.
z)) -
568 (d.
y-a.
y)*((b.
x-a.
x)*(c.
z-a.
z)-(c.
x-a.
x)*(b.
z-a.
z)) +
569 (d.
z-a.
z)*((b.
x-a.
x)*(c.
y-a.
y)-(c.
x-a.
x)*(b.
y-a.
y));
571 float d2 = (d.
x-e.
x)*((b.
y-a.
y)*(c.
z-a.
z)-(c.
y-a.
y)*(b.
z-a.
z)) -
572 (d.
y-e.
y)*((b.
x-a.
x)*(c.
z-a.
z)-(c.
x-a.
x)*(b.
z-a.
z)) +
573 (d.
z-e.
z)*((b.
x-a.
x)*(c.
y-a.
y)-(c.
x-a.
x)*(b.
y-a.
y));
578 i.
x = d.
x + d1/d2*(e.
x-d.
x);
579 i.
y = d.
y + d1/d2*(e.
y-d.
y);
580 i.
z = d.
z + d1/d2*(e.
z-d.
z);
589 float d = (b.
x-a.
x)*(c.
z-a.
z) - (c.
x-a.
x)*(b.
z-a.
z);
590 float d1 = (p.
x-a.
x)*(c.
z-a.
z) - (c.
x-a.
x)*(p.
z-a.
z);
591 float d2 = (b.
x-a.
x)*(p.
z-a.
z) - (p.
x-a.
x)*(b.
z-a.
z);
596 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:404
void LoadIdentity()
Loads the identity matrix.
Definition: matrix.h:131
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:532
void LoadScaleMatrix(Math::Matrix &mat, const Math::Vector &scale)
Loads a scaling matrix fom given vector.
Definition: geometry.h:391
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:359
bool IsZero(float a, float tolerance=Math::TOLERANCE)
Compares a to zero within tolerance.
Definition: func.h:46
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:482
void LoadZero()
Loads the zero matrix.
Definition: matrix.h:124
float x
X coord.
Definition: point.h:52
Math::Vector Transform(const Math::Matrix &m, const Math::Vector &p)
Transforms the point p by matrix m.
Definition: geometry.h:614
float MidPoint(const Math::Point &a, const Math::Point &b, float px)
Returns py up on the line a - b.
Definition: geometry.h:44
float Distance(const Point &a, const Point &b)
Returns the distance between two points.
Definition: point.h:189
Point struct and related functions.
4x4 matrix
Definition: matrix.h:66
Vector Normalize(const Math::Vector &v)
Convenience function for getting normalized vector.
Definition: vector.h:242
void Normalize()
Normalizes the vector.
Definition: vector.h:100
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:57
float x
X - 1st coord.
Definition: vector.h:55
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:505
Math::Matrix MultiplyMatrices(const Math::Matrix &left, const Math::Matrix &right)
Convenience function for multiplying a matrix.
Definition: matrix.h:427
void LoadRotationMatrix(Math::Matrix &mat, const Math::Vector &dir, float angle)
Loads a rotation matrix along the given axis.
Definition: geometry.h:447
float y
Y coord.
Definition: point.h:54
float DotProduct(const Math::Vector &left, const Math::Vector &right)
Convenience function for calculating dot product.
Definition: vector.h:250
const float PI
PI.
Definition: const.h:47
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:587
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:266
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:564
const float HUGE_NUM
Huge number.
Definition: const.h:44
bool IsEqual(float a, float b, float tolerance=Math::TOLERANCE)
Compares a and b within tolerance.
Definition: func.h:40
float RotateAngle(float x, float y)
Returns the angle between point (x,y) and (0,0)
Definition: geometry.h:200
Math::Vector MatrixVectorMultiply(const Math::Matrix &m, const Math::Vector &v, bool wDivide=false)
Calculates the result of multiplying m * v.
Definition: matrix.h:445
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:469
Namespace for (new) math code.
Definition: const.h:32
void LoadTranslationMatrix(Math::Matrix &mat, const Math::Vector &trans)
Loads a translation matrix from given vector.
Definition: geometry.h:378
void Swap(int &a, int &b)
Swaps two integers.
Definition: func.h:104
Matrix struct and related functions.
2D point
Definition: point.h:49
void RotatePoint2(const Math::Vector center, float angleH, float angleV, Math::Vector &p)
Rotates a point around a center in space.
Definition: geometry.h:180
Math::Point RotatePoint(const Math::Point ¢er, float angle, const Math::Point &p)
Rotates a point around a center.
Definition: geometry.h:88
float z
Z - 3rd coord.
Definition: vector.h:59
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:633
float Length() const
Returns the vector length.
Definition: vector.h:94
void LoadRotationZMatrix(Math::Matrix &mat, float angle)
Loads a rotation matrix along the Z axis.
Definition: geometry.h:432
void LoadRotationYMatrix(Math::Matrix &mat, float angle)
Loads a rotation matrix along the Y axis.
Definition: geometry.h:418
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:173
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:518
Math::Vector LookatPoint(const Math::Vector &eye, float angleH, float angleV, float length)
Calculates the end point.
Definition: geometry.h:602
Vector CrossProduct(const Math::Vector &left, const Math::Vector &right)
Convenience function for calculating cross product.
Definition: vector.h:256
3D (3x1) vector
Definition: vector.h:52
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:624
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:546
float y
Y - 2nd coord.
Definition: vector.h:57
float m[16]
Matrix values in column-major order.
Definition: matrix.h:69
float DistanceProjected(const Math::Vector &a, const Math::Vector &b)
Returns the distance between projections on XZ plane of two vectors.
Definition: geometry.h:495
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:335