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.