<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en-GB">
	<id>https://wiki.gideros.rocks/index.php?action=history&amp;feed=atom&amp;title=Clipper_Demo</id>
	<title>Clipper Demo - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.gideros.rocks/index.php?action=history&amp;feed=atom&amp;title=Clipper_Demo"/>
	<link rel="alternate" type="text/html" href="https://wiki.gideros.rocks/index.php?title=Clipper_Demo&amp;action=history"/>
	<updated>2026-11-17T12:06:33Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.43.11</generator>
	<entry>
		<id>https://wiki.gideros.rocks/index.php?title=Clipper_Demo&amp;diff=25529&amp;oldid=prev</id>
		<title>MoKaLux: Created page with &quot;__NOTOC__ &#039;&#039;&#039;Available since:&#039;&#039;&#039; Gideros 2015.03.22&lt;br/&gt; &#039;&#039;&#039;Class:&#039;&#039;&#039; Clipper&lt;br/&gt;  === Description === Full Clipper demo for Gideros. &lt;syntaxhighlight lang=&quot;lua&quot;&gt; -- Clip...&quot;</title>
		<link rel="alternate" type="text/html" href="https://wiki.gideros.rocks/index.php?title=Clipper_Demo&amp;diff=25529&amp;oldid=prev"/>
		<updated>2026-11-14T04:33:46Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;__NOTOC__ &amp;#039;&amp;#039;&amp;#039;Available since:&amp;#039;&amp;#039;&amp;#039; Gideros 2015.03.22&amp;lt;br/&amp;gt; &amp;#039;&amp;#039;&amp;#039;Class:&amp;#039;&amp;#039;&amp;#039; &lt;a href=&quot;/index.php/Clipper&quot; title=&quot;Clipper&quot;&gt;Clipper&lt;/a&gt;&amp;lt;br/&amp;gt;  === Description === Full Clipper demo for Gideros. &amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt; -- Clip...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;__NOTOC__&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Available since:&amp;#039;&amp;#039;&amp;#039; Gideros 2015.03.22&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Class:&amp;#039;&amp;#039;&amp;#039; [[Clipper]]&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Description ===&lt;br /&gt;
Full Clipper demo for Gideros.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
-- Clipper demo for Gideros (Luau)&lt;br /&gt;
-- Boolean operations, offsets, cleaning, simplifying and reversing&lt;br /&gt;
-- of polygons, built on top of the Clipper plugin.&lt;br /&gt;
&lt;br /&gt;
local clipperOk = pcall(require, &amp;quot;clipper&amp;quot;)&lt;br /&gt;
if not clipperOk or not Polygon or not Clipper then&lt;br /&gt;
    local t = TextField.new(nil, &amp;quot;Clipper plugin not enabled&amp;quot;)&lt;br /&gt;
    t:setTextColor(0xff0000)&lt;br /&gt;
    t:setPosition(10, 40)&lt;br /&gt;
    stage:addChild(t)&lt;br /&gt;
    return&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
