From 79de5d881ffa864b8c33462fda27608a9b20a651 Mon Sep 17 00:00:00 2001 From: randomguy3 Date: Tue, 17 Jan 2012 19:11:27 +0000 Subject: Be a bit more forgiving about whether a point is on a line segment git-svn-id: https://tikzit.svn.sourceforge.net/svnroot/tikzit/trunk@389 7c02a99a-9b00-45e3-bf44-6f3dd7fddb64 --- tikzit/src/common/util.m | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) (limited to 'tikzit/src/common/util.m') diff --git a/tikzit/src/common/util.m b/tikzit/src/common/util.m index 3dec971..ad04a96 100644 --- a/tikzit/src/common/util.m +++ b/tikzit/src/common/util.m @@ -21,6 +21,10 @@ #import "util.h" #import "math.h" +static BOOL fuzzyCompare(float f1, float f2) { + return (ABS(f1 - f2) <= 0.00001f * MIN(ABS(f1), ABS(f2))); +} + NSRect NSRectAroundPointsWithPadding(NSPoint p1, NSPoint p2, float padding) { return NSMakeRect(MIN(p1.x,p2.x)-padding, MIN(p1.y,p2.y)-padding, @@ -104,7 +108,10 @@ static BOOL lineSegmentContainsPoint(NSPoint l1, NSPoint l2, float x, float y) { float maxX = MAX(l1.x, l2.x); float minY = MIN(l1.y, l2.y); float maxY = MAX(l1.y, l2.y); - return x >= minX && x <= maxX && y >= minY && y <= maxY; + return (x >= minX || fuzzyCompare (x, minX)) && + (x <= maxX || fuzzyCompare (x, maxX)) && + (y >= minY || fuzzyCompare (y, minY)) && + (y <= maxY || fuzzyCompare (y, maxY)); } BOOL lineSegmentsIntersect(NSPoint l1start, NSPoint l1end, NSPoint l2start, NSPoint l2end, NSPoint *result) { -- cgit v1.2.3