Wednesday 6 May 2009

I know it's a bit late but I have finally got round to developing a program for my chunk.

The listing is below and I'll post a commentary later.

The purpose of the program is to demonstrate the main language features covered in the early part of the book. To do this I have animated a ball moving across the screen 'underneath' a fixed light source which causes a highlight on the ball. The position of the highlight on the ball changes as the ball's position changes in relation to the position of the light source.



//variables to define ball
//full size of ball
float ballRadius = 50;

//radius of highlit area
float hLRadius = ballRadius;

//centre of ball will move across scren during animation
float ballCentreX = 0 - ballRadius;
float ballCentreY = 200;

//to control horizontal and vertical movement of the ball
int hMove = 2;
int vMove = 2;

//to draw the highlight relative to the centre of the ball
float hLCentreX;
float hLCentreY;
float hLXAdj;
float hLYAdj;
float hLSize = ballRadius / 3;

//to control the position of the highlight relative to
//the ball's position on the screen and the light source
float hLPosX;
float hLPosY;

//to colour the ball
float r = 60;
float b = r;
float g = r;

//variables defining the position of the light source
float lightSrcX = 200;
float lightSrcY = 200;
float lightSrcHt;

void setup()
{
size(400,400);
background(40, 10, 10);
noStroke();
frameRate(25);
lightSrcHt = width / 2;
}

void draw()
{
//clear drawing area
background(40, 10, 10);

//move ball across screen by changing X-coordinate

//of centre
ballCentreX += hMove;
ballCentreY += vMove;

//detect collision with top or bottom of screen an 'bounce' if required
if ((ballCentreY + ballRadius == height) || (ballCentreY == ballRadius))
vMove *= -1;

//start again if ball moves of the side of the screen
if(ballCentreX >= width + ballRadius)
ballCentreX = 0 - ballRadius;

//position the highlight
hLCentreX = ballCentreX;
hLCentreY = ballCentreY;
hLRadius = ballRadius;

//re-set colours
r = 60;
g = 0;
b = 0;

/**
* This section uses trigonometry to calculate the position of the highlight
* relative to the positions of the ball and the light source.
*/

//these variables are used to do make the calulation a little clearer
//first calculate the x-position of the highlight
float p = lightSrcX - ballCentreX;
float k = lightSrcHt;
float rad = ballRadius;
float a; //angle the light is shining on the ball
float m; // this is the figure we're looking for

// perform calculation
a = atan(k/p);
m = (int)(rad * cos(a));

//the value calculated for m is set as the position of the highlight
if(ballCentreX < hlposx =" ballCentreX" hlposx =" ballCentreX" hlxadj =" (hLPosX" p =" lightSrcY" a =" atan(k/p);" m =" (int)(rad" hlposy =" ballCentreY" hlposy =" ballCentreY" hlyadj =" (hLPosY"> 0)
{
fill(r, g, b);
ellipse(hLCentreX, hLCentreY, hLRadius * 2, hLRadius * 2);

r += 255/ballRadius;
if(hLRadius <= hLSize)
{
g += 255/hLSize;
b += 255/hLSize;
} hLRadius -= 1;

hLCentreX += hLXAdj;
hLCentreY += hLYAdj;
}
}

Wednesday 7 January 2009

Fragment Choice

I've asked for and been assigned Chunk 19 which is mainly a description of a program using a number of the features introduced in the early parts of Greenberg. With new courses starting soon I'm hoping to get at least the first 500 words up within the next fortnight or so. Until then...

Thursday 13 November 2008

At Last

The book arrived over a week ago but I've been trying to fit a new kitchen since then so there has been no opportunity to make a start. The kitchen is now finished, however, and I have had chance to read the first couple of chapters. Although this hasn't left me much the wiser as to the capabilities of Processing, nor closer to making a choice as to which chunk I'd like to write, I have to say the book at least seems promising as I like Greenberg's writing style and he seems genuinely enthusiastic about the subject.

I plan to get as far into the book as possible before reaching a decision about my choice of chunk - but obviously if all the intersesting stuff starts to disappear I may have to revise this plan.

As well as making a start on the book, I have also downloded the software and been through some of the tutorials on the Processing website. All I can say for the moment is that it seems quite interesting and that I look forward to delving deeper. I am especially interested in how it compares to Flash which I have used quite a bit in the past.

Monday 27 October 2008

Introduction

My name is Neil Petrie and I'm a 41 year old computing student with the Open University. I've just finished what I hope was my penultimate year of a BSc Computing degree, with only the Computing Project and a course on AI to go in 2009.

I live in West Sussex with my wife, Lesley, and two dogs, Ruby & Java. (I know - but honestly it was a cooincidence; Ruby came with the name already installed.) I'm a free-lance web designer and part-time dog trainer although you'd never guess it if you'd met mine.

In my free time I read lots of books of most genres, walk the W. Sussex countryside, listen to (mostly) loud music, play computer games, watch football, rugby, cricket & motor cycling (Arsenal, Gloucester, Sussex and Hayden if you're interested) and perform poorly executed DIY.

This blog will form part of my contribution to the Mass Writing Project being run by Darrel Ince of the OU. I've no idea how things are going to go as I have never been involved in anything like this before and also have never written a blog before.

So, here goes...