# segments intersection

## segments intersection

Hi
I need a fortran algorithm to check if two line segments intersect each other ( the segments being defined by their cartesian coordinates).
Can anyone help me?
Thanks
Geraldo Cacciatore

5 posts / 0 new
For more complete information about compiler optimizations, see our Optimization Notice.

The problem is trivial in two dimensions - just check if the lines are parallel or not. How many dimensions do you want to work in?

I know how to solve the problem using analytical geometry: find the interception point of the supporting lines (if the segments are not parallel) end see if this point is inside both segments ( my space is 2D). Check also for overlapping, etc..
But I am not an expert fortran programmer, and as my fortran implementation may not be an efficient one I am looking for something already tested.
Thanks again
Geraldo

Here is some code that caluslates the intersection of two lines.
Hope this helps.
Regards,
David.

```SUBROUTINE CROSS (x1,y1,x2,y2,p1,q1,p2,q2,E1,N1,kout)
!       ---------------------------------------------------------------
!       CHECK FOR INTERSECTION OF LINE x1,y1-x2,y2 WITH p1,q1-p2,q2, result in E1,N1

integer*2	kout	! 0=no solution, 1=solution
INTEGER*4 x1,y1,x2,y2,p1,q1,p2,q2,E1,N1,EMINA,NMINA,EMAXA,NMAXA,EMINB,NMINB,EMAXB,NMAXB
real*8    EA,EB,EC,NA,NB,NC,COTA,COTB,D

kout=0
IF (x1EMAXB.OR.NMINA>NMAXB) GO TO 500
IF (EMINB>EMAXA.OR.NMINB>NMAXA) GO TO 500

EA=x1
NA=y1
EB=p1
NB=q1
IF (p1==p2) EB=EB+1D-3
IF (x1==x2) EA=EA+1D-3
COTA=(NA-y2)/(EA-x2)
COTB=(NB-q2)/(EB-p2)
IF (DABS(DABS(COTA)-DABS(COTB)).LT.1D-10) GO TO 500
EC=(EA*COTA-EB*COTB-NA+NB)/(COTA-COTB)
D=EC
NC=EC*COTA-EA*COTA+NA+5D-1
EC=D+5D-1

E1=EC
N1=NC
IF (E1EMAXA.OR.E1>EMAXB) GO TO 100
IF (N1NMAXA.OR.N1>NMAXB) GO TO 100
kout=1			! valid solution
GO TO 500

100   kout=0
500   RETURN
END
```

Thanks,I will try the code you send me.
I found an interesting algorithm in www.brunel.ac.uk/~castjjg/java/crossed/crossed.html that also deserves a try.
Best regards
Geraldo Cacciatore