Detecting line intersection - 13/05/03 08:23 AM
I have 2 Lines drawn in a picture window, I want to detect if they overlap at all, and return the point of intersection. Any ideas how to do this?
cross { ; usage /cross <line1 x1> <line1 y1> <line1 x2> <line1 y2> <line2 x1> <line2 y1> <line2 x2> <line2 y2> ; line 1: %x11 = $1 %y11 = $2 %x12 = $3 %y12 = $4 ; line 2: %x21 = $5 %y21 = $6 %x22 = $7 %y22 = $8 ;we don't like Div by 0(ie. horizontal lines), see the next too calcs, even though mIRC doesn't care about it... if (( %x11 == %x12 ) || ( %x21 == %x22 )) return %k1 = $calc( ( %y12 - %y11 ) / ( %x12 - %x11 ) ) %k2 = $calc( ( %y22 - %y21 ) / ( %x22 - %x21 ) ) if ( %k1 == %k2 ) return %x = $calc( ( %y11 - %y21 + %k2 * %x21 ) / ( %k2 - %k1 ) ) %y = $calc( %k1 * ( %x - %x11 ) + %y11 ) echo Lines ( %x11 , %y11 ) - ( %x12 , %y12 ) and ( %x21 , %y21 ) - ( %x22 , %y22 ) intersects @ $& ( %x , %y ) }
cross { set %x11 $1 set %y11 $2 set %x12 $3 set %y12 $4 set %x21 $5 set %y21 $6 set %x22 $7 set %y22 $8 if (( %x11 == %x12 ) || ( %x21 == %x22 )) { return } set %k1 $calc( ( %y12 - %y11 ) / ( %x12 - %x11 ) ) set %k2 $calc( ( %y22 - %y21 ) / ( %x22 - %x21 ) ) if ( %k1 == %k2 ) { return } set %x $calc( ( - %k1 * %x11 + %y11 - %y21 + %k2 * %x21 ) / ( %k2 - %k1 ) ) set %y $calc( %k1 * ( %x - %x11 ) + %y11 ) return %x %y }