Anonymous user
Ray-casting algorithm: Difference between revisions
m
→{{header|Tcl}}: formatting
m (note: it's one of the possible ways; not The Way.) |
m (→{{header|Tcl}}: formatting) |
||
Line 205:
set count 0
foreach side [sides $polygon] {
incr count
}
}
expr {$count % 2} ;#-- 1 = odd = true, 0 = even = false
Line 212 ⟶ 214:
foreach {x0 y0} $polygon break
foreach {x y} [lrange [lappend polygon $x0 $y0] 2 end] {
}
return $res
Line 227 ⟶ 229:
foreach {xa ya xb yb} $line1 break
foreach {xc yc xd yd} $line2 break
set det [expr {($xb - $xa) * ($yd - $yc) - ($xd - $xc) * ($yb - $ya)}]
if {$det == 0} {return 0} ;#-- parallel or collinear
set nump [expr {($xd - $xc) * ($yb + $ya) - ($xb + $xa) * ($yd - $yc) + 2. * ($xc * $yd - $xd * $yc)}]
if {abs($nump) > abs($det)} {return 0} ;#-- cross outside line1
set numq [expr {($xd + $xc) * ($yb - $ya) - ($xb - $xa) * ($yd + $yc) + 2. * ($xb * $ya - $xa * $yb)}]
if {abs($numq) > abs($det)} {return 0} ;#-- cross outside line2
return 1
}
foreach {point poly} {
{0 0} {-1 -1 -1 1 1 1 1 -1}
{2 2} {-1 -1 -1 1 1 1 1 -1}
{0 0} {-2 -2 -2 2 2 2 2 -2 2 -1 1 1 -1 1 -1 -1 1 -1 2 -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}
} {
puts "$point in $poly = [point_in_polygon $point $poly]"
}</lang>▼
▲</lang>
|