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...