////////////////////////// [ MATH2D ] ////////////////////////////////
#pragma once
#include "OAGVector3d.h"
#include "OAGVector3f.h"
#include "MathDefinitions.h"
#include <math.h>
////////////////////////// [START INLINE FUNCTIONS ] ////////////////////////////////
//Used for checking collisions in filled polygons
inline double Angle2D(double x1, double y1, double x2, double y2)
{
double dtheta,theta1,theta2;
theta1 = atan2(y1,x1);
theta2 = atan2(y2,x2);
dtheta = theta2 - theta1;
while (dtheta > _PI)
dtheta -= _2PI;
while (dtheta < - _PI)
dtheta += _2PI;
return(dtheta);
}
//Used for checking collisions in lineIntersection
inline bool LineIntersection(oag::OAGVector3d* a, oag::OAGVector3d* b, oag::OAGVector3d* c, oag::OAGVector3d* d)
{
double r, s;
r = ( ((a->m_Y - c->m_Y)*(d->m_X - c->m_X)) - ((a->m_X - c->m_X)*(d->m_Y - c->m_Y)) )/
( ((b->m_X - a->m_X)*(d->m_Y - c->m_Y)) - ((b->m_Y - a->m_Y)*(d->m_X - c->m_X)) );
s = ( ( (a->m_Y - c->m_Y)*(b->m_X - a->m_X) ) - ( (a->m_X - c->m_X)*(b->m_Y - a->m_Y) ) ) /
( ( (b->m_X - a->m_X)*(d->m_Y - c->m_Y) ) - ( (b->m_Y - a->m_Y)*(d->m_X - c->m_X) ) );
if ( (r>=0 && r<=1) && (s>=0 && s<=1) )
return true;
return false;
}
inline bool LineIntersection(oag::OAGVector3f* a, oag::OAGVector3f* b, oag::OAGVector3f* c, oag::OAGVector3f* d)
{
float r, s;
r = ( ((a->m_Y - c->m_Y)*(d->m_X - c->m_X)) - ((a->m_X - c->m_X)*(d->m_Y - c->m_Y)) )/
( ((b->m_X - a->m_X)*(d->m_Y - c->m_Y)) - ((b->m_Y - a->m_Y)*(d->m_X - c->m_X)) );
s = ( ( (a->m_Y - c->m_Y)*(b->m_X - a->m_X) ) - ( (a->m_X - c->m_X)*(b->m_Y - a->m_Y) ) ) /
( ( (b->m_X - a->m_X)*(d->m_Y - c->m_Y) ) - ( (b->m_Y - a->m_Y)*(d->m_X - c->m_X) ) );
if ( (r>=0 && r<=1) && (s>=0 && s<=1) )
return true;
return false;
}
//Used for testing whether if a point is inside of a triangle
inline bool PointInsideTriangle(oag::OAGVector3d* p1, oag::OAGVector3d* p2, oag::OAGVector3d* p3, oag::OAGVector3d clickPoint)
{
double fAB = (clickPoint.m_Y-p1->m_Y)*(p2->m_X-p1->m_X) - (clickPoint.m_X-p1->m_X)*(p2->m_Y-p1->m_Y);
double fCA = (clickPoint.m_Y-p3->m_Y)*(p1->m_X-p3->m_X) - (clickPoint.m_X-p3->m_X)*(p1->m_Y-p3->m_Y);
double fBC = (clickPoint.m_Y-p2->m_Y)*(p3->m_X-p2->m_X) - (clickPoint.m_X-p2->m_X)*(p3->m_Y-p2->m_Y);
return (fAB*fBC>0 && fBC*fCA>0) ? true : false;
}
//inline bool PointInsideTriangle(oag::OAGVector3d* p1, oag::OAGVector3d* p2, oag::OAGVector3d* p3, oag::OAGVector3d clickPoint)
//{
// float fAB = (clickPoint.m_Y-p1->m_Y)*(p2->m_X-p1->m_X) - (clickPoint.m_X-p1->m_X)*(p2->m_Y-p1->m_Y);
// float fCA = (clickPoint.m_Y-p3->m_Y)*(p1->m_X-p3->m_X) - (clickPoint.m_X-p3->m_X)*(p1->m_Y-p3->m_Y);
// float fBC = (clickPoint.m_Y-p2->m_Y)*(p3->m_X-p2->m_X) - (clickPoint.m_X-p2->m_X)*(p3->m_Y-p2->m_Y);
//
// return (fAB*fBC>0 && fBC*fCA>0) ? true : false;
//}
//Used for testing whether a point is inside of a rectangle
inline bool PointInsideRectangle(oag::OAGVector3d* lowerPoint, oag::OAGVector3d* higherPoint, oag::OAGVector3d clickPoint)
{
return clickPoint.m_X>=lowerPoint->m_X && clickPoint.m_X<=higherPoint->m_X && clickPoint.m_Y >=lowerPoint->m_Y && clickPoint.m_Y <= higherPoint->m_Y;
}
//inline bool PointInsideRectangle(oag::OAGVector3d* lowerPoint, oag::OAGVector3d* higherPoint, oag::OAGVector3d clickPoint)
//{
// return clickPoint.m_X>=lowerPoint->m_X && clickPoint.m_X<=higherPoint->m_X && clickPoint.m_Y >=lowerPoint->m_Y && clickPoint.m_Y <= higherPoint->m_Y;
//}
//Used for testing whether a number is power of two
inline bool IsPowerOf2(int number)
{
int count=0;
for(int i=15;i>=0;i--)
{
if((1<<i) & number)
count++;
}
if(count == 1)
return true;
return false;
}
////////////////////////// [END INLINE FUNCTIONS ] ////////////////////////////////