Anonymous user
Ray-casting algorithm: Difference between revisions
→{{header|Tcl}}: Use the better algorithm from the top of the page to fix the error
(fortran) |
(→{{header|Tcl}}: Use the better algorithm from the top of the page to fix the error) |
||
Line 366:
=={{header|Tcl}}==
<lang Tcl>package require Tcl 8.5
proc point_in_polygon {point polygon} {
set count 0
foreach side [sides $polygon] {
if {[ray_intersects_line $point $side]} {
incr count
}
Line 379 ⟶ 378:
}
proc sides polygon {
foreach {x y} [lrange [lappend polygon $x0 $y0] 2 end] {
lappend res [list $x0 $y0 $x $y]
Line 388 ⟶ 387:
}
proc ray_intersects_line {point line} {
# Reverse line direction if necessary
lassign $line Bx By Ax Ay
}
# Add epsilon to
set Py [expr {$Py + abs($Py)/1e6}]
}
set det [expr {($xb - $xa) * ($yd - $yc) - ($xd - $xc) * ($yb - $ya)}]▼
# Bounding box checks
if {$Py < $Ay || $Py > $By || $Px > max($Ax,$Bx)} {
} elseif {$Px < min($Ax,$Bx)} {
return 1
}
# Compare dot products to compare (cosines of) angles
set mBlu [expr {$Ax != $Px ? ($Py-$Ay)/($Px-$Ax) : Inf}]
▲ return 1
return [expr {$mBlu >= $mRed}]
}
foreach {point poly} {
{0 0} {-1 -1 -1 1 1 1 1 -1}
Line 420:
{10 10} {0 0 2.5 2.5 0 10 2.5 7.5 7.5 7.5 10 10 10 0 7.5 0.1}
{2.5 2.5} {0 0 2.5 2.5 0 10 2.5 7.5 7.5 7.5 10 10 10 0 7.5 0.1}
{-5 5} {3 0 7 0 10 5 7 10 3 10 0 5 }
} {
puts "$point in $poly = [point_in_polygon $point $poly]"
|