# how to truncate a line along the same slope at a given boundary?

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".

-
thank you so much. i graphed the result and it totally works. but i haven't done algebra in 20+ years and am trying to remember. how do I shift the formula so that that one side of it has no unknown values? in other words, given the test case you layed out, how would it be x = ? am i making sense? i need to translate this to computer code which is why I need to rearrange it. –  mix Feb 20 '11 at 6:07
You can add or multiply both sides by the same thing. So from $y-4=\frac{3}{2}(x-4)$, $\frac{2}{3}(y-4)=x-4$, $4+\frac{2}{3}(y-4)=x$ –  Ross Millikan Feb 20 '11 at 6:19
ok. so to solve it for y would it be: y = 3/2(x-4) * 1/4? Or, in variables: y = (y2-y1)/(x2-x1) * (boundaryX - x1) * (1/y1)? i'm trying to multiply each side by 1/4 to get rid of the -4 on the left side. –  mix Feb 20 '11 at 6:35
No, to solve $y-4=\frac{3}{2}(x-4)$ for $y$, just add 4 to both sides to get $y=\frac{3}{2}(x-4)+4$ –  Ross Millikan Feb 20 '11 at 16:11
thanks! i implemented the formula and ran a number of tests and found a few occasions where it isn't working for me. i suspect i'm using it wrong. an example is for the coords (-4,6)(-6,5). the formula should truncate them to within the -5/5 boundaries, but instead I get (-6,5)(-5,5.5). for examp, on the first pair, the new Y1 becomes the limit, 5, and the the new X1 is calculated: newX1 = (x2-x1)/(y2-y1) * (maxY - y1) + x1, or x = 2 * -1 + -4, or x = -6. am i using the formula incorrectly? –  mix Feb 21 '11 at 3:58

I'd suggest you to use vector formulas and at the end unfold them to X and Y coordinates. So, the function of your line is $r_0+t\cdot (r_1-r_0)$ with a scalar parameter $t$, where $r_0=(4,4)$ and $r_1=(6,7)$. Then boundaries of your rectangle comes into play. The top boundary has equation $y=5$.

$r_{0y}+t\cdot (r_{1y}-r_{0y})=5$

$t=\frac{5-r_{0y}}{r_{1y}-r_{0y}}$ (found $t$)

$x$ of the intersection point = $r_{0x}+t\cdot (r_{1x}-r_{0x})$.

For 4 boundaries you will have 4 intersection points. But your line obviously has 2 points of intersection with the rectangle. You chose intersection points based on the values of $t$. (Do not forget that every intersection point has a $t$ parameter.) Namely, trash 2 intersection points with the least $t$ and the greatest $t$. (I hope it is clear what I'm saying here.)

-