From 7a61482a045f50239106f4bd8dab91ad2202c26b Mon Sep 17 00:00:00 2001 From: Werner Almesberger Date: Tue, 20 Mar 2012 19:30:03 -0300 Subject: [PATCH] cameo/area.c: move handling of left/right from hit_path to hit_segment For future extensions. --- cameo/area.c | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/cameo/area.c b/cameo/area.c index 322f892..7b88b53 100644 --- a/cameo/area.c +++ b/cameo/area.c @@ -155,9 +155,9 @@ static int touch(double ax, double ay, double bx, double by, static int hit_segment(double fx, double fy, double tx, double ty, const struct point *a, const struct point *b, double r, int enter, - double *n) + int left, double *n) { - double dx, dy, d; + double dx, dy, nx, ny, nn; double px, py; double na, nb; @@ -167,14 +167,22 @@ static int hit_segment(double fx, double fy, double tx, double ty, dx = b->x-a->x; dy = b->y-a->y; + if (left) { + nx = dx; + ny = dy; + } else { + nx = -dx; + ny = -dy; + } + /* -dy becomes the x component of the normal vector */ - if (enter ? dy < 0 : dy > 0) + if (enter ? ny < 0 : ny > 0) return 0; - d = hypot(dx, dy); + nn = hypot(nx, ny); - px = a->x-dy/d*r; - py = a->y+dx/d*r; + px = a->x-ny/nn*r; + py = a->y+nx/nn*r; if (!intersect(fx, fy, tx, ty, px, py, dx, dy, &na, &nb)) return 0; @@ -205,8 +213,8 @@ static int hit_path(double fx, double fy, double tx, double ty, if (inside) left = !left; for (p = path->first; p != path->last; p = p->next) { - if (hit_segment(fx, fy, tx, ty, - left ? p : p->next, left ? p->next : p, r, enter, &tmp)) { + if (hit_segment(fx, fy, tx, ty, p, p->next, + r, enter, left, &tmp)) { if (!found || nx > tmp) nx = tmp; found = 1;