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? :(