0
$\begingroup$

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!!

2 Answers 2

2

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

  • 0
    The equation is then y-6=-2(x+4). This will intersect your four boundary lines in one point each. You plug x=-5, x=5, y=-5, and y=5 in and solve for the other. In this case you get (-5,8),(5,-12),(1.5,-5), and (-3.5,5). At most two will be within the rectangle, in this case the last two, so they are the ones you want. If none are within your rectangle, the line misses entirely, like y=x+11 does.2011-02-21
0

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