Displaying an analog clock

Discussion specific to NXT-G, NXC, NBC, RobotC, Lejos, and more.
Post Reply
mattallen37
Posts: 1818
Joined: 02 Oct 2010, 02:19
Location: Michigan USA
Contact:

Displaying an analog clock

Post 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?
Matt
http://mattallen37.wordpress.com/

I'm all for gun control... that's why I use both hands when shooting ;)
mattallen37
Posts: 1818
Joined: 02 Oct 2010, 02:19
Location: Michigan USA
Contact:

Re: Displaying an analog clock

Post 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.
Matt
http://mattallen37.wordpress.com/

I'm all for gun control... that's why I use both hands when shooting ;)
nxtboy
Posts: 29
Joined: 29 Sep 2010, 18:15
Location: Cambridge, UK
Contact:

Re: Displaying an analog clock

Post 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));
mattallen37
Posts: 1818
Joined: 02 Oct 2010, 02:19
Location: Michigan USA
Contact:

Re: Displaying an analog clock

Post 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.
Matt
http://mattallen37.wordpress.com/

I'm all for gun control... that's why I use both hands when shooting ;)
muntoo
Posts: 834
Joined: 01 Oct 2010, 02:54
Location: Your Worst Nightmare
Contact:

Re: Displaying an analog clock

Post 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);
}
Last edited by muntoo on 19 Dec 2010, 02:16, edited 1 time in total.
Image

Commit to LEGO Mindstorms Robotics Stack Exchange:
bit.ly/MindstormsSE


Commit to LEGO Stack Exchange: bit.ly/Area51LEGOcommit
m-goldberg
Posts: 73
Joined: 29 Sep 2010, 12:05

Re: Displaying an analog clock

Post by m-goldberg »

Muntoo, you meant to write "Temp.Y = sind(deg) * radius;" not "Temp.Y = sin(deg) * radius;", right?
Regards, Morton
muntoo
Posts: 834
Joined: 01 Oct 2010, 02:54
Location: Your Worst Nightmare
Contact:

Re: Displaying an analog clock

Post 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?
Image

Commit to LEGO Mindstorms Robotics Stack Exchange:
bit.ly/MindstormsSE


Commit to LEGO Stack Exchange: bit.ly/Area51LEGOcommit
mightor
Site Admin
Posts: 1079
Joined: 25 Sep 2010, 15:02
Location: Rotterdam, Netherlands
Contact:

Re: Displaying an analog clock

Post by mightor »

muntoo wrote:Shhh! Only the Secret Order Of The Volcano is supposed to know that, remember?
We are always watching...
| My Blog: I'd Rather Be Building Robots (http://botbench.com)
| RobotC 3rd Party Driver Suite: (http://rdpartyrobotcdr.sourceforge.net)
| Some people, when confronted with a problem, think, "I know, I'll use threads,"
| and then two they hav erpoblesms. (@nedbat)
Post Reply

Who is online

Users browsing this forum: Semrush [Bot] and 26 guests