When drawing an angle line (45 degrees) in a rectangle from a general point $p = (x,y)$ that located on the right or the top line of the rectangle. How can I find the intersection point $p2$ of this line with the rectangle?

In other words, I want to write the target point, $p2$, with my current information: $x, y, w, h$. (This variables are described in the picture below).

The point $(0,0)$ is in the top-right corner.


  • 2
    $\begingroup$ Could you rephrase your question? I can't tell what is being asked. Does the line passing through ??? and (x,y) have slope 1? $\endgroup$ – Larry Wang Jul 28 '10 at 0:55
  • $\begingroup$ The line is going from p (a point in the right or top line) to p2 a point in the left or bottom line. p coordinates are x and y. How can I represent p2 by x,y,w,h? $\endgroup$ – stacker Jul 28 '10 at 1:02
  • $\begingroup$ I would just construct the line, given the fact that you have it's point ((x,y)) by assumption, and you have it's slope $m=1$. Then find the point that this line intersects the rectangle. $\endgroup$ – JacksonFitzsimmons Dec 24 '15 at 5:19
  • $\begingroup$ In your example if$(x,y)=(-5,0)$ Given we have the slope $m=1$, we can use the point-slope equation of a line to find an explicit equation for the line. In this example the line is given by $y=x+5$. The line that describes the left hand side of your picture is given by $x=-10$. These curves intersect at $(-10,-5)$. $\endgroup$ – JacksonFitzsimmons Dec 24 '15 at 5:22
  • $\begingroup$ In general you'll have to look at each rectangle as a separate case, but you could find a single formula (or maybe two formulae) that completely gives you $P_2$ as a function of $P_1$ if you consider only one rectangle. $\endgroup$ – JacksonFitzsimmons Dec 24 '15 at 5:23

Alright, I'm not 100% sure I'm understanding this correctly. You say that p can be located on the right or top line and that p2 can be located on the bottom or left line. Do you mean the rectangle can be rotated? If that's the case, the question should say that p can be on the right or bottom line of the rectangle. Also, are you looking for two separate answers or one that works both when p2 is on the bottom and on the left?

If you do mean that the rectangle can be rotated, and want two different answers, it's pretty simple. First I'll deal with when p2 is on the bottom and p is on the right.

Since p2 is on the bottom line we know the y-coordinate is h, according to the diagram. We also know that p is (0,y). Because of the 45 degree angle, we know that the distance between p's y-coordinate and the lower right corner is the same as the distance between the lower right corner and p2's x-coordinate, which in this case is p2's x-coordinate. Therefore, the coordinates of p2 are (h-y, h).

If p2 is on the left and p is on the bottom, it's very similar. Since p2 is on the left, it's x-coordinate is h. Because p is on the x-axis, it's (x,0). Because of the 45 degree angle, the distance between the lower left corner and p is the same as the distance between the lower left corner and p2, which this time gives us p2's y-coordinate. Therefore the coordinates of p2 are (h,h-x).

Hopefully I understood your intentions correctly. If not, I hope you can use my misunderstandings to further improve your question.

  • $\begingroup$ The rectangle cannot be rotated. The p is a general point, that his x and y coordinates, are on the right or the top line. $\endgroup$ – stacker Jul 28 '10 at 17:20

If it is just $45$ degrees, then the answer is not very difficult. Center a coordinate system at the bottom left hand corner of the rectangle. Hence the coordinates of the (???) point are $(q,0)$ for some $q<w$.

Note that the because theta is $45$ degrees, $y=w-q$ (Isosceles right triangle). Hence $q=w-y$, and our point is simply $(w-y, 0)\dots$

  • $\begingroup$ I edited the question to define (0,0). I don't know why you're assume that p2 always on the bottom line. It can be also on the left line. $\endgroup$ – stacker Jul 28 '10 at 2:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.