0

I have a line identify by x1,y1,x2,y2 which are double values. Then I have several graphical objects (Let's name the class TShape) which cordinates are Left, Top, Right, Bottom: double. Only Top and Left properties are writable value. When dragging the TShape around the top and left values are updated.

I am using a function to discovery when TShape is near a Line. The function definition is:

function NearLine(const Target: TPoint; X1, Y1, X2, Y2: double; Off: integer = 5): boolean;

NearLine returns true if point specified by Target is near the line specified by Point1 and Point2. The point must be at the distance specified by Off.

I use the function with Off = 0;

In my implementation Target is the center of the TShape which I keep updated calculating it from Top and Left properties. Because Target is TPoint I do:

1-

CPoint.X := Trunc(Center.X);
CPoint.Y := Trunc(Center.Y);

2- and when the function NearLine above is true I force the mouse to release with:

3-

Mouse_Event(MOUSEEVENTF_ABSOLUTE or MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);

1,2 an 3 are called within an event UpdateMove which is called while dragging the shape.

This allow me to "stick" the TShape almost near the line however it's not exactly what I want to achive... obviously there is an error due the Trunc function.
The error is:

deltaX := Frac(Center.X);
deltaY := Frac(Center.Y);

After releasing the mouse programmatically how I can force all the center therefore all the shapes to be perfectly lined (collinear) with the line?

Any help? :(

PA.
  • 28,486
  • 9
  • 71
  • 95
marcostT
  • 573
  • 1
  • 10
  • 20
  • If your point's aren't already collinear (and you need 3 points for that because two points are always collinear) you can force them to be collinear by wrapping space and having them go trough the 4th dimension (time?) – Cosmin Prund Mar 25 '11 at 13:11
  • @Cosmin: love that comment. Must anticipate rectilinear responses on a programmer2programmer site :) – sehe Mar 25 '11 at 13:16
  • please can you format the question so that it is easily readable. – David Heffernan Mar 25 '11 at 13:33

1 Answers1

4

You're asking the question wrong, that's why you cant' see the answer your self. If 3 points aren't collinear, you're not going to "force" them collinear unless you change the laws of math and/or physics.

What you probably want is to find a point on the line defined by two points that's closest to your point of reference. That's pretty simple geometry: The closest point is as at the "foot" of a perpendicular drawn from your third point to the line defined by the first two! You can solve that using the Pythagoran theory alone, you don't even need fancy analytic geometry.

Cosmin Prund
  • 25,498
  • 2
  • 60
  • 104
  • do we need a mathoverflow site ? -- oops I'll ask on meta instead – sehe Mar 25 '11 at 14:20
  • >>Marcos, do you really need a code sample for this without trying first by yourself? R: Ye I will appreciate it, I am testing by myself almost since 2 weeks. The result is not so bad but not perfect due trunc function. – marcostT Mar 25 '11 at 19:38