get (x/y) coordinates of a square given angle?

Started by
4 comments, last by suliman 7 years, 1 month ago

Hi

If i have a square in a coordinate-system in x and y (0,0 is upper left, 1,1 is lower right), how do I get the point on the border given an angle (in radians)?

So that:

angle=pi/2 gives (1,0.5)

angle=pi gives (0.5,1)

etc

I got stuck on this one. Thanks for any help!
Erik

Advertisement

I haven't tested this but I'm pretty sure it should work.

I'm assuming your square is defined by things:

- a center point

- a radius

- a rotation


vec2 point1 = vec2(cos(pi / 4 + angle), sin(pi / 4 + angle)) * sqrt(2) * radius + center;
vec2 point2 = vec2(cos(3 * pi / 4 + angle), sin(3 * pi / 4 + angle)) * sqrt(2) * radius + center;

vec2 point3 = vec2(cos(-pi / 4 + angle), sin(-pi / 4 + angle)) * sqrt(2) * radius + center;
vec2 point4 = vec2(cos(-3 * pi / 4 + angle), sin(-3 * pi / 4 + angle)) * sqrt(2) * radius + center; 

This should give you all 4 points of the square, but once you pass pi/2 the relative position of the points will change. For instance point1 will become the top left point instead of the top right. You could combat this by either sorting the points after or just use something like:


void setAngle(float _angle) {

   angle = angle % (pi / 2);

}

which is much more efficient.

This works because if you look at a unit circle, you can make a square on the circle by taking the points at pi/4, 3pi/4 5pi/5 and 7pi/4. This will give us a square with a radius of sqrt(2)/2 because cos(pi/4) is sqrt(2)/2. To make it a radius of 1 we multiply by the square root of 2 and then by the desired radius. Take a look at the unit circle if you're having any issues visualizing this.

I would verify this before using it (I didn't really do extensive testing) but it should work.

1. Figure out which edge the ray would hit (trivial if you're centered in a square).

2. Make an angle relative to the perpendicular point on that edge (trivial in this case: the center of the edge)

3. The offset along the edge from that point (its center in this case) = tan(relative_angle) * distance_from_center_to_edge; // distance is 0.5f in your case.


For the square case you can do it very easily. For other shapes you may just want to use a general purpose ray/line intersection instead.

To clearify I already can get the point given an angle, but this point is at unit-distance (so I get the points of the circle, not the points of the square around the mid-point).

Can I get the distance I need to enlong the "circle-point" with to get the "square-point" for each angle? Is that tan(angle) maybe?

I'll do it in coordinates where (-1,+1) is the top-left corner and (+1,-1) is the bottom-right corner. You can do the conversion to your convention yourself.

#include <iostream>
#include <complex>

typedef std::complex<float> C;

C point_on_square_edge_from_angle(float angle) {
  C direction(std::cos(angle), std::sin(angle));
  return direction / std::max(std::abs(direction.real()), std::abs(direction.imag()));
}

int main() {
  float const pi = std::atan(1.0f)*4.0f;
  std::cout << point_on_square_edge_from_angle(pi*0.5f) << '\n';
  std::cout << point_on_square_edge_from_angle(pi) << '\n';
  std::cout << point_on_square_edge_from_angle(pi*0.25f) << '\n';
}

I got bluespuds suggestion working with my code.

Thanks all for your help!

This topic is closed to new replies.

Advertisement