Giter Site home page Giter Site logo

2d-polygon-self-intersections-lua's Introduction

2d-polygon-self-intersections-lua

This is a Lua port of JS library 2d-polygon-self-intersections to find self-intersections in a 2d polygon.

This library may not be fast, but it is robust. Robust in the fact that it will find all of the self-intersections in a polygon - minus of course shared endpoints.

You can expect a time complexity of O(n^2)

How to use it

local intersections = require("intersections")

local poly = { 0,0, 10,0, 0,10, 10,10 }

local isects = intersections(poly)

for i=1,#isects-1,2 do
    print("x: "..isects[i], "y: "..isects[i+1])
end

-- outputs: x: 5    y: 5

Description

intersections(polygon, filterFunc)

  • polygon - A table with a sequence of vertices (i.e. a triangle { 0,0, 10,0, 10,10 })
  • filterFunc - A filter function called whenever an intersection is found: filterFunc(ix,iy, i, x1,y1, x2,y2, j, x3,y3, x4,y4, unique)
  • ix,iy - current intersection (e.g. x: 5, y: 5) - mutations in this array get collected
  • i - index of the segment (e.g 1)
  • x1,y1 - start of the first segment (e.g 0,5)
  • x2,y2 - start of the first segment (e.g 10,5)
  • j - index of the segment (e.g 3)
  • x3,y3 - start of the first segment (e.g 5,0)
  • x4,y4 - start of the first segment (e.g 5,10)
  • unique - boolean representing whether or not this intersection point has been seen before
  • return true to collect and false to discard

returns nil if no intersection is found or a table listing all intersections in a row.

NOTE: this library assumes the polygon is closed, so manually adding the start point as the end point has no effect.

Also note that there are 2 intersections per crossing, this library by default will only report one - all intersections will be unique. This behavior can be changed with the filterFunc.

License

MIT

2d-polygon-self-intersections-lua's People

Contributors

bigfoot71 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.