3

I am a software engineer and I'm trying to edit some images. Trying to find a good formula to find a new point on the rectangle, I know the center x and y coordinates, the height and width of the rectangle, and the theta in the direction of the new point.

if I can find the length of the line from the center to the edge I can just take a sin and cos to get my new position. The issue I'm having is that it seems like there are a lot of special cases given different angles for whether I should be shifting it with regards to width or height etc and I was hoping that someone would have a simpler formula I could use.

Thanks a lot for taking the time to try and help me!

Justin
  • 131
  • 2
    Here's a suggestion about how to organize the cases more effectively: Compute the angle (based at the center of the rectangle) from the horizontal axis to the upper right-hand corner and save it. For angles between $0$ and this special angle, you're on the right hand side and you have something involving the cosine. For angles between the special angle and $\pi/2$, you're on top. Repeat for other quadrants as needed. –  Dec 03 '18 at 23:03
  • Further, assuming the center is at the origin and the rectangle is oriented parallel to the axes, the angle from the horizontal axis to the upper right corner is the angle whose tangent is $\frac{h}{w}$. – rogerl Dec 03 '18 at 23:24
  • Thank you both, the corner angle property is especially helpful! – Justin Dec 03 '18 at 23:28
  • Instead of going through all of the case analysis you could just compute all four intersections and select the nearest one that’s in the correct direction from the ray. This is particularly simple to do if the sides are axis-aligned. – amd Dec 04 '18 at 00:08

0 Answers0