Sutherland-Hodgman polygon clipping: Difference between revisions

no edit summary
No edit summary
Line 1,484:
300.00000000000000 116.66666666666666
300.00000000000000 300.00000000000000
 
=={{header|freebasic}}==
freebasic has inbuilt gfx graphics (a main feature), but I have no access to graphics uploads.
So no extra credits.
<lang freebasic>
 
Type Point
As Double x,y
End Type
 
Type Line
As Point s,f'start/finish
End Type
 
Function isleft(L As Line,p As Point) As Long
Return -Sgn((L.s.x-L.f.x)*(p.y-L.f.y)-(p.x-L.f.x)*(L.s.y-L.f.y))
End Function
 
Function segmentintersections(L1 As Line,L2 As Line) As Long
If isleft(L2,L1.s) = isleft(L2,L1.f) Then Return 0
If isleft(L1,L2.s) = isleft(L1,L2.f) Then Return 0
Return 1
End Function
 
Function allintersections(l1 As Line,l2 As Line,_out As Point) As Long
Const tolerance=.01
Var p1=l1.s, p2=l1.f, p3=l2.s, p4=l2.f
Var x12=p1.x-p2.x, x34=p3.x-p4.x, y12=p1.y-p2.y, y34=p3.y-p4.y
Var c=x12*y34-y12*x34
If Abs(c)<tolerance Then Return 0
Var a=p1.x*p2.y-p1.y*p2.x, b=p3.x*p4.y-p3.y*p4.x
_out.x = (a*x34-b*x12)/c
_out.y = (a*y34-b*y12)/c
Return 1
End Function
 
 
Dim As Point p1(...)={(50,150),(200,50),(350,150),(350,300),(250,300),(200,250), _
(150,350),(100,250),(100,200)}
Dim As Point p2(...)={(100,100),(300,100),(300,300),(100,300)}
'get the line segments around the polygons
Dim As Line L1(...)={(p1(0),p1(1)),(p1(1),p1(2)),(p1(2),p1(3)),(p1(3),p1(4)),(p1(4),p1(5)),_
(p1(5),p1(6)),(p1(6),p1(7)),(p1(7),p1(8)),(p1(8),p1(0))}
 
Dim As Line L2(...)={(p2(0),p2(1)),(p2(1),p2(2)),(p2(2),p2(3)),(p2(3),p2(0))}
 
'would normally draw these lines now, but not here.
Dim As Point x
For n1 As Long=Lbound(L1) To Ubound(L1)
For n2 As Long=Lbound(L2) To Ubound(L2)
If allintersections(L1(n1),L2(n2),x) And segmentintersections(L1(n1),L2(n2)) Then
Print x.x,x.y
End If
Next
Next
 
Sleep
</lang>
{{out}}
<pre>
125 100
100 116.6666666666667
275 100
300 116.6666666666667
300 300
250 300
175 300
125 300
100 250
100 200
</pre>
 
 
=={{header|Go}}==
Anonymous user