Coder Social home page Coder Social logo

2d-polygon-boolean-lua's Introduction

2d-polygon-boolean-lua

Port Lua of the Javascript implementation of the Greiner-Hormann algorithm taken from the paper "efficient clipping of arbitrary polygons" made by tmpvar. Original repository link here.

Use

This module is imported like this:

local polygonBoolean = require("2d-polygon-boolean")

The polygons given as a parameter must be represented by tables constructed as follows:

polygon = { x1,y1, x2,y2, x3,y3, ... }

And the function should be called like this:

polygons = polygonBoolean(polygon_subject, polygon_clip, operation, get_most_revelant)

polygon_subject is the subject polygon of the operation. polygon_clip is the polygon that will perform the operation.

operation is a string value which can be:

or : to perform a union of the polygons.
not : to perform an overlapping exclusion on the subject polygon.
and : to keep only the overlapping part.

get_most_revelant is used to keep only the most relevant result. By default this parameter is false and therefore the function returns a table of several polygons (whether an intersection was found or not) like this:

{
    { x1,y1, x2,y2, x3,y3, ... },
    { x1,y1, x2,y2, x3,y3, ... },
    ...
}

If this parameter is true and an intersection was found it will return the resulting polygon containing the most vertices (is relevant in most cases but may not be in some rare cases like with exclusion) or returns false, polygons in case no intersection was found between the subject polygon and the clip polygon.

Complete example

local polygonBoolean = require("2d-polygon-boolean")

local subject = { 0,0, 100,0, 100,100, 0,100 }
local clip = { 90,90, 110,90, 110,110, 90,110, 90,90 }

local union = polygonBoolean(subject, clip, "or")

for i, result in ipairs(union) do
    print("result "..i..": ")
    for j = 1, #result-1, 2 do
        print(
            "x"..((j+1)/2)..": "..result[j],
            "y"..((j+1)/2)..": "..result[j+1]
        )
    end
end

--[[
result 1: 
    x1: 100 y1: 90
    x2: 110 y2: 90
    x3: 110 y3: 110
    x4: 90  y4: 110
    x5: 90  y5: 100
    x6: 0   y6: 100
    x7: 0   y7: 0
    x8: 100 y8: 0
]]

local cut = polygonBoolean(subject, clip, "not")

for i, result in ipairs(cut) do
    print("result "..i..": ")
    for j = 1, #result-1, 2 do
        print(
            "x"..((j+1)/2)..": "..result[j],
            "y"..((j+1)/2)..": "..result[j+1]
        )
    end
end

--[[
result 1: 
    x1: 100 y1: 90
    x2: 90  y2: 90
    x3: 90  y3: 100
    x4: 0   y4: 100
    x5: 0   y5: 0
    x6: 100 y6: 0
]]

local intersect = polygonBoolean(subject, clip, "and")

for i, result in ipairs(intersect) do
    print("result "..i..": ")
    for j = 1, #result-1, 2 do
        print(
            "x"..((j+1)/2)..": "..result[j],
            "y"..((j+1)/2)..": "..result[j+1]
        )
    end
end

--[[
result 1: 
    x1: 100 y1: 90
    x2: 90  y2: 90
    x3: 90  y3: 100
    x4: 100 y4: 100
]]

License

MIT

2d-polygon-boolean-lua's People

Contributors

bigfoot71 avatar

Stargazers

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