-
Math + Code
Alright, I'm working in a simple graphics library, graphics.h, which can be downloaded here:
http://www.uniqueness-template.com/devcpp/
I'm currently working on a simple game which required me to be able to have a center point, and rotational value (0-360) and then be able to draw a square that has sides with a length of exactly '5' around the point at the correct rotational value... how would I accomplish this? (I need the formula)
(Separating the points is preferable... I'd like to know where each corner is)
Thanks in advance...
-
What are you using to write this game, openGL, openAL, directx, something else? There isn't a precise "formula" that fits every application, but you need to figure out what angle you are rotated from according to the Z and X axis (looking from the top on a 3D plot). Then rotate that much about the Y axis. For moving the box with the rotation just make the rotational value effect the box also.
-
No no no... I'm not using Open GL or anything like that, open the graphics library in the download and take a look at it, you'll understand it in about 5 seconds... it's really really basic. I'm actually going to need to draw the square line by line, so truely I just need the points based off of the rotation... it's a mathematical formula I want...
Given:
Length between points (5)
Center of the square
Desired rotational value (0-360)
Expected result:
Four points that when connected make a square at the proper rotation.
Thus a rotation of '0' would yeild
_ _
|_|
(The top wouldn't extend over the sides, but you get the idea..
-
graphics.h is an old borland pre-standard header, I dont think many modern compilers support it.
-
Yes it works
It works... I've used it... with Dev-C++ (compiler)
-
In pseudocode you could do it something like:
Code:
rotation = 0;
diagonal_length = sqrt((width * width)+(height * height));
radius = diagonal_length / 2;
x[4]
y[4]
for(int i=0; i<4; i++)
{
x[i] = center_x + (cos(rotation)*radius);
y[i] = center_y + (sin(rotation)*radius);
rotation+=90;
rotation%=360;
}
Then just draw lines between the points. You would also have to convert degrees to radians. But it should give you an idea of how it can be done.