2D point rotation problem

Discuss the development of new homebrew software, tools and libraries.

Moderators: cheriff, TyRaNiD

Post Reply
Art
Posts: 642
Joined: Wed Nov 09, 2005 8:01 am

2D point rotation problem

Post by Art »

Hi Guys,
Well I've done my homework on this one,
so I hope I can find some help.

This program spins a line around a point in the centre of the screen,
like a clock hand moving around a clock face.
The program works,
but as I increment the angle from 0-360, the line makes a full circle many times, and only has about 5 or 6 frames per full circle rotation.
If I increment the angle by 0.01, the display looks great, and movement
is fluid, but it only takes 7 or 8 degrees to make a full circle.

Any ideas what I'm doing wrong?

all vars are ints except for theta & thetab.

Code: Select all

 while (1) {  

clearScreen(0);

xx = 240;            // set the starting point.
yy = 30;
 
drawLineScreen(240, 136, xx, yy, white); // draw stationary line for reference
 
x = xx - 240;                        //  coords X=240,Y=136 is a point in the middle of the screen to spin around
y = yy - 136;
 
theta = theta + 1;                    // increment the angle to 360 degrees.
if (theta > 359) {theta = 0;}
thetab = 360 - theta;
 
sprintf (filler, "              %f", theta); // print the current angle
printTextScreen(0,0,filler, white);
 
xnew = cos(thetab)*x - sin(thetab)*y;
ynew = sin(thetab)*x + cos(thetab)*y;
 
xnew = xnew + 240; // if I leave this out, the line spins around 0,0 coords, and I only see a quater of the action
ynew = ynew + 136;
 
drawLineScreen(240, 136, xnew, ynew, white);
 
sceDisplayWaitVblankStart();
flipScreen();
for&#40;del=0; del<10; del++&#41; &#123;sceDisplayWaitVblankStart&#40;&#41;;&#125; // delay to see the current frame
 &#125;
If not actually, then potentially.
User avatar
Torch
Posts: 825
Joined: Wed May 28, 2008 2:50 am

Post by Torch »

Radians?
Art
Posts: 642
Joined: Wed Nov 09, 2005 8:01 am

Post by Art »

Yes :)
NVM, I forgot the conversion.. came back to delete it ;)
If not actually, then potentially.
marksteven
Posts: 3
Joined: Mon Sep 21, 2009 8:58 pm

Post by marksteven »

Hi,
i can not find error in this code.It is all right.But if you want to see analog clock then here some code,
#include<graphics.h>
#include<conio.h>
#include<math.h>
#include<dos.h>
void main()
{
int gd=DETECT,gm;
int x=320,y=240,r=200,i,h,m,s,thetamin,thetasec;
struct time t;
char n[12][3]={"3","2","1","12","11","10","9","8","7","6","5","4"};
initgraph(&gd,&gm,"f:\arun\tc");\put the directory which contains
egavga.bgi
circle(x,y,210);
setcolor(4);
settextstyle(4,0,5);
for(i=0;i<12;i++)
{
if(i!=3)
outtextxy(x+(r-14)*cos(M_PI/6*i)-10,y-(r-14)*sin(M_PI/6*i)-26,n);
else
outtextxy(x+(r-14)*cos(M_PI/6*i)-20,y-(r-14)*sin(M_PI/6*i)-26,n);
}
gettime(&t);
printf("The current time is: %2d:%02d:%02d.%02d
",t.ti_hour, t.ti_min,
t.ti_sec, t.ti_hund);
while(!kbhit())
{
setcolor(5);
setfillstyle(1,5);
circle(x,y,10);
floodfill(x,y,5);
gettime(&t);
if(t.ti_min!=m)
{
setcolor(0);
line(x,y,x+(r-60)*cos(thetamin*(M_PI/180)),y-(r-60)*sin(thetamin*(M_PI/180
)));
circle(x+(r-80)*cos(thetamin*(M_PI/180)),y-(r-80)*sin(thetamin*(M_PI/180))
,10);
line(x,y,x+(r-110)*cos(M_PI/6*h-((m/2)*(M_PI/180))),y-(r-110)*sin(M_PI/6*h
-((m/2)*(M_PI/180))));
circle(x+(r-130)*cos(M_PI/6*h-((m/2)*(M_PI/180))),y-(r-130)*sin(M_PI/6*h-(
(m/2)*(M_PI/180))),10);
}
if(t.ti_hour>12)
t.ti_hour=t.ti_hour-12;
if(t.ti_hour<4)
h=abs(t.ti_hour-3);
else
h=15-t.ti_hour;
m=t.ti_min;
if(t.ti_min<=15)
thetamin=(15-t.ti_min)*6;
else
thetamin=450-t.ti_min*6;
if(t.ti_sec<=15)
thetasec=(15-t.ti_sec)*6;
else
thetasec=450-t.ti_sec*6;
setcolor(4);
line(x,y,x+(r-110)*cos(M_PI/6*h-((m/2)*(M_PI/180))),y-(r-110)*sin(M_PI/6*h
-((m/2)*(M_PI/180))));
circle(x+(r-130)*cos(M_PI/6*h-((m/2)*(M_PI/180))),y-(r-130)*sin(M_PI/6*h-(
(m/2)*(M_PI/180))),10);
line(x,y,x+(r-60)*cos(thetamin*(M_PI/180)),y-(r-60)*sin(thetamin*(M_PI/180
)));
circle(x+(r-80)*cos(thetamin*(M_PI/180)),y-(r-80)*sin(thetamin*(M_PI/180))
,10);
setcolor(15);
line(x,y,x+(r-70)*cos(thetasec*(M_PI/180)),y-(r-70)*sin(thetasec*(M_PI/180
)));
delay(1000);
setcolor(0);
line(x,y,x+(r-70)*cos(thetasec*(M_PI/180)),y-(r-70)*sin(thetasec*(M_PI/180
)));
}
}



Thanks for sharing this information.
Art
Posts: 642
Joined: Wed Nov 09, 2005 8:01 am

Post by Art »

Yes it dos the same thing.
That clock has been in LUAplayer from the beginning.
If not actually, then potentially.
Post Reply