Codesnippets > Math

SIN and COS, Rotational and Circular Math.

(1/2) > >>

Hemlos:
How to use SIN and COS, for Rotational and Circular Math:
Below you will find examples of Trigonometry math...
...Functions for DRAWCIRCLE(), DRAWDISC(), and DRAWRING()
You will also find the code for a Desktop Clock, which uses internal computer clock.  :good:

Here is an example of  "How to do ROTATE"
In this example we will make a circle out of dots.
You need to make presets for the Center Position X,Y, and Radius.
Then, you need to rotate an angle, so declare a variable RotationAngle .
In order to make a circle, you need to draw it out obviously...
...we are making dots , these will need a 2 variables for both planes in XY
So lets get the program started....first declare variables, then begin the main loop:
--- Code: (glbasic) ---LIMITFPS 60
LOCAL DotX,DotY,RotationAngle //these are variables and dont need to be preset.
X=200 //place the center of circle here
Y=200
Radius=150 // This is the distance from center of circle to be drawn
WHILE TRUE  //start the code

--- End code ---

In order to make a point spin around (X,Y), at Radius distance,
1. You multiply the Radius by the cosine of X, and add the distance X .
Always use COS for X plane.
This will always orient angle 0 to point to the right, to the positive x direction.

2. You multiply the Radius by the sine of Y, and add the distance Y . Always use SIN for Y plane.

Now we're ready to start Rotating the Angle and drawing it out.
FOR, NEXT with a SETPIXEL command will be suitable for this example:

--- Code: ( 0 to 359=360 degrees) ---FOR RotationAngle=0 to 359 //360 degrees
SETPIXEL DotX,DotY,RGB(255,255,255)
NEXT

SHOWSCREEN
WEND

--- End code ---

Notes: you can make presets for individual Radius for both X and Y planes....to make globes etc
Make sure you declare,

//ellipse

And include those into the FOR NEXT statement.

Now we're done, here is the full code of the example above:

--- Code: (glbasic) ---LIMITFPS 60
LOCAL DotX,DotY,RotationAngle //these are variables and dont need to be preset.
X=200 //place the center of circle here
Y=200
Radius=150 // This is the distance from center of circle to be drawn
WHILE TRUE  //start the code

FOR RotationAngle=0 to 359 //360 degrees
SETPIXEL DotX,DotY,RGB(255,255,255)
NEXT

SHOWSCREEN
WEND

--- End code ---

Nice and easy....i have added coded functions to make circles and discs using this math:
To use this function:

--- Code: (DRAWCIRCLE() Function draws a circle with lines) ---
//draws a 36 point circle, using lines
LOCAL Cir,CirXS,CirYS,CirXE,CirYE
FOR Cir=0 TO 359 STEP 10
DRAWLINE CirXS,CirYS,CirXE,CirYE,CircleColor
NEXT
ENDFUNCTION

--- End code ---

and here is a disc using the same exact math, but with polyvectors and images for skin:
To use this function, you need 2 colors, one for the center and one for the outside.
And you can skin it to with BmpSkin="image.bmp"

--- Code: (DRAWDISC() Function creates a filled disc of specified radius) ---

//Function creates a filled disc of specified radius
FOR Cir=0 TO 359 STEP 10
STARTPOLY BmpSkin,0
ENDPOLY
NEXT

ENDFUNCTION

--- End code ---

It is used exactly like the examples above:

--- Code: (DrawRing() Function draws a ring with adjustable width..  inside and outside radius) ---
FOR Cir=0 TO 359 STEP 10
STARTPOLY BmpSkin
POLYVECTOR CirX1,CirY1,CirX1,CirY1,Color1
POLYVECTOR CirX2,CirY2,CirX2,CirY2,Color1
POLYVECTOR CirX3,CirY3,CirX3,CirY3,Color2
POLYVECTOR CirX4,CirY4,CirX4,CirY4,Color2
ENDPOLY
NEXT
ENDFUNCTION

