Shamos Hoey Line intersection algorithm
fdlk / shamos-hoey Goto Github PK
View Code? Open in Web Editor NEWShamos Hoey Line intersection algorithm
License: Other
Shamos Hoey Line intersection algorithm
License: Other
I've been trying to write my own version of shamos-hoey and I tried to use your code to start with. Apparently I figured out it doesnt work in a particular case: if we exclude all intersections related to having same point on an end of line segment it will not work for trivial case:
here is init code:
List<Point2D> points = new List<Point2D>()
{
new Point2D(-2.50291252, -1.23415804),
new Point2D(-3.68300033, -0.474377155),
new Point2D(-1.5621109, -0.437843502),
new Point2D(-2.82422805, 0.294551164)
};
List<LineSegment2D> segments = new List<LineSegment2D>();
for (int i = 0; i < points.Count-1; i++)
{
segments.Add(new LineSegment2D(points[i], points[i+1]));
}
segments.Add(new LineSegment2D(points[points.Count-1], points[0]));
it wont work if we exclude intersection of endpoints, you can do it by adding this in the head of IntersectsWith method:
double kTolerance = 0.0000001;
if (Math.Abs(P1.X - other.P2.X) < kTolerance && Math.Abs(P1.Y - other.P2.Y) < kTolerance ||
Math.Abs(P2.X - other.P1.X) < kTolerance && Math.Abs(P2.Y - other.P1.Y) < kTolerance ||
Math.Abs(P1.X - other.P1.X) < kTolerance && Math.Abs(P1.Y - other.P1.Y) < kTolerance ||
Math.Abs(P2.X - other.P2.X) < kTolerance && Math.Abs(P2.Y - other.P2.Y) < kTolerance)
{
return false;
}
It happens because your shamos code has a bug in segment start intersections section:
case EventType.SegmentStart:
sweepline.Add(currentEvent.Segment);
int i = sweepline.IndexOf(currentEvent.Segment);
if (i > 0 && currentEvent.Segment.IntersectsWith(sweepline[0])) // This is wrong! it has to be currentEvent.Segment.IntersectsWith(sweepline[i-1]) instead
{
return true;
}
if (i < sweepline.Count - 1 && currentEvent.Segment.IntersectsWith(sweepline[i + 1]))
{
return true;
}
break;
Why is there "sweepline[0]" and not "sweepline[i - 1]"?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.