Two Lines Crossing Problem [message #68989] |
Wed, 09 December 2009 17:51 |
Jye
Messages: 17 Registered: May 2008
|
Junior Member |
|
|
Hi Everyone,
I currently have a small script that determines if a line projected
out from a point on an array cross another defined line. Plus it also
calculates the angle of intersection. Below is an example of how Ive
been doing it.. and probably a good example of how not to do it. My
question (challenge) is what would be a more elegant approach to this
problem? and as a result decrease the processing time?
Cheers
Jye
PRO CROSS_AND_ANGLE
; Array Dimensions
X = 400
Y = 400
; Point on the array
pX = 10
pY = 30
; Coordinates defining the ends of a line
x1 = 200
x2 = 200
y1 = 100
y2 = 300
Cross = FINDGEN(X, Y) * 0
Angle = FINDGEN(X, Y) * 0
p = [pX, pY]
L = [x1, x2, y1, y2]
Xarray = REBIN(FINDGEN(X, 1), X, Y)
Yarray = REBIN(FINDGEN(1, Y), X, Y)
x1 = L[0] * 1.
y1 = L[2] * 1.
x2 = L[1] * 1.
IF x2 EQ x1 THEN x2 += 1
y2 = L[3] * 1.
u1 = p[0] * 1.
v1 = p[1] * 1.
u2 = Xarray
u2[where(u2 eq u1)] += 1
v2 = Yarray
b1 = (y2 - y1) / (x2 - x1)
b2 = (v2 - v1) / (u2 - u1)
a1 = y1 - b1 * x1
a2 = v1 - b2 * u1
xi = -(a1 - a2)/(b1 - b2)
yi = a1 + b1 * xi
Index = where( (x1-xi)*(xi-x2) GE 0 AND (u1-xi)*(xi-u2) GE 0 AND (y1-
yi)*(yi-y2) GE 0 AND (v1-yi)*(yi-v2) GE 0 )
IF Index[0] NE -1 THEN BEGIN
Cross[Index] = 1
Angle[Index] = (ABS(ABS(ATAN(b1)*180/!PI - ATAN(b2)*180/!PI) - 90))
[Index]
ENDIF
window, 0, xsize=X, ysize=Y
tvscl, Cross
window, 1, xsize=X, ysize=Y
tvscl, Angle
END
|
|
|