27 lines
696 B
C++
27 lines
696 B
C++
#include "mathutils.h"
|
|
#include <math.h>
|
|
|
|
float MathUtils::distance(float x1, float y1, float x2, float y2)
|
|
{
|
|
float dy = y2 - y1;
|
|
float dx = x2 - x1;
|
|
return sqrt(dy*dy + dx*dx);
|
|
}
|
|
|
|
// Algorithm found at http://paulbourke.net/geometry/lineline2d/
|
|
bool MathUtils::lines_intersect(int x1, int y1, int x2, int y2,
|
|
int x3, int y3, int x4, int y4)
|
|
{
|
|
int denom = (y4 - y3) * (x2 - x1) - (x4 - x3) * (y2 - y1);
|
|
|
|
int a = (x4 - x3) * (y1 - y3) - (y4 - y3) * (x1 - x3);
|
|
int b = (x2 - x1) * (y1 - y3) - (y2 - y1) * (x1 - x3);
|
|
|
|
if (denom == 0) return false;
|
|
|
|
float ua = (float)a / denom;
|
|
float ub = (float)b / denom;
|
|
|
|
return ua > 0 && ua < 1 && ub > 0 && ub < 1;
|
|
}
|