comp.lang.idl-pvwave archive
Messages from Usenet group comp.lang.idl-pvwave, compiled by Paulo Penteado

Home » Public Forums » archive » Re: Is there a quick way to find the intersection of two lines?
Show: Today's Messages :: Show Polls :: Message Navigator
E-mail to friend 
Return to the default flat view Create a new topic Submit Reply
Re: Is there a quick way to find the intersection of two lines? [message #58609 is a reply to message #58607] Tue, 05 February 2008 02:45 Go to previous messageGo to previous message
Wox is currently offline  Wox
Messages: 184
Registered: August 2006
Senior Member
On Mon, 4 Feb 2008 16:08:16 -0800 (PST), eyuchen@gmail.com wrote:

> Hi there,
>
> I have two sets of x-y data:
> x1=[1,2,3,4,5] y1=[3.2,7.4,8.2,9.3,7.9];
> x2=[1.2,1.4,2.3,2.8,3.3,3.9,4.1,4.5,5.2]
> y2=[3.1,5.2,6.2,7.3,7.5,8.6,9.6,8.7,7.4];
>
> By running:
> plot, x1, y1
> oplot, x2, y2
> we can clearly see that there are four intersections, but it is not
> clear what are the x,y coordinates of these points.
>
> Is there an easy way to do it? Thank you very much.
>
> Eugene


I got carried away and made something with Ben's suggestion. Maybe
there's a better way, but it's a start.





function segmentintersect,L1x,L1y,L2x,L2y,xy=xy

; code:
; 0: not intersecting
; 1: intersect in 1 point
; 2: parallel
; 3: coincident

denom=float(L2y[1]-L2y[0])*(L1x[1]-L1x[0])-(L2x[1]-L2x[0])*( L1y[1]-L1y[0])
numa=(L2x[1]-L2x[0])*(L1y[0]-L2y[0])-(L2y[1]-L2y[0])*(L1x[0] -L2x[0])
numb=(L1x[1]-L1x[0])*(L1y[0]-L2y[0])-(L1y[1]-L1y[0])*(L1x[0] -L2x[0])

if denom eq 0 then code= (numa eq 0 and numb eq 0)+2 $
else begin
ua = numa / denom
ub = numb / denom

code= ua ge 0 and ua le 1 and ub ge 0 and ub le 1
if code then
xy=[L1x[0]+ua*(L1x[1]-L1x[0]),L1y[0]+ua*(L1y[1]-L1y[0])]
endelse

return,code
end;function segmentintersect
;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%

pro segtest

x1=[1,2,3,4,5]
y1=[3.2,7.4,8.2,9.3,7.9];
x2=[1.2,1.4,2.3,2.8,3.3,3.9,4.1,4.5,5.2]
y2=[3.1,5.2,6.2,7.3,7.5,8.6,9.6,8.7,7.4]
window
plot,x1,y1,psym=-2
oplot,x2,y2,psym=-2

n=n_elements(x2)
y2_1=interpol(y1,x1,x2)
b=y2_1 gt y2
interval=where(b[0:n-2]-b[1:*],ct)
if ct ne 0 then begin
xy=fltarr(2,ct)
for i=0,ct-1 do begin
j=interval[i]
L2x=x2[j:j+1]
L2y=y2[j:j+1]
j=value_locate(x1,L2x)
k=0
repeat begin
L1x=x1[j[k]:j[k]+1]
L1y=y1[j[k]:j[k]+1]
code=segmentintersect(L1x,L1y,L2x,L2y,xy=tmp)
b=code eq 1
if b then begin
xy[*,i]=tmp
plots,[tmp[0],tmp[0]],[0,tmp[1]],/data
endif
k++
endrep until b or (k eq 2)
endfor
endif

end;pro segtest
;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Previous Topic: Confusion about the filter in time and frequency domain
Next Topic: Re: Confusion about the filter in time and frequency domain

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ] [ PDF ]

Current Time: Sat Oct 11 05:34:00 PDT 2025

Total time taken to generate the page: 0.72238 seconds