-------------------------------------------------------------------------------&lt;br /&gt;
-- Palette and layout&lt;br /&gt;
-------------------------------------------------------------------------------&lt;br /&gt;
&lt;br /&gt;
local C = {&lt;br /&gt;
    bg = 0xf4f4f4, card = 0xffffff, line = 0xcccccc,&lt;br /&gt;
    subject = 0x2a6fdb, clip = 0xe05252, result = 0x2faa4a,&lt;br /&gt;
    bounds = 0xffb000, text = 0x222222, dim = 0x666666,&lt;br /&gt;
    accent = 0x2a6fdb, btn = 0xe8e8e8, btnActive = 0xcfe3ff,&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
local DESIGN_W, DESIGN_H = 320, 480&lt;br /&gt;
local CANVAS_X, CANVAS_Y = 10, 70&lt;br /&gt;
local CANVAS_W, CANVAS_H = 300, 250&lt;br /&gt;
&lt;br /&gt;
local screenW, screenH = application:getContentWidth(), application:getContentHeight()&lt;br /&gt;
application:setBackgroundColor(C.bg)&lt;br /&gt;
&lt;br /&gt;
local ui = Sprite.new()&lt;br /&gt;
ui:setPosition(math.floor((screenW - DESIGN_W) / 2), math.floor((screenH - DESIGN_H) / 2))&lt;br /&gt;
stage:addChild(ui)&lt;br /&gt;
&lt;br /&gt;
-------------------------------------------------------------------------------&lt;br /&gt;
-- UI helpers&lt;br /&gt;
-------------------------------------------------------------------------------&lt;br /&gt;
&lt;br /&gt;
local function addPanel(parent, x, y, w, h, color)&lt;br /&gt;
    local p = Pixel.new(color, 1, w, h)&lt;br /&gt;
    p:setPosition(x, y)&lt;br /&gt;
    parent:addChild(p)&lt;br /&gt;
    return p&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
local function addText(parent, text, x, y, color, scale)&lt;br /&gt;
    local t = TextField.new(nil, text)&lt;br /&gt;
    t:setTextColor(color)&lt;br /&gt;
    t:setPosition(x, y)&lt;br /&gt;
    if scale then t:setScale(scale) end&lt;br /&gt;
    parent:addChild(t)&lt;br /&gt;
    return t&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
local function centerX(t, y)&lt;br /&gt;
    t:setPosition(math.floor((DESIGN_W - t:getWidth()) / 2), y)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
local buttonRegistry = {}&lt;br /&gt;
&lt;br /&gt;
local function makeButton(x, y, w, h, label, onClick)&lt;br /&gt;
    local root = Sprite.new()&lt;br /&gt;
    root:setPosition(x, y)&lt;br /&gt;
&lt;br /&gt;
    local bg = Pixel.new(C.btn, 1, w, h)&lt;br /&gt;
    root:addChild(bg)&lt;br /&gt;
&lt;br /&gt;
    local txt = TextField.new(nil, label)&lt;br /&gt;
    txt:setTextColor(C.text)&lt;br /&gt;
    txt:setPosition(math.floor((w - txt:getWidth()) / 2),&lt;br /&gt;
        math.floor((h - txt:getHeight()) / 2))&lt;br /&gt;
    root:addChild(txt)&lt;br /&gt;
&lt;br /&gt;
    local active = false&lt;br /&gt;
    local function refresh(down)&lt;br /&gt;
        bg:setColor(down and C.btnActive or (active and C.btnActive or C.btn))&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    function root:setText(s)&lt;br /&gt;
        txt:setText(s)&lt;br /&gt;
        txt:setPosition(math.floor((w - txt:getWidth()) / 2),&lt;br /&gt;
            math.floor((h - txt:getHeight()) / 2))&lt;br /&gt;
    end&lt;br /&gt;
    function root:setActive(v)&lt;br /&gt;
        active = v&lt;br /&gt;
        refresh(false)&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    local entry = {root = root, x = x, y = y, w = w, h = h,&lt;br /&gt;
        onClick = onClick, refresh = refresh, active = function(v) active = v end}&lt;br /&gt;
    table.insert(buttonRegistry, entry)&lt;br /&gt;
&lt;br /&gt;
    ui:addChild(root)&lt;br /&gt;
    return root&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
-------------------------------------------------------------------------------&lt;br /&gt;
-- Polygon helpers&lt;br /&gt;
-------------------------------------------------------------------------------&lt;br /&gt;
&lt;br /&gt;
local function makePolygon(points)&lt;br /&gt;
    local p = Polygon.new()&lt;br /&gt;
    for _, pt in ipairs(points) do&lt;br /&gt;
        p:addPoint(pt[1], pt[2])&lt;br /&gt;
    end&lt;br /&gt;
    return p&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
local function pointsFromPoly(poly)&lt;br /&gt;
    local pts = poly:getPoints()&lt;br /&gt;
    local out = {}&lt;br /&gt;
    for i = 1, #pts do&lt;br /&gt;
        out[i] = { pts[i][1], pts[i][2] }&lt;br /&gt;
    end&lt;br /&gt;
    return out&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
local function pointsFromPolys(polys)&lt;br /&gt;
    local out = {}&lt;br /&gt;
    for i = 1, #polys do&lt;br /&gt;
        out[i] = pointsFromPoly(polys[i])&lt;br /&gt;
    end&lt;br /&gt;
    return out&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
local function polyArea(points)&lt;br /&gt;
    if #points &amp;lt; 3 then return 0 end&lt;br /&gt;
    local p = makePolygon(points)&lt;br /&gt;
    return p:getArea()&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
local function boundsOf(points)&lt;br /&gt;
    if #points == 0 then return 0, 0, 0, 0 end&lt;br /&gt;
    local minX, minY = points[1][1], points[1][2]&lt;br /&gt;
    local maxX, maxY = minX, minY&lt;br /&gt;
    for i = 2, #points do&lt;br /&gt;
        local x, y = points[i][1], points[i][2]&lt;br /&gt;
        if x &amp;lt; minX then minX = x end&lt;br /&gt;
        if y &amp;lt; minY then minY = y end&lt;br /&gt;
        if x &amp;gt; maxX then maxX = x end&lt;br /&gt;
        if y &amp;gt; maxY then maxY = y end&lt;br /&gt;
    end&lt;br /&gt;
    return minX, minY, maxX - minX, maxY - minY&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
-------------------------------------------------------------------------------&lt;br /&gt;
-- State&lt;br /&gt;
-------------------------------------------------------------------------------&lt;br /&gt;
&lt;br /&gt;
local subjectPoints = { {80, 80}, {230, 90}, {220, 230}, {90, 220} }&lt;br /&gt;
local clipPoints = { {150, 60}, {270, 170}, {160, 290}, {40, 180} }&lt;br /&gt;
&lt;br /&gt;
local active = &amp;quot;subject&amp;quot;          -- &amp;quot;subject&amp;quot; or &amp;quot;clip&amp;quot;&lt;br /&gt;
local currentOp = &amp;quot;intersection&amp;quot;  -- intersection / union / difference / exclusion&lt;br /&gt;
local fillRule = Polygon.FILL_EVEN_ODD&lt;br /&gt;
local showResult = true&lt;br /&gt;
&lt;br /&gt;
local resultPolys = {}   -- list of point-lists&lt;br /&gt;
&lt;br /&gt;
-------------------------------------------------------------------------------&lt;br /&gt;
-- Canvas and shapes&lt;br /&gt;
-------------------------------------------------------------------------------&lt;br /&gt;
&lt;br /&gt;
addPanel(ui, CANVAS_X - 2, CANVAS_Y - 2, CANVAS_W + 4, CANVAS_H + 4, C.line)&lt;br /&gt;
local canvasBg = addPanel(ui, CANVAS_X, CANVAS_Y, CANVAS_W, CANVAS_H, C.card)&lt;br /&gt;
&lt;br /&gt;
local subjShape = Shape.new()&lt;br /&gt;
local clipShape = Shape.new()&lt;br /&gt;
local resShape = Shape.new()&lt;br /&gt;
local boundsShape = Shape.new()&lt;br /&gt;
local lineShape = nil&lt;br /&gt;
&lt;br /&gt;
local canvas = Sprite.new()&lt;br /&gt;
canvas:setPosition(CANVAS_X, CANVAS_Y)&lt;br /&gt;
ui:addChild(canvas)&lt;br /&gt;
canvas:addChild(subjShape)&lt;br /&gt;
canvas:addChild(clipShape)&lt;br /&gt;
canvas:addChild(resShape)&lt;br /&gt;
canvas:addChild(boundsShape)&lt;br /&gt;
&lt;br /&gt;
local function drawPolygon(shape, points, fillColor, fillAlpha, lineColor, lineWidth)&lt;br /&gt;
    shape:clear()&lt;br /&gt;
    if #points &amp;lt; 2 then return end&lt;br /&gt;
    if #points &amp;lt; 3 then&lt;br /&gt;
        shape:setLineStyle(lineWidth or 2, lineColor, 1)&lt;br /&gt;
        shape:beginPath()&lt;br /&gt;
        shape:moveTo(points[1][1], points[1][2])&lt;br /&gt;
        for i = 2, #points do&lt;br /&gt;
            shape:lineTo(points[i][1], points[i][2])&lt;br /&gt;
        end&lt;br /&gt;
        shape:endPath()&lt;br /&gt;
        return&lt;br /&gt;
    end&lt;br /&gt;
    shape:setFillStyle(Shape.SOLID, fillColor, fillAlpha or 0.4)&lt;br /&gt;
    shape:setLineStyle(lineWidth or 2, lineColor, 1)&lt;br /&gt;
    shape:beginPath()&lt;br /&gt;
    shape:moveTo(points[1][1], points[1][2])&lt;br /&gt;
    for i = 2, #points do&lt;br /&gt;
        shape:lineTo(points[i][1], points[i][2])&lt;br /&gt;
    end&lt;br /&gt;
    shape:closePath()&lt;br /&gt;
    shape:endPath()&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
local function drawPolys(shape, polys, fillColor, fillAlpha, lineColor, lineWidth)&lt;br /&gt;
    shape:clear()&lt;br /&gt;
    for _, pts in ipairs(polys) do&lt;br /&gt;
        if #pts &amp;gt;= 3 then&lt;br /&gt;
            shape:setFillStyle(Shape.SOLID, fillColor, fillAlpha or 0.4)&lt;br /&gt;
            shape:setLineStyle(lineWidth or 2, lineColor, 1)&lt;br /&gt;
            shape:beginPath()&lt;br /&gt;
            shape:moveTo(pts[1][1], pts[1][2])&lt;br /&gt;
            for i = 2, #pts do&lt;br /&gt;
                shape:lineTo(pts[i][1], pts[i][2])&lt;br /&gt;
            end&lt;br /&gt;
            shape:closePath()&lt;br /&gt;
            shape:endPath()&lt;br /&gt;
        end&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
local infoLabel = addText(ui, &amp;quot;&amp;quot;, 10, 322, C.dim)&lt;br /&gt;
&lt;br /&gt;
local function rebuild()&lt;br /&gt;
    local subjPts = subjectPoints&lt;br /&gt;
    local clipPts = clipPoints&lt;br /&gt;
&lt;br /&gt;
    drawPolygon(subjShape, subjPts, C.subject, 0.25, C.subject, 2)&lt;br /&gt;
    drawPolygon(clipShape, clipPts, C.clip, 0.20, C.clip, 2)&lt;br /&gt;
&lt;br /&gt;
    -- clear the transient line-offset shape&lt;br /&gt;
    if lineShape then&lt;br /&gt;
        lineShape:removeFromParent()&lt;br /&gt;
        lineShape = nil&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    -- run the boolean operation&lt;br /&gt;
    resultPolys = {}&lt;br /&gt;
    if #subjPts &amp;gt;= 3 and #clipPts &amp;gt;= 3 then&lt;br /&gt;
        local c = Clipper.new()&lt;br /&gt;
        c:addSubjectPolygon(makePolygon(subjPts))&lt;br /&gt;
        c:addClipPolygon(makePolygon(clipPts))&lt;br /&gt;
        local raw&lt;br /&gt;
        if currentOp == &amp;quot;intersection&amp;quot; then&lt;br /&gt;
            raw = c:getIntersection(fillRule, fillRule)&lt;br /&gt;
        elseif currentOp == &amp;quot;union&amp;quot; then&lt;br /&gt;
            raw = c:getUnion(fillRule, fillRule)&lt;br /&gt;
        elseif currentOp == &amp;quot;difference&amp;quot; then&lt;br /&gt;
            raw = c:getDifference(fillRule, fillRule)&lt;br /&gt;
        else&lt;br /&gt;
            raw = c:getExclusion(fillRule, fillRule)&lt;br /&gt;
        end&lt;br /&gt;
        resultPolys = pointsFromPolys(raw)&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    if showResult then&lt;br /&gt;
        drawPolys(resShape, resultPolys, C.result, 0.45, 0x1f7a33, 2)&lt;br /&gt;
    else&lt;br /&gt;
        resShape:clear()&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    -- bounds of the active polygon&lt;br /&gt;
    boundsShape:clear()&lt;br /&gt;
    local activePts = (active == &amp;quot;subject&amp;quot;) and subjPts or clipPts&lt;br /&gt;
    local bx, by, bw, bh = boundsOf(activePts)&lt;br /&gt;
    if bw &amp;gt; 0 and bh &amp;gt; 0 then&lt;br /&gt;
        boundsShape:setLineStyle(2, C.bounds, 1)&lt;br /&gt;
        boundsShape:beginPath()&lt;br /&gt;
        boundsShape:moveTo(bx, by)&lt;br /&gt;
        boundsShape:lineTo(bx + bw, by)&lt;br /&gt;
        boundsShape:lineTo(bx + bw, by + bh)&lt;br /&gt;
        boundsShape:lineTo(bx, by + bh)&lt;br /&gt;
        boundsShape:closePath()&lt;br /&gt;
        boundsShape:endPath()&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    -- info text&lt;br /&gt;
    local subjArea = polyArea(subjPts)&lt;br /&gt;
    local clipArea = polyArea(clipPts)&lt;br /&gt;
    local resArea = 0&lt;br /&gt;
    for _, rp in ipairs(resultPolys) do&lt;br /&gt;
        resArea = resArea + polyArea(rp)&lt;br /&gt;
    end&lt;br /&gt;
    infoLabel:setText(string.format(&lt;br /&gt;
        &amp;quot;subj %d pts  %.0f px2   clip %d pts  %.0f px2   result %d poly  %.0f px2&amp;quot;,&lt;br /&gt;
        #subjPts, subjArea, #clipPts, clipArea, #resultPolys, resArea))&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
-------------------------------------------------------------------------------&lt;br /&gt;
-- Touch: add points to the active polygon&lt;br /&gt;
-------------------------------------------------------------------------------&lt;br /&gt;
&lt;br /&gt;
-- canvas touches are handled by the single dispatcher below&lt;br /&gt;
&lt;br /&gt;
-------------------------------------------------------------------------------&lt;br /&gt;
-- Controls&lt;br /&gt;
-------------------------------------------------------------------------------&lt;br /&gt;
&lt;br /&gt;
-- Header&lt;br /&gt;
addPanel(ui, 16, 12, 4, 24, C.accent)&lt;br /&gt;
addText(ui, &amp;quot;Clipper&amp;quot;, 28, 10, C.text, 2)&lt;br /&gt;
addText(ui, &amp;quot;polygon boolean operations and offsets&amp;quot;, 16, 40, C.dim)&lt;br /&gt;
addPanel(ui, 16, 58, 288, 1, C.line)&lt;br /&gt;
&lt;br /&gt;
local activeBtn&lt;br /&gt;
activeBtn = makeButton(10, 332, 92, 30, &amp;quot;EDIT: SUBJECT&amp;quot;, function()&lt;br /&gt;
    active = (active == &amp;quot;subject&amp;quot;) and &amp;quot;clip&amp;quot; or &amp;quot;subject&amp;quot;&lt;br /&gt;
    activeBtn:setText(&amp;quot;EDIT: &amp;quot; .. active:upper())&lt;br /&gt;
    rebuild()&lt;br /&gt;
end)&lt;br /&gt;
&lt;br /&gt;
local undoBtn = makeButton(108, 332, 62, 30, &amp;quot;UNDO&amp;quot;, function()&lt;br /&gt;
    local pts = (active == &amp;quot;subject&amp;quot;) and subjectPoints or clipPoints&lt;br /&gt;
    if #pts &amp;gt; 0 then pts[#pts] = nil end&lt;br /&gt;
    rebuild()&lt;br /&gt;
end)&lt;br /&gt;
&lt;br /&gt;
local clearBtn = makeButton(176, 332, 62, 30, &amp;quot;CLEAR&amp;quot;, function()&lt;br /&gt;
    if active == &amp;quot;subject&amp;quot; then subjectPoints = {} else clipPoints = {} end&lt;br /&gt;
    rebuild()&lt;br /&gt;
end)&lt;br /&gt;
&lt;br /&gt;
local fillBtn&lt;br /&gt;
fillBtn = makeButton(244, 332, 66, 30, &amp;quot;FILL EO&amp;quot;, function()&lt;br /&gt;
    if fillRule == Polygon.FILL_EVEN_ODD then&lt;br /&gt;
        fillRule = Polygon.FILL_NON_ZERO&lt;br /&gt;
        fillBtn:setText(&amp;quot;FILL NZ&amp;quot;)&lt;br /&gt;
    else&lt;br /&gt;
        fillRule = Polygon.FILL_EVEN_ODD&lt;br /&gt;
        fillBtn:setText(&amp;quot;FILL EO&amp;quot;)&lt;br /&gt;
    end&lt;br /&gt;
    rebuild()&lt;br /&gt;
end)&lt;br /&gt;
&lt;br /&gt;
-- Operation buttons&lt;br /&gt;
local function opButton(label, opName, x)&lt;br /&gt;
    return makeButton(x, 368, 72, 30, label, function()&lt;br /&gt;
        currentOp = opName&lt;br /&gt;
        rebuild()&lt;br /&gt;
    end)&lt;br /&gt;
end&lt;br /&gt;
opButton(&amp;quot;INTERSECT&amp;quot;, &amp;quot;intersection&amp;quot;, 10)&lt;br /&gt;
opButton(&amp;quot;UNION&amp;quot;, &amp;quot;union&amp;quot;, 88)&lt;br /&gt;
opButton(&amp;quot;DIFFERENCE&amp;quot;, &amp;quot;difference&amp;quot;, 166)&lt;br /&gt;
opButton(&amp;quot;XOR&amp;quot;, &amp;quot;exclusion&amp;quot;, 244)&lt;br /&gt;
&lt;br /&gt;
-- Offset / clean / reverse / simplify&lt;br /&gt;
local function offsetActive(delta)&lt;br /&gt;
    local pts = (active == &amp;quot;subject&amp;quot;) and subjectPoints or clipPoints&lt;br /&gt;
    if #pts &amp;lt; 3 then return end&lt;br /&gt;
    local poly = makePolygon(pts)&lt;br /&gt;
    local res = poly:getPolygonOffset(delta, Polygon.JOINT_ROUND, 2, true)&lt;br /&gt;
    if res and #res &amp;gt; 0 then&lt;br /&gt;
        local newPts = pointsFromPoly(res[1])&lt;br /&gt;
        if #newPts &amp;gt;= 3 then&lt;br /&gt;
            if active == &amp;quot;subject&amp;quot; then subjectPoints = newPts&lt;br /&gt;
            else clipPoints = newPts end&lt;br /&gt;
        end&lt;br /&gt;
    end&lt;br /&gt;
    rebuild()&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
makeButton(10, 404, 58, 30, &amp;quot;OFF+&amp;quot;, function() offsetActive(8) end)&lt;br /&gt;
makeButton(74, 404, 58, 30, &amp;quot;OFF-&amp;quot;, function() offsetActive(-8) end)&lt;br /&gt;
&lt;br /&gt;
makeButton(138, 404, 58, 30, &amp;quot;CLEAN&amp;quot;, function()&lt;br /&gt;
    local pts = (active == &amp;quot;subject&amp;quot;) and subjectPoints or clipPoints&lt;br /&gt;
    if #pts &amp;lt; 3 then return end&lt;br /&gt;
    local poly = makePolygon(pts)&lt;br /&gt;
    poly:clean(1.415)&lt;br /&gt;
    local newPts = pointsFromPoly(poly)&lt;br /&gt;
    if active == &amp;quot;subject&amp;quot; then subjectPoints = newPts&lt;br /&gt;
    else clipPoints = newPts end&lt;br /&gt;
    rebuild()&lt;br /&gt;
end)&lt;br /&gt;
&lt;br /&gt;
makeButton(202, 404, 58, 30, &amp;quot;REVERSE&amp;quot;, function()&lt;br /&gt;
    local pts = (active == &amp;quot;subject&amp;quot;) and subjectPoints or clipPoints&lt;br /&gt;
    if #pts &amp;lt; 3 then return end&lt;br /&gt;
    local poly = makePolygon(pts)&lt;br /&gt;
    poly:reverse()&lt;br /&gt;
    local newPts = pointsFromPoly(poly)&lt;br /&gt;
    if active == &amp;quot;subject&amp;quot; then subjectPoints = newPts&lt;br /&gt;
    else clipPoints = newPts end&lt;br /&gt;
    rebuild()&lt;br /&gt;
end)&lt;br /&gt;
&lt;br /&gt;
makeButton(266, 404, 44, 30, &amp;quot;SIMPL&amp;quot;, function()&lt;br /&gt;
    local pts = (active == &amp;quot;subject&amp;quot;) and subjectPoints or clipPoints&lt;br /&gt;
    if #pts &amp;lt; 3 then return end&lt;br /&gt;
    local poly = makePolygon(pts)&lt;br /&gt;
    local res = poly:simplify(fillRule)&lt;br /&gt;
    if res and #res &amp;gt; 0 then&lt;br /&gt;
        local newPts = pointsFromPoly(res[1])&lt;br /&gt;
        if #newPts &amp;gt;= 3 then&lt;br /&gt;
            if active == &amp;quot;subject&amp;quot; then subjectPoints = newPts&lt;br /&gt;
            else clipPoints = newPts end&lt;br /&gt;
        end&lt;br /&gt;
    end&lt;br /&gt;
    rebuild()&lt;br /&gt;
end)&lt;br /&gt;
&lt;br /&gt;
-- Show / hide result&lt;br /&gt;
local showBtn&lt;br /&gt;
showBtn = makeButton(10, 440, 96, 30, &amp;quot;RESULT: ON&amp;quot;, function()&lt;br /&gt;
    showResult = not showResult&lt;br /&gt;
    showBtn:setText(showResult and &amp;quot;RESULT: ON&amp;quot; or &amp;quot;RESULT: OFF&amp;quot;)&lt;br /&gt;
    rebuild()&lt;br /&gt;
end)&lt;br /&gt;
&lt;br /&gt;
-- Line offset demo: offsets the active polygon as a polyline (open shape)&lt;br /&gt;
makeButton(112, 440, 96, 30, &amp;quot;LINE OFF&amp;quot;, function()&lt;br /&gt;
    local pts = (active == &amp;quot;subject&amp;quot;) and subjectPoints or clipPoints&lt;br /&gt;
    if #pts &amp;lt; 2 then return end&lt;br /&gt;
    local poly = makePolygon(pts)&lt;br /&gt;
    local res = poly:getLineOffset(6, Polygon.JOINT_ROUND, Polygon.END_ROUND, 2)&lt;br /&gt;
    if lineShape then lineShape:removeFromParent() end&lt;br /&gt;
    lineShape = Shape.new()&lt;br /&gt;
    lineShape:setLineStyle(2, 0xffb000, 1)&lt;br /&gt;
    for _, lp in ipairs(pointsFromPolys(res)) do&lt;br /&gt;
        if #lp &amp;gt;= 2 then&lt;br /&gt;
            lineShape:beginPath()&lt;br /&gt;
            lineShape:moveTo(lp[1][1], lp[1][2])&lt;br /&gt;
            for i = 2, #lp do&lt;br /&gt;
                lineShape:lineTo(lp[i][1], lp[i][2])&lt;br /&gt;
            end&lt;br /&gt;
            lineShape:endPath()&lt;br /&gt;
        end&lt;br /&gt;
    end&lt;br /&gt;
    canvas:addChild(lineShape)&lt;br /&gt;
end)&lt;br /&gt;
&lt;br /&gt;
local legend = addText(ui, &amp;quot;blue = subject   red = clip   green = result   yellow = bounds&amp;quot;, 10, 474, C.dim)&lt;br /&gt;
legend:setScale(0.85)&lt;br /&gt;
&lt;br /&gt;
-- Single touch dispatcher: avoids Gideros delivering one touch to several sprites.&lt;br /&gt;
local pressedButton = nil&lt;br /&gt;
&lt;br /&gt;
local function findButtonAt(px, py)&lt;br /&gt;
    for i = #buttonRegistry, 1, -1 do&lt;br /&gt;
        local b = buttonRegistry[i]&lt;br /&gt;
        if px &amp;gt;= b.x and px &amp;lt;= b.x + b.w and py &amp;gt;= b.y and py &amp;lt;= b.y + b.h then&lt;br /&gt;
            return b&lt;br /&gt;
        end&lt;br /&gt;
    end&lt;br /&gt;
    return nil&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
ui:addEventListener(Event.TOUCHES_BEGIN, function(e)&lt;br /&gt;
    local ux, uy = ui:globalToLocal(e.touch.x, e.touch.y)&lt;br /&gt;
    local b = findButtonAt(ux, uy)&lt;br /&gt;
    if b then&lt;br /&gt;
        pressedButton = b&lt;br /&gt;
        b.refresh(true)&lt;br /&gt;
    else&lt;br /&gt;
        pressedButton = nil&lt;br /&gt;
        -- canvas tap: add a point to the active polygon&lt;br /&gt;
        local cx, cy = canvas:globalToLocal(e.touch.x, e.touch.y)&lt;br /&gt;
        if cx &amp;gt;= 0 and cy &amp;gt;= 0 and cx &amp;lt;= CANVAS_W and cy &amp;lt;= CANVAS_H then&lt;br /&gt;
            local pts = (active == &amp;quot;subject&amp;quot;) and subjectPoints or clipPoints&lt;br /&gt;
            pts[#pts + 1] = { math.floor(cx), math.floor(cy) }&lt;br /&gt;
            rebuild()&lt;br /&gt;
        end&lt;br /&gt;
    end&lt;br /&gt;
    return true&lt;br /&gt;
end)&lt;br /&gt;
&lt;br /&gt;
ui:addEventListener(Event.TOUCHES_END, function(e)&lt;br /&gt;
    if pressedButton then&lt;br /&gt;
        pressedButton.refresh(false)&lt;br /&gt;
        if pressedButton.onClick then pressedButton.onClick() end&lt;br /&gt;
        pressedButton = nil&lt;br /&gt;
    end&lt;br /&gt;
    return true&lt;br /&gt;
end)&lt;br /&gt;
&lt;br /&gt;
ui:addEventListener(Event.TOUCHES_CANCEL, function(e)&lt;br /&gt;
    if pressedButton then&lt;br /&gt;
        pressedButton.refresh(false)&lt;br /&gt;
        pressedButton = nil&lt;br /&gt;
    end&lt;br /&gt;
    return true&lt;br /&gt;
end)&lt;br /&gt;
&lt;br /&gt;
rebuild()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{Clipper}}&lt;/div&gt;</summary>
		<author><name>MoKaLux</name></author>
	</entry>
</feed>