[1]


Task

Find the point of intersection of two lines in 2D.



The 1st line passes though    (4,0)    and    (6,10) .

The 2nd line passes though    (0,3)    and    (10,7) .




INCLUDE "D2:REAL.ACT" ;from the Action! Tool Kit
 
DEFINE REALPTR="CARD"
TYPE PointR=[REALPTR x,y]
 
PROC Det(REAL POINTER x1,y1,x2,y2,res)
  REAL tmp1,tmp2
 
  RealMult(x1,y2,tmp1)
  RealMult(y1,x2,tmp2)
  RealSub(tmp1,tmp2,res)
RETURN
 
BYTE FUNC IsZero(REAL POINTER a)
  CHAR ARRAY s(10)
 
  StrR(a,s)
  IF s(0)=1 AND s(1)='0 THEN
    RETURN (1)
  FI
RETURN (0)
 
BYTE FUNC Intersection(PointR POINTER p1,p2,p3,p4,res)
  REAL det1,det2,dx1,dx2,dy1,dy2,nom,denom
 
  Det(p1.x,p1.y,p2.x,p2.y,det1)
  Det(p3.x,p3.y,p4.x,p4.y,det2)
  RealSub(p1.x,p2.x,dx1)
  RealSub(p1.y,p2.y,dy1)
  RealSub(p3.x,p4.x,dx2)
  RealSub(p3.y,p4.y,dy2)
  Det(dx1,dy1,dx2,dy2,denom)
 
  IF IsZero(denom) THEN
    RETURN (0)
  FI
 
  Det(det1,dx1,det2,dx2,nom)
  RealDiv(nom,denom,res.x)
  Det(det1,dy1,det2,dy2,nom)
  RealDiv(nom,denom,res.y)
RETURN (1)
 
PROC PrintPoint(PointR POINTER p)
  Print("(") PrintR(p.x)
  Print(",") PrintR(p.y)
  Print(")")
RETURN
 
PROC PrintLine(PointR POINTER p1,p2)
  PrintPoint(p1)
  Print(" and ")
  PrintPoint(p2)
RETURN
 
PROC Test(PointR POINTER p1,p2,p3,p4)
  BYTE res
  REAL px,py
  PointR p
 
  p.x=px p.y=py
  Print("Line 1 points: ")
  PrintLine(p1,p2) PutE()
  Print("Line 2 points: ")
  PrintLine(p3,p4) PutE()
 
  res=Intersection(p1,p2,p3,p4,p)
  IF res THEN
    Print("Intersection point: ")
    PrintPoint(p) PutE()
  ELSE
    PrintE("There is no intersection")
  FI
  PutE()
RETURN
 
PROC Main()
  REAL x1,y1,x2,y2,x3,y3,x4,y4,px,py
  PointR p1,p2,p3,p4
 
  Put(125) PutE() ;clear screen
 
  p1.x=x1 p1.y=y1
  p2.x=x2 p2.y=y2
  p3.x=x3 p3.y=y3
  p4.x=x4 p4.y=y4
 
  IntToReal(4,x1) IntToReal(0,y1)
  IntToReal(6,x2) IntToReal(10,y2)
  IntToReal(0,x3) IntToReal(3,y3)
  IntToReal(10,x4) IntToReal(7,y4)
  Test(p1,p2,p3,p4)
 
  IntToReal(0,x1) IntToReal(0,y1)
  IntToReal(1,x2) IntToReal(1,y2)
  IntToReal(1,x3) IntToReal(2,y3)
  IntToReal(4,x4) IntToReal(5,y4)
  Test(p1,p2,p3,p4)
RETURN