Skip to content

Instantly share code, notes, and snippets.

@Willem3141
Created February 17, 2025 18:22
Show Gist options
  • Save Willem3141/a4054c5a11ee7f93f3e7a9832f481ec2 to your computer and use it in GitHub Desktop.
Save Willem3141/a4054c5a11ee7f93f3e7a9832f481ec2 to your computer and use it in GitHub Desktop.
Ipelet that measures the area of a polygon
label = "Measure"
about = [[
Measures areas of polygons
]]
function measure(model, num)
local p = model.doc[model.pno]
if p:primarySelection() == nil then
model.ui:explain('No primary selection')
return
end
local obj = p[p:primarySelection()]
if obj:type() ~= 'path' then
model.ui:explain('Selection is not a path object')
return
end
shape = obj:shape()
if #shape ~= 1 then
model.ui:explain('Selection contains more than one subpath')
return
end
curve = shape[1]
if curve.type ~= 'curve' then
model.ui:explain('Selection contains something which is not a line segment')
return
end
local area = 0
for i, segment in ipairs(curve) do
if segment.type ~= 'segment' then
model.ui:explain('Selection contains something which is not a line segment')
return
end
startPoint = segment[1]
endPoint = segment[2]
x1 = startPoint.x
y1 = startPoint.y
x2 = endPoint.x
y2 = endPoint.y
print('segment ' .. x1 .. ' ' .. y1 .. ' ' .. x2 .. ' ' .. y2)
area = area + x1 * y2 - x2 * y1
print('area now ' .. area)
end
-- still need to add the last segment
startPoint = curve[#curve][2]
endPoint = curve[1][1]
x1 = startPoint.x
y1 = startPoint.y
x2 = endPoint.x
y2 = endPoint.y
print('last segment ' .. x1 .. ' ' .. y1 .. ' ' .. x2 .. ' ' .. y2)
area = area + x1 * y2 - x2 * y1
print('final area ' .. area)
model.ui:explain('Area = ' .. (0.5 * area))
end
----------------------------------------------------------------------
methods = {
{ label = "Measure", run=measure }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment