Michael von Aichberger <[EMAIL PROTECTED]> wrote:
> Suppose you have two straight lines, defined by two points:
> 
> Let's say
> 
> point(x1, y1) and point(x2, y2) for line 1
> 
> and
> 
> point(x3, y3) and point(x4, y4) for line 2.
> 
> What's the algorithm for calculating the point where these two lines
> intersect?

Hi Michael,

The mathematics are quite simple, but there are a number of special cases
(vertical and horizontal lines) that need to be taken into consideration.
You'll find below my handlers for this.  You could use the following syntax:

line1        = lineEquation(point(x1, y1), point(x2, y2))
line2        = lineEquation(point(x3, y3), point(x4, y4))
intersection = lineCutsLine(line1, line2)
put intersection

Cheers,

James


----------------------------------------------------------------------------


on lineCutsLine(line1, line2) ----------------------------------------
  -- ACTION: Finds the intersection points for two infinite lines
  --
  -- INPUT:
  -- <line1> and <line2> must be property lists with one of the
  -- following formats:
  --   [#slope: <float>, #zeroX: <float>] --> y = ax + b
  --   [#x: <float>]                      --> vertical line
  -- See the lineEquation() handler for details.
  --
  -- RETURNS: point(<integer or float>, <integer or float>) if the
  -- lines intersect, or #parallel if they do not.
  --------------------------------------------------------------------
  
  if voidP(line1[#slope]) then
    -- line1 is vertical
    if voidP(line2[#slope]) then
      -- line2 is also vertical
      return #parallel
    else
      
      -- Intersection with line2 will occur at line1.x
      x = line1.x
      y = line2.slope * x + line2.zeroX
      return point(x, y)
    end if
    
  else if voidP(line2[#slope]) then
    -- line2 is vertical: Swap line1 and line2 and try again
    return lineIntersection(line2, line1)
    
  else -- Both lines slope
    slopeDifference = line1.slope - line2.slope
    if slopeDifference = 0 then
      return #parallel
    end if
    
    x = (line2.zeroX - line1.zeroX) / slopeDifference
    y = line1.slope * x + line1.zeroX
    return point(x, y)
  end if
end lineCutsLine



on lineEquation(point1, point2) --------------------------------------
  -- ACTION: Converts a 2D line defined by two points into a formula
  -- corresponding to:
  --   y = ax + b (or in Lingo : v = slope * h + zeroX).
  --
  -- The two points used to define the line may be considered to be
  -- the end points of a segment, or one of them may be the end point
  -- of a ray.
  --
  -- INPUT:
  -- <point1> and <point2> should be point objects.
  --
  -- RETURNS: a list with one of the following formats::
  --
  --   [#slope: <float>, #zeroX: <float>, #x1: <float>, #x2: <float>]
  --   [#x: <float>, #y1: <float>, #y2: <float>]   -- vertical line
  --
  -- In the first version, #slope corresponds to "a", #zeroX
  -- corresponds to "b" and #x1 and #x2 correspond to the values for
  -- x at the ends of the line segment.
  --
  -- The second version is a special case for a vertical line.
  -- #x corresponds to the constant value of x for all values of y.
  -- #y1 and #y2 give the y values at the ends of the line segment.
  --------------------------------------------------------------------
  
  -- Check parameters
  if ilk(point1) <> #point then
    return #invalidPoint
  else if ilk(point2) <> #point then
    return #invalidPoint
  else if point1 = point2 then
    return #identicalPoints
  end if
  -- End of checking
  
  h1 = point1.locH
  v1 = point1.locV
  h2 = point2.locH
  v2 = point2.locV
  
  deltaH = h1 - h2
  if deltaH = 0 then -- The line is vertical
    tEquation = [#x: h1]
    
    tEquation[#y1] = v1
    tEquation[#y2] = v2
    
  else -- The line is horizontal or sloping
    b = float((h1 * v2) - (h2 * v1)) / (h1 - h2)
    if h1 = 0 then
      a = (v2 - b) / h2
    else
      a = (v1 - b) / h1
    end if
    
    tEquation = [#slope: a, #zeroX: b]
    
    tEquation[#x1] = h1
    tEquation[#x2] = h2
    
  end if
  
  return tEquation
end lineEquation

[To remove yourself from this list, or to change to digest mode, go to 
http://www.penworks.com/lingo-l.cgi  To post messages to the list, email 
[EMAIL PROTECTED]  (Problems, email [EMAIL PROTECTED]). Lingo-L is for 
learning and helping with programming Lingo.  Thanks!]

Reply via email to