Ray-casting algorithm: Difference between revisions
Content added Content deleted
m (added whitespace to the task's preamble, enlarged a Greek glyph.) |
m (→{{header|REXX}}: changed/added comments and whitespace, changed indentations.) |
||
Line 2,859: | Line 2,859: | ||
exit /*stick a fork in it, we're all done. */ |
exit /*stick a fork in it, we're all done. */ |
||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
||
in$out: procedure expose point. poly.; |
in$out: procedure expose point. poly.; parse arg p; #=0 |
||
do side=1 to poly.0 by 2; #=#+intersect(p,side); end /*side*/ |
do side=1 to poly.0 by 2; #=#+intersect(p,side); end /*side*/ |
||
return #// |
return # // 2 /*ODD is inside. EVEN is outside.*/ |
||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
||
intersect: procedure expose point. poly.; parse arg ?,s; sp=s+1 |
intersect: procedure expose point. poly.; parse arg ?,s; sp=s+1 |
||
epsilon='1e' || (-digits()%2); infinity="1e" || (digits() *2) |
epsilon='1e' || (-digits()%2); infinity="1e" || (digits() *2) |
||
Px=point.?.x; Ax=poly.s.x; Ay=poly.s.y |
Px=point.?.x; Ax=poly.s.x; Ay=poly.s.y |
||
Py=point.?.y; Bx=poly.sp.x; By=poly.sp.y /* [↓] do a swap.*/ |
Py=point.?.y; Bx=poly.sp.x; By=poly.sp.y /* [↓] do a swap.*/ |
||
if Ay>By then parse value Ax Ay Bx By with Bx By Ax Ay |
if Ay>By then parse value Ax Ay Bx By with Bx By Ax Ay |
||
if Py=Ay | Py=By then Py=Py+epsilon |
if Py=Ay | Py=By then Py=Py + epsilon |
||
if Py<Ay | Py>By | Px>max(Ax,Bx) then return 0 |
if Py<Ay | Py>By | Px>max(Ax,Bx) then return 0 |
||
if Px<min(Ax,Bx) then return 1 |
if Px<min(Ax,Bx) then return 1 |
||
Line 2,875: | Line 2,875: | ||
if Ax\=Px then m_blue=(Py-Ay) / (Px-Ax) |
if Ax\=Px then m_blue=(Py-Ay) / (Px-Ax) |
||
else return 1 |
else return 1 |
||
return m_blue>=m_red |
return m_blue >= m_red |
||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
||
points: wx=0; wy=0; do j=1 for arg(); parse value arg(j) with xx yy |
|||
points: wx=0 |
|||
wx=max(wx, length(xx) ); call value 'POINT.'j".X", xx |
|||
wy=max(wy, length(yy) ); call value 'POINT.'j".Y", yy |
|||
end /*j*/ |
|||
call value point.0, j-1 /*define the number of points. */ |
|||
call value POINT.0, j-1 /*define the number of points. */ |
|||
return |
return |
||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
||
poly: @='POLY.'; parse arg Fx Fy |
poly: @='POLY.'; parse arg Fx Fy /* [↓] process the X,Y points.*/ |
||
n=0 |
n=0 |
||
do j=1 for arg(); n=n+1; |
do j=1 for arg(); n=n+1; parse value arg(j) with xx yy |
||
call value @ || n'.X', xx ; call value @ || n".Y", yy |
call value @ || n'.X', xx ; call value @ || n".Y", yy |
||
if n//2 then iterate |
if n//2 then iterate; n=n+1 |
||
n |
call value @ || n'.X', xx ; call value @ || n".Y", yy |
||
end /*j*/ |
|||
end /*j*/ |
|||
n=n+1 |
n=n+1 |
||
call value @ || n'.X', Fx; call value @ || n".Y", Fy; call value @'0',n |
call value @ || n'.X', Fx; call value @ || n".Y", Fy; call value @'0',n |
||
return /*POLY.0 |
return /*POLY.0 is # segments(sides).*/ |
||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
||
test: say; do k=1 for point.0; |
test: say; do k=1 for point.0; w=wx+wy+2 |
||
say right(' ['arg(1)"] point:", 30), |
say right(' ['arg(1)"] point:", 30), |
||
right( right(point.k.x, wx)', 'right(point.k.y, wy), |
right( right(point.k.x, wx)', 'right(point.k.y, wy), w) " is ", |
||
right( word('outside inside', in$out(k)+1), 7) |
right( word('outside inside', in$out(k)+1), 7) |
||
end /*k*/ |
end /*k*/ |
||
return</lang> |
return</lang> |
||
'''output''' |
'''output''' |
||
<pre> |
<pre> |