Ray-casting algorithm: Difference between revisions

no edit summary
No edit summary
Line 1,114:
0 1 1 0 0 1 1 1 0 1 0
1 0 0 0 0 0 0 1 0 1 0</lang>
 
=={{header|Liberty BASIC}}==
'Translated from C code at: http://alienryderflex.com/polygon/
<br>
Displays interactively on-screen.
<lang lb>
NoMainWin
Global sw, sh, verts
 
sw = 640 : sh = 480
WindowWidth = sw+8 : WindowHeight = sh+31
UpperLeftX = (DisplayWidth -sw)/2
UpperLeftY = (DisplayHeight-sh)/2
Open"Ray Casting Algorithm" For Graphics_nf_nsb As #g
#g "Down; TrapClose [halt]"
h$ = "#g"
 
Dim xp(15),yp(15)
#g "when leftButtonDown [halt];when mouseMove checkPoint"
#g "when rightButtonDown [Repeat]"
 
[Repeat]
#g "Cls;Fill 32 160 255; Color white;BackColor 32 160 255"
#g "Place 5 460;\L-click to exit"
#g "Place 485 460;\R-click for new polygon"
 
'generate polygon from random points
numPoints = rand(4,15)
verts = numPoints
For i = 0 To numPoints-1
xp(i) = rand(20,620)
yp(i) = rand(40,420)
Next
Call drawPoly h$, verts, "white"
#g "Flush"
Wait
 
[halt]
Close #g
End
 
'Point In Polygon Function
Function pnp(x, y, numSides)
j= numSides-1: oddNodes = 0
For i = 0 To numSides-1
If ((yp(i)<y) And (yp(j)>=y)) Or ((yp(j)<y) And (yp(i)>=y)) Then
f1 = y - yp(i):f2 = yp(j) - yp(i): f3 = xp(j) - xp(i)
If (xp(i) + f1 / f2 * f3) < x Then oddNodes = 1 - oddNodes
End If
j = i
Next
pnp = oddNodes
End Function
 
'draw the polygon
Sub drawPoly h$, verts, colour$
#h$, "Color ";colour$
j = verts-1
For i = 0 To verts-1
#h$ "Line ";xp(j);" ";yp(j);" ";xp(i);" ";yp(i)
j = i
Next
End Sub
 
'change message and color of polygon
Sub checkPoint h$, x, y
If pnp(x,y,verts) Then
#h$ "Color 32 160 255;BackColor 32 160 255"
#h$ "Place 5 0;BoxFilled 150 20;Color white"
#h$ "Place 7 15;\Mouse In Polygon"
Call drawPoly h$, verts, "red"
Else
#h$ "Color 32 160 255;BackColor 32 160 255"
#h$ "Place 5 0;BoxFilled 150 20;Color white"
#h$ "Place 7 15;\Mouse Not In Polygon"
Call drawPoly h$, verts, "white"
End If
End Sub
 
Function rand(loNum,hiNum)
rand = Int(Rnd(0)*(hiNum-loNum+1)+loNum)
End Function
</lang>
 
=={{header|OCaml}}==
Anonymous user