An Analogue Clock in Processing

I came across some code I wrote in Processing a while ago so I thought Iâ€™d post bits of it here. I made a really simple analogue clock. Below is s screenshot of what I came up with.

I wrote a small function that handles drawing a hand of the clock. This function is called for each hand of the code by the draw loop.

```void draw_hand(long count, long modulo, long len)
{
float nx, ny;
a = (2*PI/modulo) * count - (2*PI/4);
nx = (width/2) + (Clock_Size/2-len) * cos(a);
ny = (height/2) + (Clock_Size/2-len) * sin(a);
strokeWeight(7);
line(width/2, height/2, nx, ny);
}```

Then all we need to do in the draw loop is call the function for each of the hands of the clock. Here is an example of drawing the hours hand.

To put the ticks around the outside of the clock I used the code below.

```  fill(0,0,0);
for( int i = 0; i < 12; i ++ ) {
float angle = radians(i*30) - (2*PI/4);
nx = (width/2) + (Clock_Size/2-35) * cos(angle);
ny = (height/2) + (Clock_Size/2-35) * sin(angle);
text(i == 0 ? 12 : i, nx, ny);
}```

Processing.js Version

I have got this code working fine using Processing.js. See it here

Paying with Processing (Part Two)

Last time I ran an example sketch and then wrote my own that prints â€śHello Worldâ€ť in bright colours. Now I want to play around with some of the basic shapes that Processing provides. From the documentation it looks like the 2D primitives are the arc, ellipse, line, point, quad, rect, and triangle.

The first one Iâ€™m going to play with is theÂ rect() primitive, so I wrote a simple sketch to draw one. All it does is set the window size then draw a rectangle somewhere in the window.

```void setup() {
size(640, 480);
smooth();
rect(30, 30, 155, 155);
}```

TheÂ rect() function takes four parameters as inputs, the first two are the x and y co-ordinates of where to start drawing. The second two are the height and width of the rectangle. Then I thought it would be cool if I made it follow the mouse, this as it turns out is really easy. In the draw function you get a couple of variables you can use for this, mouseX and mouseY. They hold the x and y co-ordinates of where the mouse is so all I have to do is put the call toÂ rect() into the draw loop then use the mouse co-ordinatesÂ to set that starting position of the rectangle.

`rect(mouseX, mouseY, 155, 155);`

This code worked ok but the viewport filled up with loads of rectangles as you moved the mouse really quickly. I wanted just a single rectangle that followed the mouse. So I added some extra lines of code that drew another rectangle over the whole viewport to clear it then draw the smaller rectangle. I also thought it would look much nicer if the mouse cursor was always located in the centre of the rectangle rather than at the top left-hand corner. So to do this I subtracted half the width of the rectangle from the x co-ordinate or the mouse cursor, and did the same with the height and y co-ordinate. This kept the mouse cursor roughly in the middle of the rectangle.

To draw the rectangle over the entire viewport I two other special variables, width and height. These are set to the width and height of the window you are using. The code for all of this is below.

```void setup() {
size(640, 480);
smooth();
}

void draw() {
fill(128,128,128);
rect(0, 0, width, height);
fill(0, 0, 255);
rect(mouseX-(155/2), mouseY-(155/2), 155, 155);
}```

Well thatâ€™s all for now. See ya!

Playing with Processing (Part One)

After a while playing with the Arduino Iâ€™ve become interested in Processing which is what the Arduino language and Arduino IDE is based on. Just as the Arduino is a thin layer on top of C, Processing is simply a library for Java that makes playing with computer graphics much easier. So today Iâ€™ve decided to play around with it a bit and write about it.

First of all I downloaded the Processing IDE from theÂ Processing website, I downloaded the Linux version as Iâ€™m using Ubuntu but you can get a Windows or Mac OSX version as well. Then I decompressed the files and fired up the IDE by executing the â€śprocessingâ€ť file, I was then greeted by the IDE.

Looks good eh?? It looks almost idendical to the Arduino IDE. I then has a quick read through some of the documentation on the website. The first code snippet I ran was from Processing own tutorial, although I left a small bit of code out. The code for it is below, what it does is draw a circleÂ whereverÂ the mouse cursor currently is.

```void setup() {
size(560, 160);
smooth();
}

void draw() {
ellipse(mouseX, mouseY, 80, 80);
}```

Yay, it worked!! I love how exciting it is to get instant feedback. I wasnâ€™t sure exactly what the smooth() function did,

I suspected it may have beenÂ somethingÂ to do with anti-aliasing and it turns out I was right after looking at the reference. As you can see from the screen shot it can be used to make pretty snaking patterns.

I decided that I wanted to create a sketch that printed â€śHello Worldâ€ť because thatâ€™s the first program most people write :-). I had a quick look around theÂ documentationÂ on the website and I found theÂ text() function and some example code. I typed that into the IDE but I wanted to use a bigger font, it turns out the font file must be in the /data directory of the sketch.

There is a tool provided that will take a font on your computer and turn it into something Processing can use. To use this tool, go to the â€śToolsâ€ť menu and select â€śCreate Fontâ€¦â€ť and then in the dialog box that pops up select the font you want and the filename then click OK.

```void setup() {
size(640, 160);
background(254, 153, 0);
}

void draw() {
PFont font = loadFont("Verdana-Bold-48.vlw");
textFont(font);
fill(0, 105, 253);
text("Hello World", 15, 88);
}```

Clicking the run button produces a large window with â€śHello Worldâ€ť written in large letters. It worked! Yay!!! So far itâ€™s been pretty fun playing around with Processing, I hope to do a lot more with it. I want to use it to talk to the Arduino. Perhaps plotting the voltage on the analogue pins over time.

Thatâ€™s it for now, I hope to have another post on this later in the week. See ya!