Set your presentation theme:
Black (default) -
White -
League -
Sky -
Beige -
Simple
Serif -
Blood -
Night -
Moon -
Solarized
H:
by Jean Pierre Charalambos
Universidad Nacional de Colombia
Presentation best seen online
See also the source code
H:
- Program paradigms (brief overview)
- Using an object
- Declaring an object
- Initializing an object
- Calling object methods
H:
- Program paradigms brief overview
- Object Oriented Programming (OPP) Paradigm
V:
A fundamental style of computer programming, serving as a way of building the programs
more here
V:
- Machine code
- Asembly language
- Structured Programming (SP is detailed here)
- Object Oriented Programming (OOP)
- Other paradigms
V:
Langs that described programs as a group of mutually interactive objects
H:
Consider the problem of visually representing the Fibonacci sequence as a tile of squares with different hue values
V:
An SP design would require:
- Data (Global Variables)
- Fibonacci terms (
squares
) - Hue value
- Setup
- Initialize hue value
- Draw:
- for_each square: compute_color, draw
V:
SP implementation
// 1. Global variables
int squares;
color hue;
int fibonacci(int n) {
if(n == 1)
return 0;
if(n == 2)
return 1;
if( n > 2)
return fibonacci(n-2) + fibonacci(n-1);
return -1;
}
void setup() {
size(720,640);
colorMode(HSB, 360, 100, 100);
squares = 5;
// 2. Initialize data
hue = 90;
noLoop();
}
// 3. Implement the functionality
void draw() {
int square_width = width / squares;
for(int i = 0; i < squares; i++) {
fill(hue, 100, map(fibonacci(i+1), 0, fibonacci(squares), 0, 100));
rect(i*square_width,0,square_width,50);
}
}
V:
V:
In our case an OOP design would thus require:
- Data (Global Variables) Fibonacci object
- Setup Initialize the Fibonacci object
- Draw Display the Fibonacci object
V:
...something like this:
// Step 1. Declare an object
Fibonacci sequence;
void setup() {
size(720,640);
colorMode(HSB, 360, 100, 100);
// Step 2. Initialize object
sequence = new Fibonacci();
noLoop();
}
void draw() {
// Step 3. Call methods on the object
sequence.display(10);
}
V:
where the Fibonacci object may be implemented as:
class Fibonacci {
color hue = 90;
int compute(int n) {
if (n == 1)
return 0;
if (n == 2)
return 1;
if ( n > 2)
return compute(n-2) + compute(n-1);
return -1;
}
void display(int terms) {
int square_width = width / terms;
for (int i = 0; i < terms; i++) {
fill(hue, 100, map(compute(i+1), 0, compute(terms), 0, 100));
rect(i*square_width, 0, square_width, 50);
}
}
}
V:
in p5.js it looks like this:
H:
Remember how it's done with primitive data types
int var;
V:
in OOP it's done similarly:
Fibonacci sequence;
H:
Remember how it's done with primitive data types
var = 10;
V:
in OOP it's done with an 'object constructor'
sequence = new Fibonacci();
The constructor that takes no arguments is known as the default constructor. They don't require an explicit implementation that others do.
V:
Suppose now that we want our Fibonacci visual representation to change its placement according to our mouse y-position
V:
We could declare a new Fibonacci attribute to represent its visual placement:
int yPos;
V:
and two methods to set/get its value:
void setHeight(int h) {
yPos = h;
}
int height() {
return yPos;
}
V:
finally, we can also implement a non-default constructor to set out the hue()
attribute:
Fibonacci(int h) {
setHue(h);
}
V:
our global data, setup()
and draw()
global methods will now look like:
Fibonacci sequence;
void setup() {
size(720,640);
colorMode(HSB, 360, 100, 100);
// Note here the new non-default constructor call
sequence = new Fibonacci(120);
}
void draw() {
background(0);
sequence.setHeight(mouseY);
sequence.display(10);
}
V:
an our Fibonacci
implementation like this:
class Fibonacci {
color hue;
int yPos;
Fibonacci(int h) {
setHue(h);
}
void setHeight(int h) {
yPos = h;
}
int height() {
return yPos;
}
void setHue(color h) {
hue = h;
}
color hue() {
return hue;
}
int compute(int n) {
if (n == 1)
return 0;
if (n == 2)
return 1;
if ( n > 2)
return compute(n-2) + compute(n-1);
return -1;
}
void display(int terms) {
int square_width = width / terms;
for (int i = 0; i < terms; i++) {
fill(hue, 100, map(compute(i+1), 0, compute(terms), 0, 100));
// note that we now pass our height() attribute
// as a parameter type to the Processing rect() method
rect(i*square_width, height(), square_width, 50);
}
}
}
H:
Functions are called with the "dot syntax", like this:
sequence.setHeight(mouseY);
sequence.display(10);
H: