Answer to a question no one asked

James Hurley jhurley at infostations.com
Thu Jan 6 10:40:10 EST 2005


What is the perpendicular distance between a point and a line?

Application for which there is no redeeming social value:

Create a line graphic "line" and an oval graphic "circle"
Put the following script into a transparent radio button
and then drag that button.

local myName, p2,p3

on mouseDown
   put the name of me into myName
   put the points of grc "line" into tPoints
   put line 1 of tPoints into p2
   put line 2 of tPoints into p3
end mouseDown

on mouseUP
put "" into myName
end mouseUP

on mouseLeave
   mouseUP
end mouseLeave

on mouseMove u,v
   if myName is "" then exit mouseMove
   set the loc of me to u,v
   put u,v into p1
   put  abs(perpDist(p1,p2,p3)) into tRadius
   set the loc of grc "circle" to u,v
   set the width of grc "circle" to 2*tRadius
   set the height of  grc "circle" to 2*tRadius
end mouseMove

function perpDist p1,p2,p3
   --Returns the perpendicular distance
   --between point p1 and the line with
   --end points p2 and p3
   put item 1 of p1 into x1
   put item 2 of p1 into y1
   put item 1 of p2 into x2
   put item 2 of p2 into y2
   put item 1 of p3 into x3
   put item 2 of p3 into y3

   if x3-x2 is 0 then
     return (x1-x2)
   else
     put (y3-y2)/(x3-x2) into m -- The slope
     return (m*(x1-x2)-(y1-Y2))/sqrt(1+m*m)
   end if

end perpDist

Jim


More information about the Use-livecode mailing list