Let's say I have a 2D coordinate space defined by $-5 \leq x,y \leq 5$.
Then let's say I have coordinates $(x_1,y_1)$ and $(x_2,y_2)$ for a line that will run from $(4,4)$ to $(6,7)$.
How do I truncate that line so that it ends at the edge of the coordinate space (but maintains the same slope)? In other words, how do I get the values for $(x_2,y_2)$ so that $y_2$ is 5 (the maximum it could be)?
I originally thought that just using the percentage change in the highest of the out of boundary coordinates would do the trick (e.g. $(X_{max})({x_2}/{y_2})$), but I get a different slope.
If things change when x or y goes negative I need to account for that too.
thank you for any help!!
The two point formula for a line is $y-y_1=(x-x_1)\frac{y_2-y_1}{x_2-x_1}$ where the points are $(x_1,y_1)$ and $(x_2,y_2)$ You can plug your boundaries into this to find where the line intersects the boundaries. So in your case you have $y-4=\frac{3}{2}(x-4)$ and you can put in $y=5$ to solve for $x$ where you hit the boundary, getting $4\frac{2}{3}$. If you put in all four boundaries, two of the points will be "in play" and two will be "out of bounds", so just draw the line between the ones "in play".