How to shorten a line but maintain its angle?

863 Views Asked by At

I have a line that looks like this:

enter image description here

to draw this line i have the following:

point A: (x,y) point B: (x,y)

so for example, i will be given this path:

(90,20),(400,300)

and then i can draw a line because i know point A and B clearly.

Now my issue is the following:

i would like to shorten both ends of the line but maintain its angle.

I tried just subtracting manually a value from y position but its not always accurate. see the thing is the points i will be given ALWAYS make the line point to the center of the markers on the map. See how in the photo the line points directly to the center of the "B" marker. so i just need to maintain its angle and i'll be fine. So how can i shorten the line on both ends but still maintain the correct angle ?

the coordinate system i am using looks like this:

enter image description here

1

There are 1 best solutions below

2
On BEST ANSWER

If the coordinates of $A$ are $(Ax,Ay)$ and of $B$ are $(Bx,By)$ the current length of the line is $$L=\sqrt{(Ax-Bx)^2+(Ay-By)^2}$$ You can parameterize the line as $$(Ax+t(Bx-Ax),Ay+t(By-Ay))$$ When $t=0$ you are at $A$ and when $t=1$ you are at $B$. If you want to shorten the line, just go from, say, $t=0.1$ to $t=0.8$. That will shorten the line by $10\%$ at the $A$ end and $20\%$ at the $B$ end.