#
#
#
# Line Normalization
#
#


#
#### PUT LINE BREAK DETECT INTO linNORM
## TO ALLOW OBJECT MOVE TO MOVE ATTACTED LINES 
## AND NOT HAVE TO DO ANY BREAKING.
## MAY ALSO BE OF USE FOR SEGMOVE

# start  = number of first coord (ie. 0 - end , or end - 0 }

proc lineNORM { start step coords } {

	set coords [join $coords]
	set len [llength $coords]
	if { $len == 4 } { return $coords}
	regsub -all {\.0} $coords {} coords
	#Log "NORM I:   $coords"
	set stop [llength $coords]
	set xy 4
	set Ax [geomClip $step [lindex $coords 0]]
	set Ay [geomClip $step [lindex $coords 1]]
	lappend new $Ax $Ay
	set Bx [geomClip $step [lindex $coords 2]]
	set By [geomClip $step [lindex $coords 3]]
	while { $xy < $stop } {
		set Cx [geomClip $step [lindex $coords $xy]]
		incr xy
		set Cy [geomClip $step [lindex $coords $xy]]
		incr xy
		set sCB  [lineSlope $Bx $By $Ax $Ay] 
		set sBA  [lineSlope $Cx $Cy $Bx $By]

		set BAob [expr (($sBA > 1) || ($sBA < -1))]
		set CBob [expr (($sCB > 1) || ($sCB < -1))]
		#Log "  Slope CB  $sCB  BA $sBA Oblique  BA $BAob  CB $CBob"
		if { ($BAob || $CBob)  } {
			Log "   Oblique $start shifted BA $BAob/$sBA  CB $CBob/$sCB"
			if { $start != 0 } {
				if { $BAob } {
					if { $sBA == 2 } {
						set Bx $Ax
						set By $Cy
					} else {
						set Bx $Cx
						set By $Ay
					}
				} else {
					if { $sCB == 2 } {
						set Bx $Cx
						set By $By
					} else {
						set Bx $Bx
						set By $Cy
					}
				}
			}
			if { $start == 0 } {
				if { $CBob } {
					if { $sCB == -2 } {
						set Bx $Ax
						set By $Cy
					} else {
						set Bx $Cx
						set By $Ay
					}
				} else {
					if { $sBA == 2 } {
						set Bx $Cx
						set By $By
					} else {
						set Bx $Bx
						set By $Cy
					}
				}
			}

		}
		if { (($sCB != $sBA)) && ( ($Bx != $Ax) || ($By != $Ay) ) && ( ($Cx != $Bx) || ($Cy != $By) ) } {
			lappend new $Bx $By
		}
		set Ax $Bx
		set Ay $By
		set Bx $Cx
		set By $Cy
	}
	lappend new $Cx $Cy
	#Log "NORM O:   $new"
	return $new
}

proc lineSlope {args} {

	set list [join $args]
	set x1 [lindex $args 0]
	set y1 [lindex $args 1]
	set x2 [lindex $args 2]
	set y2 [lindex $args 3]

	set dx [expr $x2 - $x1]
	set dy [expr $y2 -$y1]
	if { $y2 < $y1 } {set sign -1 } else { set sign 1}
	if { $dy == 0 } { return 0 }
	if { $dx == 0 } { return 1 }
	if { $dx == $dy } { return [expr 0.5 * $sign] }
#
# oblique line 2 = in x , -2 = in y
#
	if { [expr abs($dx) > abs($dy)] } { return 2}
	return -2
}


proc lineEndOver { l } {

	set nl ""
	foreach {x y} $l {
		set nl "$x $y $nl"
	}
	return $nl
}









