Page 1 of 1

Displaying an analog clock

Posted: 17 Dec 2010, 20:16
by mattallen37
How do you display an analog clock on the NXT, using NXC? I know how to use the draw circle function, and I know how to draw lines, but how do I draw a line that starts at the center of the circle, and goes to one of 360 positions (or however many), without entering all the values manually? What math function do I need to perform?

Re: Displaying an analog clock

Posted: 17 Dec 2010, 20:18
by mattallen37
Actually, I think what I want, is to know the math that is used to write a 2D array of values, used to plot a circle.

Re: Displaying an analog clock

Posted: 17 Dec 2010, 21:41
by nxtboy
Is this within a RIC file?

If not, just use trigonometry!

I'm a getting bit shaky with my NXC now, so here's some pseudo-code:

Code: Select all

int xCenter = 50;
int yCenter = 32;
int radius = 20;
float angle = Pi/6 * hour;
drawLine(xCenter, yCenter, xCenter + radius * sin(angle), yCenter + radius * cos(angle));

Re: Displaying an analog clock

Posted: 17 Dec 2010, 21:58
by mattallen37
Well, I can't say as though I know much of any trigonometry. Thanks a lot, that seems to be almost exactly what I want.

Re: Displaying an analog clock

Posted: 18 Dec 2010, 02:11
by muntoo
mattallen37 wrote:Actually, I think what I want, is to know the math that is used to write a 2D array of values, used to plot a circle.
It's a pretty "simple" algorithm, actually. It uses the Pythagorean Theorem, which I expect you've heard of. It's called the Midpoint Circle Algorithm which uses a similar method to Bresenhan's Line Algorithm. It's very fast for non anti-aliased graphics (aka monochrome display).

I've written a few versions for NXC (including stealing Ellipse code [when I couldn't find what I was looking for] off the EF source ;) ), but I'm sure you don't really want these.

What you want is this:

Code: Select all

inline LocationType GetCircleVectorOfTerriblyLongNamesForSimpleFunctionsDeg(float deg, float radius)
{
    LocationType ltTemp;
    ltTemp.X = cosd(deg) * radius;
    ltTemp.Y = sind(deg) * radius;
    return(ltTemp);
}

Re: Displaying an analog clock

Posted: 18 Dec 2010, 02:37
by m-goldberg
Muntoo, you meant to write "Temp.Y = sind(deg) * radius;" not "Temp.Y = sin(deg) * radius;", right?

Re: Displaying an analog clock

Posted: 19 Dec 2010, 02:17
by muntoo
m-goldberg wrote:Muntoo, you meant to write "Temp.Y = sind(deg) * radius;" not "Temp.Y = sin(deg) * radius;", right?
Shhh! Only the Secret Order Of The Volcano is supposed to know that, remember?

Re: Displaying an analog clock

Posted: 19 Dec 2010, 07:24
by mightor
muntoo wrote:Shhh! Only the Secret Order Of The Volcano is supposed to know that, remember?
We are always watching...