1
0
mirror of git://projects.qi-hardware.com/cae-tools.git synced 2024-12-23 09:55:32 +02:00

cameo/area.c: move equation solving from "intersect", for future sharing

This commit is contained in:
Werner Almesberger 2012-03-20 19:02:50 -03:00
parent 19bc92aa79
commit d79424d9ae

View File

@ -84,6 +84,29 @@ static int is_inside(const struct path *a, const struct path *b)
/* /*
* Solve * Solve
* *
* ax+by = e
* cx+dy = f
*
* with Cramer's rule:
* http://en.wikipedia.org/wiki/Cramer's_rule
*/
static int cramer2(double a, double b, double c, double d, double e, double f,
double *x, double *y)
{
double det;
det = a*d-b*c;
if (fabs(det) < EPSILON)
return 0;
*x = (e*d-b*f)/det;
*y = (a*f-e*c)/det;
return 1;
}
/*
* Solve
* ax+na*bx = cx+nb*dx * ax+na*bx = cx+nb*dx
* ay+na*by = cy+nb*dy * ay+na*by = cy+nb*dy
* *
@ -91,23 +114,12 @@ static int is_inside(const struct path *a, const struct path *b)
* *
* na*bx + nb*-dx = cx-ax * na*bx + nb*-dx = cx-ax
* na*by + nb*-dy = cy-ay * na*by + nb*-dy = cy-ay
*
* which we the solve with Cramer's rule:
* http://en.wikipedia.org/wiki/Cramer's_rule
*/ */
static int intersect(double ax, double ay, double bx, double by, static int intersect(double ax, double ay, double bx, double by,
double cx, double cy, double dx, double dy, double *na, double *nb) double cx, double cy, double dx, double dy, double *na, double *nb)
{ {
double det; return cramer2(bx, -dx, by, -dy, cx-ax, cy-ay, na, nb);
det = dx*by-bx*dy;
if (fabs(det) < EPSILON)
return 0;
*na = (dx*(cy-ay)-dy*(cx-ax))/det;
*nb = (bx*(cy-ay)-by*(cx-ax))/det;
return 1;
} }