Intersect Function

hh hh at hyperhh.de
Mon Sep 24 12:23:12 EDT 2018


It is not this complicated. Only if you are looking for
the intersection of two line *segments* you have do a
few more checks.

A LC object "line" is a line segment given by two pairs
of points (x,y).
The OP probably mean with line the math object line that is
given by and passing through these pair of points.

For example to get the intersection of two lines (if not
parallel) this works here.

-- pts1 = two lines of points from the first LC-"line"
-- pts2 = two lines of points from the second LC-"line"
function lineIntersection pts1,pts2
  put pts1 &cr& pts2 into p
  repeat with i=1 to 4
    put item 1 of line i of p into x[i]
    put item 2 of line i of p into y[i]
  end repeat
  put (y[4]-y[3])*(x[2]-x[1]) \
         - (y[2]-y[1])*(x[4]-x[3]) into d
  if abs(d) < 0.00001 then
    return "parallel"
  else
    put (x[4]-x[3])*(x[2]*y[1]-x[1]*y[2]) \
          - (x[2]-x[1])*(x[4]*y[3]-x[3]*y[4]) into xn
    put (y[1]-y[2])*(x[4]*y[3]-x[3]*y[4]) \
          - (y[3]-y[4])*(x[2]*y[1]-x[1]*y[2]) into yn
  end if
  return format ("%0.3f,%0.3f", xn/d, yn/d)
end lineIntersection

on mouseUp
  put the points of grc 1 into p1
  put the points of grc 2 into p2
  if there is no grc "ip" then
    create grc "ip"
    set style of grc "ip" to "oval"
    set rect of grc "ip" to (0,0,10,10)
    set foreColor of grc "ip" to "255,0,0"
    set lineSize of grc "ip" to 1 
  end if
  put lineIntersection(p1,p2) into ip
  put ip into fld "info"
  if ip is not "parallel" then
    set loc of grc "ip" to \
          round(item 1 of ip),round(item 2 of ip)
  end if
end mouseUp





More information about the use-livecode mailing list