Original Post
Hi I'm trying to rotate a circle, well it's a circle made out of circles that I'm creating programatically. For some reasone when I rotate my circle it gets all distorted(streached flattened) but I can't figure out why?? What I want is for the circle to rotate without loosing its shape.
I Wrote this program using Allegro...
here's the source sorry if it's confusing I'm just hacking on this program to try to learn stuff.
I put a //NOTE comment at the part I have a question about so you can find it quickly I just thought I'd include the entire code incase someone wanted to compile it keep in mind you'll need allegro if you want to compile it.
#include <stdio.h>
#include <math.h>
#include "allegro.h"
volatile int frame_count, fps;
void fps_proc(void)
{
fps = frame_count;
frame_count = 0;
}
END_OF_FUNCTION(fps_proc);
int main(void)
{
#define PI 3.14159265358979
BITMAP *dblbuffer;
int x;
int i = 0;
int theta = 0;
int plotx, ploty;
int rotate = 0;
char buffer[20];
char score_buf[80];
frame_count = fps = 0;
allegro_init();
install_keyboard();
install_timer(); /* needed for `rest' function */
if (set_gfx_mode(GFX_AUTODETECT, 1280, 1024, 0, 0) < 0) {
//printf("%s\n", allegro_error);
exit(1);
}
clear(screen);
/* set up the interrupt routines... */
LOCK_VARIABLE(frame_count);
LOCK_VARIABLE(fps);
LOCK_FUNCTION(fps_proc);
install_int(fps_proc, 1000);
dblbuffer = create_bitmap(SCREEN_W, SCREEN_H);
clear(dblbuffer);
vsync();
blit(dblbuffer, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H);
do{
poll_keyboard();
if(key[KEY_ESC])
break;
for (x = 0; x < 360; x++) {
//rest(2); /* slow it down so we can see it */
//circlefill(dblbuffer, x - 1, 100, 50, 0); /* erase from last place */
//circlefill(dblbuffer, x, 450+100*( sin((x+y*90)*PI/180)) , 10, x%30 + 32 ); //sin
//circlefill(dblbuffer, x, 450+50*( tan((x+y*90)*PI/180)) , 10, x%30 + 32 );//tan
//putpixel(dblbuffer,x,450+100*( sin((x+y*90)*PI/180)),x%30 + 32);
//calculate x, y for circle elements
plotx = 300* cos(x*PI/180);
ploty = 300* sin(x*PI/180);
//rotate x, y
if(rotate > 0)
{
//NOTE This is the part I'm having trouble with!!
plotx = (plotx * cos(theta*PI/180) - ploty * sin(theta*PI/180));
ploty = (ploty *cos(theta*PI/180) + plotx*sin(theta*PI/180));
}
//shift
plotx = plotx + SCREEN_W / 2;
ploty = ploty + SCREEN_H / 2;
circlefill(dblbuffer, plotx, ploty , 10, x%30 + 32 ); //sin
poll_keyboard();
if(key[KEY_R])
rotate = 1;
//textout(dblbuffer, font, "Double buffering (mode 13h)", 0, 0, 15);
}
if (fps)
sprintf(score_buf, "(%ld fps)",(long)fps);
else
sprintf(score_buf, "0");
sprintf(buffer,"theta: %d degrees", theta);
textout(dblbuffer, font, score_buf, 0, 0, 7);
textout(dblbuffer, font, "Press R to begin Rotation", 1015, 0, 7);
textout(dblbuffer,font,buffer,0,20,7);
theta++;
if(theta > 360)
theta = 1;
//vsync();
//readkey();
blit(dblbuffer, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H);
frame_count++;
clear(dblbuffer);
} while (!key[KEY_ESC]);
// Now, remember to FREE the memory you previously allocated
destroy_bitmap(dblbuffer);
return 0;
}
END_OF_MAIN();
I'm looking for a solution that will allow me to rotate any graph of points. when i posted this on the allegro.cc forum I was told:
quote:I understand that by adding theta to x it makes the circle rotate but that isn't neccisarilly a solution for rotating other types of graphs. Say you wanted to graph y = 100*(sinx) or something like that if you put, ploty = 100*(sin(x+theta*PI/180)); then you would see the sine graph phase shifting correct? I tried nearly the same program as the original source listed with the graph of a sine wave and it also got all distoreted as I tried to rotate it. What I'd like to figure out is a way to manipulate the x and y values of the points on a graph to rotate it. Thanks so much for any help with this. -Jake [edited by - jakerrz on January 10, 2004 3:14:56 AM]
Try replacing the above with just: //calculate x, y for circle elements plotx = 300* cos((x+theta)*PI/180); ploty = 300* sin((x+theta)*PI/180);