Ray-casting algorithm: Difference between revisions
Content added Content deleted
(Kotlin: some improvements) |
m (→{{header|FreeBASIC}}: indentation) |
||
Line 1,078: | Line 1,078: | ||
=={{header|FreeBASIC}}== |
=={{header|FreeBASIC}}== |
||
Inpolygon by Winding number method |
Inpolygon by Winding number method |
||
<lang FreeBASIC> |
<lang FreeBASIC>Type Point |
||
As Single x,y |
|||
End Type |
End Type |
||
Function inpolygon(p1() As Point,p2 As Point) As Integer |
Function inpolygon(p1() As Point,p2 As Point) As Integer |
||
#Macro isleft2(L,p) |
|||
-Sgn( (L(1).x-L(2).x)*(p.y-L(2).y) - (p.x-L(2).x)*(L(1).y-L(2).y)) |
-Sgn( (L(1).x-L(2).x)*(p.y-L(2).y) - (p.x-L(2).x)*(L(1).y-L(2).y)) |
||
#EndMacro |
|||
Dim As Integer index,nextindex |
|||
Dim k As Integer=UBound(p1)+1 |
|||
Dim send (1 To 2) As Point |
|||
Dim wn As Integer=0 |
|||
For n As Integer=1 To UBound(p1) |
|||
index=n Mod k:nextindex=(n+1) Mod k |
|||
If nextindex=0 Then nextindex=1 |
|||
send(1).x=p1(index).x:send(2).x=p1(nextindex).x |
|||
send(1).y=p1(index).y:send(2).y=p1(nextindex).y |
|||
If p1(index).y<=p2.y Then |
|||
If p1(nextindex).y>p2.y Then |
|||
If isleft2(send,p2)>=0 Then '= |
|||
wn=wn+1 |
|||
End If |
|||
End If |
|||
Else |
|||
If p1(nextindex).y<=p2.y Then |
|||
If isleft2(send,p2)<=0 Then'= |
|||
wn=wn-1 |
|||
End If |
|||
End If |
|||
End If |
End If |
||
End If |
|||
End If |
|||
Next n |
|||
Return wn |
|||
End Function |
End Function |
||
Line 1,122: | Line 1,121: | ||
Dim As Point exagon(1 To 6) ={(3,0),(7,0),(10,5),(7,10),(3,10),(0,5)} |
Dim As Point exagon(1 To 6) ={(3,0),(7,0),(10,5),(7,10),(3,10),(0,5)} |
||
'printouts |
'printouts |
||
For z As Integer=1 To 4 |
For z As Integer=1 To 4 |
||
Select Case z |
|||
Case 1: Print "squared" |
Case 1: Print "squared" |
||
Print "(5,5) " ;Tab(12); |
Print "(5,5) " ;Tab(12); |
||
If inpolygon(square(),Type<Point>(5,5)) Then Print "in" Else Print "out" |
If inpolygon(square(),Type<Point>(5,5)) Then Print "in" Else Print "out" |
||
Print "(5,8) " ;Tab(12); |
Print "(5,8) " ;Tab(12); |
||
If inpolygon(square(),Type<Point>(5,8)) Then Print "in" Else Print "out" |
If inpolygon(square(),Type<Point>(5,8)) Then Print "in" Else Print "out" |
||
Print "(-10,5) " ;Tab(12); |
Print "(-10,5) " ;Tab(12); |
||
If inpolygon(square(),Type<Point>(-10,5)) Then Print "in" Else Print "out" |
If inpolygon(square(),Type<Point>(-10,5)) Then Print "in" Else Print "out" |
||
Print "(0,5) " ;Tab(12); |
Print "(0,5) " ;Tab(12); |
||
If inpolygon(square(),Type<Point>(0,5)) Then Print "in" Else Print "out" |
If inpolygon(square(),Type<Point>(0,5)) Then Print "in" Else Print "out" |
||
Print "(10,5) " ;Tab(12); |
Print "(10,5) " ;Tab(12); |
||
If inpolygon(square(),Type<Point>(10,5)) Then Print "in" Else Print "out" |
If inpolygon(square(),Type<Point>(10,5)) Then Print "in" Else Print "out" |
||
Print "(8,5) " ;Tab(12); |
Print "(8,5) " ;Tab(12); |
||
If inpolygon(square(),Type<Point>(8,5)) Then Print "in" Else Print "out" |
If inpolygon(square(),Type<Point>(8,5)) Then Print "in" Else Print "out" |
||
Print "(10,10) " ;Tab(12); |
Print "(10,10) " ;Tab(12); |
||
If inpolygon(square(),Type<Point>(10,10)) Then Print "in" Else Print "out" |
If inpolygon(square(),Type<Point>(10,10)) Then Print "in" Else Print "out" |
||
Print |
Print |
||
Case 2:Print "squared hole" |
Case 2:Print "squared hole" |
||
Print "(5,5) " ;Tab(12); |
Print "(5,5) " ;Tab(12); |
||
If Not inpolygon(hole(),Type<Point>(5,5)) And inpolygon(square(),Type<Point>(5,5)) Then Print "in" Else Print "out" |
If Not inpolygon(hole(),Type<Point>(5,5)) And inpolygon(square(),Type<Point>(5,5)) Then Print "in" Else Print "out" |
||
Print "(5,8) " ;Tab(12); |
Print "(5,8) " ;Tab(12); |
||
If Not inpolygon(hole(),Type<Point>(5,8)) And inpolygon(square(),Type<Point>(5,8))Then Print "in" Else Print "out" |
If Not inpolygon(hole(),Type<Point>(5,8)) And inpolygon(square(),Type<Point>(5,8))Then Print "in" Else Print "out" |
||
Print "(-10,5) " ;Tab(12); |
Print "(-10,5) " ;Tab(12); |
||
If Not inpolygon(hole(),Type<Point>(-10,5))And inpolygon(square(),Type<Point>(-10,5)) Then Print "in" Else Print "out" |
If Not inpolygon(hole(),Type<Point>(-10,5))And inpolygon(square(),Type<Point>(-10,5)) Then Print "in" Else Print "out" |
||
Print "(0,5) " ;Tab(12); |
Print "(0,5) " ;Tab(12); |
||
If Not inpolygon(hole(),Type<Point>(0,5))And inpolygon(square(),Type<Point>(0,5)) Then Print "in" Else Print "out" |
If Not inpolygon(hole(),Type<Point>(0,5))And inpolygon(square(),Type<Point>(0,5)) Then Print "in" Else Print "out" |
||
Print "(10,5) " ;Tab(12); |
Print "(10,5) " ;Tab(12); |
||
If Not inpolygon(hole(),Type<Point>(10,5))And inpolygon(square(),Type<Point>(10,5)) Then Print "in" Else Print "out" |
If Not inpolygon(hole(),Type<Point>(10,5))And inpolygon(square(),Type<Point>(10,5)) Then Print "in" Else Print "out" |
||
Print "(8,5) " ;Tab(12); |
Print "(8,5) " ;Tab(12); |
||
If Not inpolygon(hole(),Type<Point>(8,5))And inpolygon(square(),Type<Point>(8,5)) Then Print "in" Else Print "out" |
If Not inpolygon(hole(),Type<Point>(8,5))And inpolygon(square(),Type<Point>(8,5)) Then Print "in" Else Print "out" |
||
Print "(10,10) " ;Tab(12); |
Print "(10,10) " ;Tab(12); |
||
If Not inpolygon(hole(),Type<Point>(10,10))And inpolygon(square(),Type<Point>(10,10)) Then Print "in" Else Print "out" |
If Not inpolygon(hole(),Type<Point>(10,10))And inpolygon(square(),Type<Point>(10,10)) Then Print "in" Else Print "out" |
||
Print |
Print |
||
Case 3:Print "strange" |
Case 3:Print "strange" |
||
Print "(5,5) " ;Tab(12); |
Print "(5,5) " ;Tab(12); |
||
If inpolygon(strange(),Type<Point>(5,5)) Then Print "in" Else Print "out" |
If inpolygon(strange(),Type<Point>(5,5)) Then Print "in" Else Print "out" |
||
Print "(5,8) " ;Tab(12); |
Print "(5,8) " ;Tab(12); |
||
If inpolygon(strange(),Type<Point>(5,8)) Then Print "in" Else Print "out" |
If inpolygon(strange(),Type<Point>(5,8)) Then Print "in" Else Print "out" |
||
Print "(-10,5) " ;Tab(12); |
Print "(-10,5) " ;Tab(12); |
||
If inpolygon(strange(),Type<Point>(-10,5)) Then Print "in" Else Print "out" |
If inpolygon(strange(),Type<Point>(-10,5)) Then Print "in" Else Print "out" |
||
Print "(0,5) " ;Tab(12); |
Print "(0,5) " ;Tab(12); |
||
If inpolygon(strange(),Type<Point>(0,5)) Then Print "in" Else Print "out" |
If inpolygon(strange(),Type<Point>(0,5)) Then Print "in" Else Print "out" |
||
Print "(10,5) " ;Tab(12); |
Print "(10,5) " ;Tab(12); |
||
If inpolygon(strange(),Type<Point>(10,5)) Then Print "in" Else Print "out" |
If inpolygon(strange(),Type<Point>(10,5)) Then Print "in" Else Print "out" |
||
Print "(8,5) " ;Tab(12); |
Print "(8,5) " ;Tab(12); |
||
If inpolygon(strange(),Type<Point>(8,5)) Then Print "in" Else Print "out" |
If inpolygon(strange(),Type<Point>(8,5)) Then Print "in" Else Print "out" |
||
Print "(10,10) " ;Tab(12); |
Print "(10,10) " ;Tab(12); |
||
If inpolygon(strange(),Type<Point>(10,10)) Then Print "in" Else Print "out" |
If inpolygon(strange(),Type<Point>(10,10)) Then Print "in" Else Print "out" |
||
Print |
Print |
||
Case 4:Print "exagon" |
Case 4:Print "exagon" |
||
Print "(5,5) " ;Tab(12); |
Print "(5,5) " ;Tab(12); |
||
If inpolygon(exagon(),Type<Point>(5,5)) Then Print "in" Else Print "out" |
If inpolygon(exagon(),Type<Point>(5,5)) Then Print "in" Else Print "out" |
||
Print "(5,8) " ;Tab(12); |
Print "(5,8) " ;Tab(12); |
||
If inpolygon(exagon(),Type<Point>(5,8)) Then Print "in" Else Print "out" |
If inpolygon(exagon(),Type<Point>(5,8)) Then Print "in" Else Print "out" |
||
Print "(-10,5) " ;Tab(12); |
Print "(-10,5) " ;Tab(12); |
||
If inpolygon(exagon(),Type<Point>(-10,5)) Then Print "in" Else Print "out" |
If inpolygon(exagon(),Type<Point>(-10,5)) Then Print "in" Else Print "out" |
||
Print "(0,5) " ;Tab(12); |
Print "(0,5) " ;Tab(12); |
||
If inpolygon(exagon(),Type<Point>(0,5)) Then Print "in" Else Print "out" |
If inpolygon(exagon(),Type<Point>(0,5)) Then Print "in" Else Print "out" |
||
Print "(10,5) " ;Tab(12); |
Print "(10,5) " ;Tab(12); |
||
If inpolygon(exagon(),Type<Point>(10,5)) Then Print "in" Else Print "out" |
If inpolygon(exagon(),Type<Point>(10,5)) Then Print "in" Else Print "out" |
||
Print "(8,5) " ;Tab(12); |
Print "(8,5) " ;Tab(12); |
||
If inpolygon(exagon(),Type<Point>(8,5)) Then Print "in" Else Print "out" |
If inpolygon(exagon(),Type<Point>(8,5)) Then Print "in" Else Print "out" |
||
Print "(10,10) " ;Tab(12); |
Print "(10,10) " ;Tab(12); |
||
If inpolygon(exagon(),Type<Point>(10,10)) Then Print "in" Else Print "out" |
If inpolygon(exagon(),Type<Point>(10,10)) Then Print "in" Else Print "out" |
||
Print |
Print |
||
End Select |
End Select |
||
Next z |
Next z |
||
⚫ | |||
sleep |
|||
⚫ | |||
Output: |
Output: |
||
<pre> |
<pre>squared |
||
squared |
|||
(5,5) in |
(5,5) in |
||
(5,8) in |
(5,8) in |
||
Line 1,229: | Line 1,227: | ||
(10,5) in |
(10,5) in |
||
(8,5) in |
(8,5) in |
||
(10,10) out |
(10,10) out</pre> |
||
</pre> |
|||
=={{header|Go}}== |
=={{header|Go}}== |