Giter Club home page Giter Club logo

polygon-contains-point's Introduction

polygon-contains-point

The point-in-polygon (PIP) problem asks whether a given point in the plane lies inside, outside, or on the boundary of a polygon. Wiki reference: Point in polygon

Latest Release

๐Ÿ“ฅ Download - polygon-1.0.2.jar

๐Ÿ“‘ Changelog - https://github.com/sromku/polygon-contains-point/releases/latest

Algorith

The number of intersections for a ray passing from the exterior of the polygon to any point; if odd, it shows that the point lies inside the polygon. If it is even, the point lies outside the polygon.

Usage

Build Polygon

This is the simple version of the polygon. You can build both types of polygons: concave and non-convex

Polygon polygon = Polygon.Builder()
        .addVertex(new Point(1, 3))
        .addVertex(new Point(2, 8))
        .addVertex(new Point(5, 4))
        .addVertex(new Point(5, 9))
        .addVertex(new Point(7, 5))
        .addVertex(new Point(6, 1))
        .addVertex(new Point(3, 1))
        .build();

Build Polygon with Holes

First build all the border sides of the polygon. Close the borders and then you can start adding holes into the polygon. You can add multiple number of holes, just remember to close() after each added hole.

Polygon polygon = Polygon.Builder()
        .addVertex(new Point(1, 2)) // polygon
        .addVertex(new Point(1, 6))
        .addVertex(new Point(8, 7))
        .addVertex(new Point(8, 1))
        .close() 
        .addVertex(new Point(2, 3)) // hole one
        .addVertex(new Point(5, 5))
        .addVertex(new Point(6, 2))
        .close() 
        .addVertex(new Point(6, 6)) // hole two
        .addVertex(new Point(7, 6))
        .addVertex(new Point(7, 5))
        .build();

Check if the point inside

Point point = new Point(4.5f, 7);
boolean contains = polygon.contains(point);

Tests

Two main tests are attached (not in junit format). Both tests cover polygons with holes and without.

License

Apache 2.0. See LICENSE

Follow us

Twitter URL Twitter Follow

polygon-contains-point's People

Contributors

squidpickles avatar sromku avatar

Watchers

 avatar

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.