Posted to tcl by schmitzu at Mon Apr 17 14:02:39 GMT 2023view pretty
# get direction of line segment given
# by start-/endpoint as unit vector [xu:yu]
proc pp2uv {x1 y1 x2 y2} {
set dx [expr {$x2 - $x1}]
set dy [expr {$y2 - $y1}]
set ab [expr {sqrt ($dx * $dx + $dy * $dy)}]
set xu [expr {$dx / $ab}]
set yu [expr {$dy / $ab}]
return [list $xu $yu]
}
# get perpedicular line on linesegment ls at
# position t with lenth l
# ls line segment {x1 y1 x2 y2}
# t positon on ls [0..1]
# l length of result segment
# returns:
# {px1 py1 px2 py2} start-/endpoint perpendicular linesegment
proc getPpline {ls t l} {
lassign $ls x1 y1 x2 y2
# calculate point at t
set it [expr {1-$t}]
set xp [expr {$x1 * $t + $x2 * $it}]
set yp [expr {$y1 * $t + $y2 * $it}]
# get unit vector of ls
lassign [pp2uv $x1 $y1 $x2 $y2] xu yu
set l2 [expr {$l / 2.}]
# generate start point of perpedicular line
set px1 [expr {$xp - $yu * $l2}]
set py1 [expr {$yp + $xu * $l2}]
# generate end point of perpedicular line
set px2 [expr {$xp + $yu * $l2}]
set py2 [expr {$yp - $xu * $l2}]
return [list $px1 $py1 $px2 $py2]
}
set linesegment {1. 1. 9. 9.}
set ppl [getPpline $linesegment 0.5 2.0]
puts $ppl