--- End code ---

This is a full screen Analog Clock, it is syncronized with your computers time...can be a great screen saver.
Read the readme in the zipfile, for info on how to make it into a screen saver.

Ive been tinkering...its getting bigger and more colorful.
Render At 800x600 fullscreen.

--- Code: (Main Clock Code) ---// --------------------------------- //
// Project: DesktopClock
// Start: Thursday, August 14, 2008
// IDE Version: 5.341

//compile into fullscreen mode...looks cool
LOCAL sx,sy,h,w
GETSCREENSIZE sx,sy
GETFONTSIZE w,h
LOCAL cx=sx/2
LOCAL cy=sy/2
AUTOPAUSE FALSE
ALPHAMODE -1

WHILE TRUE
SYSTEMPOINTER FALSE
drawwater()
dosfx=dosfx+1
IF dosfx>3 THEN dosfx=0
IF dosfx=0
velx=RND(4)-2
vely=RND(4)-2
cx2=cx
cx=cx2+velx
cy2=cy
cy=cy2+vely
ENDIF
IF cy>400 THEN cy=400
IF cx>600 THEN cx=600
IF cx<200 THEN cx=200
IF cy<200 THEN cy=200
IF fup=0;  fx1=fx1-1; IF fx1<1; fup=1;ENDIF; ENDIF
IF fup=1;  fx1=fx1+1; IF fx1>250; fup=0;ENDIF;ENDIF
sxf(RGB(1,1,1),RGB(-fx1,1,1),cx,cy,170,66)
DRAWRING(RGB(20,20,fx1),RGB(-fx1,20,20),cx,cy,245,195)
DRAWRING(RGB(-fx1,20,20),RGB(20,20,fx1),cx,cy,195,170)
DRAWCIRCLE(RGB(1,1,1),cx,cy,66)
DRAWCIRCLE(RGB(fx1,1,1),cx,cy,200)
DRAWCIRCLE(RGB(1,1,fx1),cx,cy,240)
IF KeyBoardIO()>-1 THEN END

//seconds hand
STARTPOLY -1
//DRAWLINE cx,cy,cx+200*COS(secs*6-90),cy+200*SIN(secs*6-90),RGB(RND(200)+20,RND(200)+20,RND(200)+20) //secs
POLYVECTOR cx+200*COS(secs*6-90),cy+200*SIN(secs*6-90),cx+200*COS(secs*6-90),cy+200*SIN(secs*6-90),RGB(255,255,fx1)
POLYVECTOR cx+10*COS(secs*6-180),cy+10*SIN(secs*6-180),cx+10*COS(secs*6-180),cy+10*SIN(secs*6-180),RGB(1,1,1)
POLYVECTOR cx+10*COS(secs*6),cy+10*SIN(secs*6),cx+10*COS(secs*6),cy+10*SIN(secs*6),RGB(1,1,1)
ENDPOLY

//minutes hand
STARTPOLY -1
//DRAWLINE cx,cy,cx+166*COS(mins*6-90),cy+166*SIN(mins*6-90),RGB(RND(200)+20,0,0)//mins
POLYVECTOR cx+150*COS(mins*6-90),cy+150*SIN(mins*6-90),cx+200*COS(mins*6-90),cy+200*SIN(mins*6-90),RGB(255,255,fx1)
POLYVECTOR cx+10*COS(mins*6-180),cy+10*SIN(mins*6-180),cx+10*COS(mins*6-180),cy+10*SIN(mins*6-180),RGB(1,1,1)
POLYVECTOR cx+10*COS(mins*6),cy+10*SIN(mins*6),cx+10*COS(mins*6),cy+10*SIN(mins*6),RGB(1,1,1)
ENDPOLY

