diff options
Diffstat (limited to 'src/math/point.h')
-rw-r--r-- | src/math/point.h | 84 |
1 files changed, 74 insertions, 10 deletions
diff --git a/src/math/point.h b/src/math/point.h index fa411d0..8b9dcba 100644 --- a/src/math/point.h +++ b/src/math/point.h @@ -20,6 +20,9 @@ #pragma once +#include "const.h" +#include "func.h" + #include <cmath> @@ -29,18 +32,9 @@ FPOINT RotatePoint(float angle, FPOINT p); FPOINT RotatePoint(float angle, float dist); void RotatePoint(float cx, float cy, float angle, float &px, float &py); - void RotatePoint(D3DVECTOR center, float angleH, float angleV, D3DVECTOR &p); - void RotatePoint2(D3DVECTOR center, float angleH, float angleV, D3DVECTOR &p); - float RotateAngle(float x, float y); float RotateAngle(FPOINT center, FPOINT p1, FPOINT p2); - float MidPoint(FPOINT a, FPOINT b, float px); - BOOL IsInsideTriangle(FPOINT a, FPOINT b, FPOINT c, FPOINT p); - - BOOL LineFunction(FPOINT p1, FPOINT p2, float &a, float &b); - - float IsInsideTriangle(FPOINT a, FPOINT b, FPOINT c); - + */ // Math module namespace @@ -91,12 +85,82 @@ struct Point } }; +//! Permutes two points +inline void Swap(Point &a, Point &b) +{ + Point c; + + c = a; + a = b; + b = c; +} + //! Returns the distance between two points inline float Distance(const Point &a, const Point &b) { return sqrt((a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y)); } +//! Returns py up on the line \a a - \a b +inline float MidPoint(const Point &a, const Point &b, float px) +{ + if (IsEqual(a.x, b.x)) + { + if (a.y < b.y) + return HUGE; + else + return -HUGE; + } + return (b.y-a.y) * (px-a.x) / (b.x-a.x) + a.y; +} + +//! Calculates the parameters a and b of the linear function passing through \a p1 and \a p2 +/** Returns \c false if the line is vertical. + \param p1,p2 points + \param a,b linear function parameters */ +inline bool LinearFunction(const Point &p1, const Point &p2, float &a, float &b) +{ + if ( IsZero(p1.x-p2.x) ) + { + a = HUGE; + b = p2.x; + return false; + } + + a = (p2.y-p1.y) / (p2.x-p1.x); + b = p2.y - p2.x*a; + + return true; +} + +//! Checks if the point is inside triangle defined by vertices \a a, \a b, \a c +inline bool IsInsideTriangle(Point a, Point b, Point c, const Point &p) +{ + if ( p.x < a.x && p.x < b.x && p.x < c.x ) return false; + if ( p.x > a.x && p.x > b.x && p.x > c.x ) return false; + if ( p.y < a.y && p.y < b.y && p.y < c.y ) return false; + if ( p.y > a.y && p.y > b.y && p.y > c.y ) return false; + + if ( a.x > b.x ) Swap(a,b); + if ( a.x > c.x ) Swap(a,c); + if ( c.x < a.x ) Swap(c,a); + if ( c.x < b.x ) Swap(c,b); + + float n, m; + + n = MidPoint(a, b, p.x); + m = MidPoint(a, c, p.x); + if ( (n>p.y || p.y>m) && (n<p.y || p.y<m) ) + return false; + + n = MidPoint(c, b, p.x); + m = MidPoint(c, a, p.x); + if ( (n>p.y || p.y>m) && (n<p.y || p.y<m) ) + return false; + + return true; +} + /* @} */ // end of group }; // namespace Math |