mIRC Home    About    Download    Register    News    Help

Print Thread
Detecting line intersection #23912 13/05/03 08:23 AM
Joined: Feb 2003
Posts: 32
S
SaX0n Offline OP
Ameglian cow
OP Offline
Ameglian cow
S
Joined: Feb 2003
Posts: 32
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?

Re: Detecting line intersection #23913 13/05/03 12:21 PM
Joined: Dec 2002
Posts: 774
T
theRat Offline
Hoopy frood
Offline
Hoopy frood
T
Joined: Dec 2002
Posts: 774
This (should) show the intersection(or where it would be if the lines were infinite in length)...
About checking if they intersect on screen, dunno, atm at least...

Code:
 
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 )
}
 


Done with the help of mathbook and too tired to flip things around if lines are horizontal...


Code:
//if ( khaled isgod ) echo yes | else echo no
Re: Detecting line intersection #23914 13/05/03 12:32 PM
Joined: Feb 2003
Posts: 32
S
SaX0n Offline OP
Ameglian cow
OP Offline
Ameglian cow
S
Joined: Feb 2003
Posts: 32
Thanks for your reply. I changed the last line so I can use the alias as an identifier, and the result seems incorrect:

alias test {
window -podC +tn @test 1 1 250 250
drawrect -rf @test 0 0 0 0 900 900

drawline -r @test 220 1 30 30 100 200
drawline -r @test $rgb(255,255,255) 1 30 200 70 30

drawdot -r @test $rgb(0,0,255) 3 $cross(30,30,100,200,30,200,70,30)
}

Also, the lines that I will be checking will include horizontal and vertical lines, what would i do in that situation? Thanks for your help.

- The last line was replaced with:
return %x %y

Last edited by SaX0n; 13/05/03 12:35 PM.
Re: Detecting line intersection #23915 13/05/03 01:20 PM
Joined: Dec 2002
Posts: 774
T
theRat Offline
Hoopy frood
Offline
Hoopy frood
T
Joined: Dec 2002
Posts: 774
Code:
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
}
  


Now this works, it's not my fault that mIRC draws the dots in the wrong place?


Code:
//if ( khaled isgod ) echo yes | else echo no
Re: Detecting line intersection #23916 13/05/03 01:39 PM
Joined: Feb 2003
Posts: 32
S
SaX0n Offline OP
Ameglian cow
OP Offline
Ameglian cow
S
Joined: Feb 2003
Posts: 32
That one works, and the dot is in the right place (when it's size 1) thanks.