// hours hand
STARTPOLY -1
//DRAWLINE cx,cy,cx+122*COS(hrs*30-90),cy+122*SIN(hrs*30-90),RGB(0,0,RND(200)+20) //hours
POLYVECTOR cx+100*COS(hrs*30-90),cy+100*SIN(hrs*30-90),cx+200*COS(hrs*30-90),cy+200*SIN(hrs*30-90),RGB(255,255,fx1)
POLYVECTOR cx+10*COS(hrs*30-180),cy+10*SIN(hrs*30-180),cx+10*COS(hrs*30-180),cy+10*SIN(hrs*30-180),RGB(1,1,1)
POLYVECTOR cx+10*COS(hrs*30),cy+10*SIN(hrs*30),cx+10*COS(hrs*30),cy+10*SIN(hrs*30),RGB(1,1,1)
ENDPOLY

DRAWDISC(RGB(1,1,1),RGB(255,255,fx1),cx,cy,22)
DRAWCIRCLE(RGB(1,1,1),cx,cy,22)

FOR i= 1 TO 12 //draw little lines and numbers
DRAWLINE cx+200*COS(i*30),cy+200*SIN(i*30),cx+210*COS(i*30),cy+210*SIN(i*30),RGB(1,fx1,1)
PRINT i,cx+225*COS(i*30-90)-8,cy+225*SIN(i*30-90)-h/2-2
NEXT

LET secs1=MID\$(PLATFORMINFO\$("TIME"),18,1)
LET secs2=MID\$(PLATFORMINFO\$("TIME"),17,1)
LET mins1=MID\$(PLATFORMINFO\$("TIME"),15,1)
LET mins2=MID\$(PLATFORMINFO\$("TIME"),14,1)
LET hrs1 =MID\$(PLATFORMINFO\$("TIME"),12,1)
LET hrs2 =MID\$(PLATFORMINFO\$("TIME"),11,1)
secs=secs2*10
secs=secs+secs1
mins=mins2*10
mins=mins+mins1
hrs=hrs2*10
hrs=hrs+hrs1
IF hrs=0 THEN hrs=12
IF hrs>12;
hrs=hrs-12; pm\$="pm"
ELSE
pm\$="am"
ENDIF
PRINT MID\$(PLATFORMINFO\$("TIME"),0,10),2,2+fx1

IF mins<10
PRINT hrs+":0"+mins+" "+pm\$,20,25+fx1
ELSE
PRINT hrs+":"+mins+" "+pm\$,20,25+fx1
ENDIF

//tick=ASC(MID\$(PLATFORMINFO\$("TIME"),18,1))

SHOWSCREEN

WEND

FOR Cir=0 TO 359 STEP 10
STARTPOLY -1
POLYVECTOR CirX1,CirY1,CirX1,CirY1,Col2
POLYVECTOR CirX2,CirY2,CirX2,CirY2,Col2
POLYVECTOR CirX3,CirY3,CirX3,CirY3,Col1
POLYVECTOR CirX4,CirY4,CirX4,CirY4,Col1
ENDPOLY
NEXT

ENDFUNCTION

STATIC sf
FOR Cir=0 TO 359 STEP 20
STARTPOLY -1
sf=sf+1
Cir=Cir+sf
POLYVECTOR CirX1,CirY1,CirX1,CirY1,Col2
POLYVECTOR CirX2,CirY2,CirX2,CirY2,Col2
POLYVECTOR CirX3,CirY3,CirX3,CirY3,Col1
POLYVECTOR CirX4,CirY4,CirX4,CirY4,Col1
ENDPOLY
NEXT
FOR Cir=359 TO 0 STEP 20
STARTPOLY -1
sf=sf-1
Cir=Cir+sf+90
POLYVECTOR CirX1,CirY1,CirX1,CirY1,Col2
POLYVECTOR CirX2,CirY2,CirX2,CirY2,Col2
POLYVECTOR CirX3,CirY3,CirX3,CirY3,Col1
POLYVECTOR CirX4,CirY4,CirX4,CirY4,Col1
ENDPOLY
NEXT
IF sf>360 THEN sf=0
ENDFUNCTION

