Fundamentals of Programming

9
FUNDAMENTALS OF PROGRAMMING SM1204 SEMESTER A 2012

description

Fundamentals of Programming. SM1204 Semester A 2012. Due date 27 NOV 2012 Collection via ACS. Assignment 2. Requirements. Design and simulate set of objects (20%) e.g. germs, ants, cars, Use of "for" statements and “array” data structures (20%) With interaction among objects (30%) - PowerPoint PPT Presentation

Transcript of Fundamentals of Programming

Page 1: Fundamentals of Programming

FUNDAMENTALSOF PROGRAMMING

SM1204 SEMESTER A 2012

Page 2: Fundamentals of Programming

ASSIGNMENT 2

DUE DATE 27 NOV 2012

COLLECTION VIA ACS

Page 3: Fundamentals of Programming

REQUIREMENTS

oDesign and simulate set of objects (20%)oe.g. germs, ants, cars,

oUse of "for" statements and “array” data structures (20%)

oWith interaction among objects (30%)

oCreative object behaviors (30%)oNote that graphic is not important in this assignment

Page 4: Fundamentals of Programming

EXAMPLE

http://processing.org/learning/topics/flocking.html

Page 5: Fundamentals of Programming

EXAMPLE – SIMPLE GERMS

int n = 80;

float ballSize = 5;

float[] x = new float[n];

float[] y = new float[n];

float[] sx = new float[n];

float[] sy = new float[n];

void setup() {

size(200, 200);

smooth();

for (int i=0; i<n; i++) {

x[i] = random(10, width-10);

y[i] = random(10, height-10);

sx[i] = random(-1, 1);

sy[i] = random(-1, 1);

}

}

Define and initialization data (arrays)

Page 6: Fundamentals of Programming

EXAMPLE – SIMPLE GERMS

void move()

{

for (int i=0; i<n; i++) {

x[i] += sx[i];

y[i] += sy[i];

if (x[i] < 0) { x[i] = 0; sx[i] = -sx[i]; }

if (x[i] > width) { x[i] = width; sx[i] = -sx[i]; }

if (y[i] < 0) { y[i] = 0; sy[i] = -sy[i]; }

if (y[i] > height) { y[i] = height; sy[i] = -sy[i];}

}

}

How objects move

Page 7: Fundamentals of Programming

EXAMPLE – SIMPLE GERMS

void draw() {

background(200);

for (int i=0; i<n; i++) {

for (int j=0; j<n; j++) {

float d = dist(x[i], y[i], x[j], y[j]);

if (d < 30) {

float c = map(d, 0, 30, 0, 200);

stroke(c);

line(x[i], y[i], x[j], y[j]);

}

}

}

stroke(0); fill(255);

for (int i=0; i<n; i++) {

ellipse(x[i], y[i], ballSize, ballSize);

}

move();

}

How objects display

Page 8: Fundamentals of Programming

EXAMPLE – SIMPLE GERMS

for (int i=0; i<n; i++) {

for (int j=0; j<n; j++) {

if (i != j) {

float dx = 1 / (x[i] - x[j]);

float dy = 1 / (y[i] - y[j]);

dx = constrain(dx, -0.9, +0.9);

dy = constrain(dy, -0.9, +0.9);

sx[i] += dx;

sy[i] += dy;

sx[i] = constrain(sx[i], -3, +3);

sy[i] = constrain(sy[i], -3, +3);

}

}

}

How objects interact with each other (insert this to function move() )

Page 9: Fundamentals of Programming

USEFUL FUNCTIONS & REFERENCES

omap (value, low1, high1, low2, high2)

oRe-maps a number from one range to another

oconstrain(value, min, max)

oConstrains a value to not exceed a maximum and minimum value.

oabs(value)

oCalculates the absolute value (always position) of a number.