Sutherland-Hodgman polygon clipping: Difference between revisions

Added 11l
No edit summary
(Added 11l)
Line 20:
(When displaying you may use either a north-west or a south-west origin, whichever is more convenient for your display mechanism.)
<br><br>
 
=={{header|11l}}==
{{trans|Python}}
 
<lang 11l>F clip(subjectPolygon, clipPolygon)
F inside(p, cp1, cp2)
R (cp2.x - cp1.x) * (p.y - cp1.y) > (cp2.y - cp1.y) * (p.x - cp1.x)
 
F computeIntersection(s, e, cp1, cp2)
V dc = cp1 - cp2
V dp = s - e
V n1 = cp1.x * cp2.y - cp1.y * cp2.x
V n2 = s.x * e.y - s.y * e.x
V n3 = 1.0 / (dc.x * dp.y - dc.y * dp.x)
R ((n1 * dp.x - n2 * dc.x) * n3, (n1 * dp.y - n2 * dc.y) * n3)
 
V outputList = subjectPolygon
V cp1 = clipPolygon.last
 
L(clipVertex) clipPolygon
V cp2 = clipVertex
V inputList = outputList
outputList.clear()
V s = inputList.last
 
L(subjectVertex) inputList
V e = subjectVertex
I inside(e, cp1, cp2)
I !inside(s, cp1, cp2)
outputList.append(computeIntersection(s, e, cp1, cp2))
outputList.append(e)
E I inside(s, cp1, cp2)
outputList.append(computeIntersection(s, e, cp1, cp2))
s = e
cp1 = cp2
R (outputList)
 
V subjectp = [(50.0, 150.0), (200.0, 50.0), (350.0, 150.0), (350.0, 300.0), (250.0, 300.0), (200.0, 250.0), (150.0, 350.0), (100.0, 250.0), (100.0, 200.0)]
V clipp = [(100.0, 100.0), (300.0, 100.0), (300.0, 300.0), (100.0, 300.0)]
print_elements(clip(subjectp, clipp), sep' "\n")</lang>
 
{{out}}
<pre>
(100, 116.667)
(125, 100)
(275, 100)
(300, 116.667)
(300, 300)
(250, 300)
(200, 250)
(175, 300)
(125, 300)
(100, 250)
</pre>
 
=={{header|Ada}}==
1,463

edits