Force points to be on a line
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 draggi开发者_JAVA技巧ng 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? :(
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.
精彩评论