FOR Cir=0 TO 359 STEP 10
STARTPOLY -1
CirXO=CircleX
CirYO=CircleY
POLYVECTOR CirXS,CirYS,CirXS,CirYS,CircleColor1
POLYVECTOR CirXE,CirYE,CirXE,CirYE,CircleColor1
POLYVECTOR CirXO,CirYO,CirXO,CirYO,CircleColor2
ENDPOLY
NEXT

ENDFUNCTION

//draws a 36 point circle
LOCAL Cir,CirXS,CirYS,CirXE,CirYE
FOR Cir=0 TO 359 STEP 10
DRAWLINE CirXS,CirYS,CirXE,CirYE,CircleColor
NEXT
ENDFUNCTION

// ------------------------------------------------------------- //
// ---  KeyBoardIO  --- detect any key press...0 to 299
// returns instance of key pressed..one time...preventing repeated input
// waits till button is released...then returns that keycode.
// its in this program to check if any key is pressed, and end program.
// ------------------------------------------------------------- //
FUNCTION KeyBoardIO:
LOCAL i
STATIC K,SENDKWAIT,SKB
K=-1
FOR i=0 TO 299
IF KEY(i) THEN K=i
NEXT
IF (K=-1) AND (SENDKWAIT=TRUE); SENDKWAIT=FALSE; RETURN SKB; ENDIF
IF K>-1; SENDKWAIT=TRUE; SKB=K; K=-1; ENDIF
RETURN K
ENDFUNCTION

FUNCTION drawwater:

STATIC z1,z2

FOR y = 0 TO 600 STEP 60
FOR x= 0 TO 800 STEP 80
y1=y-3
STARTPOLY -1
POLYVECTOR x+RND(3)-3,y1+RND(3),x,y,RGB(RND(99),x/9+y/9,x/9+y/9)
POLYVECTOR x+RND(3)-3,y1+60+RND(3)+3,x,y,RGB(RND(99),x/9+y/9,x/7+y/9)
POLYVECTOR x+80+RND(3)+3,y1+60+RND(3)+3,x,y,RGB(RND(99),x/9+y/9,x/9+y/7)
POLYVECTOR x+80+RND(3)+3,y1+RND(3)-3,x,y,RGB(RND(99),x/9+y/9,x/9+y/9)
ENDPOLY
NEXT
NEXT
ENDFUNCTION

--- End code ---

Bonus code from clock program:
Want to detect a key pressed AND Released ? Once?
Reports only once...after key is released...fantastic for user inputs.
This code prevents key press getting reported multiple times.
It only reports back which key is pressed one time, and only after the key is released.
If no keys are pressed then KeyBoardIO() = -1
Check for keys pressed like this:
KeyPressed=KeyBoardIO()
if KeyPressed > -1 then key was pressed and released, and afterwards resets back to value=-1
if KeyPressed = -1 then no keys has been pressed and released

--- Code: (  key detection function ) ---// ------------------------------------------------------------- //
// ---  KeyBoardIO  --- detect any key press...great for user inputs
// waits till button is released...then returns that keycode.
// ------------------------------------------------------------- //
FUNCTION KeyBoardIO:
LOCAL i
STATIC K,SENDKWAIT,SKB
K=-1
FOR i=0 TO 299
IF KEY(i) THEN K=i
NEXT
IF (K=-1) AND (SENDKWAIT=TRUE); SENDKWAIT=FALSE; RETURN SKB; ENDIF
IF K>-1; SENDKWAIT=TRUE; SKB=K; K=-1; ENDIF
RETURN K
ENDFUNCTION

--- End code ---

Schranz0r:
That's awesome  :good:
Nice work!

BumbleBee:
It looks and works very good, Hemlos. :good: What a pity. It's just a PC clock. Cause my wife needs a new kitchen clock. The old one (i mean the clock, not my wife) :) stopped working last week.

Cheers

acristo:
brilliant !!

Hemlos: