Skip to content

Instantly share code, notes, and snippets.

@evilpie
Created September 26, 2019 10:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save evilpie/ecbf7bf57c9f42c1c9194e785e527221 to your computer and use it in GitHub Desktop.
Save evilpie/ecbf7bf57c9f42c1c9194e785e527221 to your computer and use it in GitHub Desktop.
typedef struct {
int x;
int y;
} Point;
typedef struct {
Point s;
Point e;
} Line;
Point LineLineIntersect(int x1, int y1, //Line 1 start
int x2, int y2, //Line 1 end
int x3, int y3, //Line 2 start
int x4, int y4 //Line 2 end
)
{
Point result;
int detL1 = 0;
int detL2 = 0;
int x1mx2 = 0;
int x3mx4 = 0;
int y1my2 = 0;
int y3my4 = 0;
int xnom = 0;
int ynom = 0;
int denom = 0;
detL1 = x1 * y2 - y1 * x2;
detL2 = x3 * y4 - y3 * x4;
x1mx2 = x1 - x2;
x3mx4 = x3 - x4;
y1my2 = y1 - y2;
y3my4 = y3 - y4;
xnom = detL1 * x3mx4 - x1mx2 * detL2;
ynom = detL1 * y3my4 - y1my2 * detL2;
denom = x1mx2 * y3my4 - y1my2 * x3mx4;
if (denom == 0) {
// Lines don't seem to cross
result.x = 99999;
result.y = 99999;
return result;
}
result.x = xnom / denom;
result.y = ynom / denom;
return result;
}
Point mpc_main(Line INPUT_A, Line INPUT_B) {
return LineLineIntersect(INPUT_A.s.x, INPUT_A.s.y, INPUT_A.e.x, INPUT_A.e.y,
INPUT_B.s.x, INPUT_B.s.y, INPUT_B.e.x, INPUT_B.e.y);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment