delphimathstraight-line-detection

Plotting a point on straight line


The equation of a line is Y = M.X + C,

I have a point and the points facing angle, So I can work out the line equation

Slop := Tan(Rot)  // We  are passing radians to convert to gradient
C := (-Slop*X) + Y // Substitute our point XY values 

So that's the current math I am using to get our Y intercept and our slop or gradient.

However I am wanting to know how to plot a point X amount of distance in front of our starting point.

Currently, I am attempting the following where Y2 and X2 are values of our original point plus 100 units.

 NewPoint.X :=  Round( (Y2 - C) / Slop );
 NewPoint.Y := Round((slop*X2) + C);

Here a paste bin of the full function :

https://pastebin.com/8435NzYc

Thanks.


Solution

  • To make things simpler, define your line with parametric equations:

    X = X0 + UX * t
    Y = Y0 + UY * t
    

    Where X0, Y0 are coordinates of some base point, UX, UY are components of unit direction vector. Note that

    UX = Cos(Phi)
    UY = Sin(Phi)
    

    where Phi is angle between line and OX axis.

    On the other hand, Tan(Phi) is equal your slope.

    If line is defined with two points, then

    Len = Hypot(X1 - X0, Y1 - Y0)
    UX = (X1 - X0) / Len
    UY = (Y1 - Y0) / Len
    

    And point at needed distance Dist from base point is just

    X = X0 + UX * Dist
    Y = Y0 + UY * Dist