Sunday 29 March 2009

First Chunk done at last !

Since November I've been getting my M801 Dissertation finished. At last that is done-and-dusted !

However, I haven't been idle, and have given much thought to the project.

So now I have the first part of my Chunk (50) written, and here it is :

CHUNK 50
TITLE Generating a simple curve


In the previious section you was able to see how a graph of distance against time could be represented with a curve.

Also, you saw that for a given constant speed (i.e. zero acceleration), a straight line is drawn, because

In other words the slope of the line determines the speed.

In many cases speed is not constant and so a curve will be drawn instead. This can be interpreted as a line with a varying slope.



But how do we draw lines with varying slope in the Processing Environment ?

First, we need a function to describe the shape of the curve. One frequently used case, is when a ball or other object is dropped from a height. We can describe the speed of the object in terms of the amount of time elapsed from dropping the object approximately as follows :

Where 's' is the number of seconds since dropping the object and '9.81' is the constant acceleration due to the gravitational pull of the earth. This is a form of constant aceeleration in one dimension.

Lets develop a program to draw this graph



First, we need an anvironment to draw in :

size(300,300);

background(255);

int margin = height / 15;

strokeWeight(3);

smooth();



Now define the x an y values for plotting the points

float x = 0;

float y = 0;



We need a constant to represent the acceleration due to gravity

float g = 9.81;



This constant we will use to increment the x (time) value.

float timeInc = 1;



We need to scale the x-points according to the size of the window. This constant calculates the maximum valkue of x that we can expect during the execution of the program.

float xMax = width/sqrt(width / g);



Now we are ready to run the program. A 'whjile' loop is used to calculate values for y, as long as the current value will fit into the window.



while(y

Work out where the x point is in terms of time:

float xPoint = xMax * x;

Tell us what is going on (for information only)

System.out.println("x=" + x + ", y=" + y + ", xPoint=" + xPoint);

Draw the point on the graph

point(xPoint+margin, y+margin);

Increment time by our required factor. Calculate the next value of y , and starty again

x += timeInc;

y = g * x*x;

}



Put together, this program looks like this :

// Set the environment

size(300,300);

background(255);

int margin = height / 15;

strokeWeight(3);

smooth();

// Set constants

float x = 0;

float y = 0;

float g = 9.81;

float timeInc = 1;

float xMax = width/sqrt(width / g);

// Run the loop

while(y

float xPoint = xMax * x;

System.out.println("x=" + x + ", y=" + y + ", xPoint=" + xPoint);

point(xPoint+margin, y+margin);

x += timeInc;

y = g * x*x;

}


Using the code above, I obtained the following output:












You can see that five points were plotted representing the time values of 0 -4 seconds after release of the object, in one second increments.

You should be able to easily change this program to show the curve as below :












Hint : look at the value of timeInc