Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

so no easiest question ! ;)

look here.

You have line segment from point A1(ax1,ay1) to point B1(bx1,bx2)

first you must find the line for this points.

I think so you can use line equation like this :

y := a1*x+b1

so you must compute the a1 and b1

a1 := (by1-ay1)/(bx1-ax1)

b1 := ay1 - ax1*(by1-ay1)/(bx1-ax1)

now you must find line which is perpendicular to this one. Ok this line is :

y := -a1*x +b2

You don't know the b2, but the line need to go throught your point X1,Y1

so you can compute the b2 and :

b2 := Y1+a1*X1

now you must find the point of intersection of this lines. Nothing easiest.You must find Point X0,Y0 which is in both lines.

so you have

X0 := (X1-b1+Y1*a1)/(2*a1)

Y0 := (X1-b1+Y1*a1)/2 + b1

ok now we have 2 examples

1. the point X0.Y0 is on the line segment

2. the point X0.Y0 is outside the line segment

we must know at which example we are so you can easy check this, but for easiest way we can make an assumption so the ax1<bx1 and if not than you can check this on start of proc and than exchange points

In this assumption we have :

if (X0>ax1) and (X0<ax2) then point1 else point2;

so for point1 we need to calculate the length of the line segment from point X1,Y1 to point X0,Y0

the leght is l:= Sqrt((X1-X0)*(X1-X0)+(Y1-Y

for the second point2 we need calculate the distance between point X1.Y1 and points A1 and B1 and get the lowest value

so :

l1 := Sqrt((X1-ax1)*(X1-ax1)+(Y1

l2 := Sqrt((X1-ax2)*(X1-ax2)+(Y1

if l1<l2 then l := l1 else l := l2;

It is easy - right ? ;)

ok now we can prepare the final proc for getting the l

if ax1>ax2 then { exchange points - easy and I don't write this }

a1 := (by1-ay1)/(bx1-ax1);

b1 := ay1 - ax1*(by1-ay1)/(bx1-ax1);

X0 := (X1-b1+Y1*a1)/(2*a1);

Y0 := (X1-b1+Y1*a1)/2 + b1;

if (X0>ax1) and (X0<ax2) then

begin

l:= Sqrt((X1-X0)*(X1-X0)+(Y1-Y

end

else

begin

l1 := Sqrt((X1-ax1)*(X1-ax1)+(Y1

l2 := Sqrt((X1-ax2)*(X1-ax2)+(Y1

if l1<l2 then l := l1 else l := l2;

end;

this is all, however you must trap examples when the a := 0 ! because in this examples you can get devide by 0 error !

Regards

mirek.