GithubHelp home page GithubHelp logo

shamos-hoey's Introduction

shamos-hoey

Shamos Hoey Line intersection algorithm

shamos-hoey's People

Contributors

fdlk avatar

Stargazers

 avatar  avatar

Watchers

 avatar

Forkers

tomgregar

shamos-hoey's Issues

Doesnt work on a particular case:

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]));

visualisation:
image

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;

ShamosHoey.cs

Why is there "sweepline[0]" and not "sweepline[i - 1]"?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.