Skip to content

Changes to functions examples #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
151 changes: 76 additions & 75 deletions 08_Functions/conversion_function/conversion_function.pde
Original file line number Diff line number Diff line change
@@ -1,75 +1,76 @@
// Conversion Function
// The Coding Train / Daniel Shiffman
// Processing Intro Series

float measurement, footLength, meterLength;
float minNum = 20; //min measurement
float maxNum = 330; //max measurement

void setup() {
size(640, 360);
measurement = selectNum();
}

void mousePressed() {
measurement = selectNum();
}

void draw() {
background(240);
//Position of lines
float x1 = width/6;
float x2 = width - width/6;
float footLineY = height/4; //top line
float meterLineY = height - height/4; //bottom line

strokeWeight(80);
/*blendMode() blends the pixels in the display window according to a defined mode.
blendMode(BLEND) is the default. */

//Gray lines
blendMode(BLEND);
stroke(230);
line(x1, footLineY, x2, footLineY);
line(x1, meterLineY, x2, meterLineY);

//Black lines (update with every new measurement)
stroke(0);

/*lerp() calculates a number between two numbers at a specific increment.
It is convenient for creating motion along a straight path.
Try un-commenting the alternative to see how the line length changes from
a gradual motion to instantaneous update.*/
footLength = lerp(footLength, measurement, 0.05);
meterLength = lerp(meterLength, footToMeter(measurement), 0.05);

//----ALTERNATIVE-----
//footLength = measurement;
//meterLength = footToMeter(measurement);

line(x1, footLineY, x1 + footLength, footLineY);
line(x1, meterLineY, x1 + meterLength, meterLineY);



textSize(14);
fill(255);
/*blendMode(DIFFERENCE) subtracts colors from the underlying image/shape so
when the black line is over the text, the text is white. Otherwise,
the text is black (legibility). */
blendMode(DIFFERENCE);
textAlign(CENTER, CENTER);

//Use the round() function to round to the nearest foot/meter.
text(round(footToMeter(measurement)) + " METERS", width/2, meterLineY);
text(round(measurement) + " FEET", width/2, footLineY);
text("FOR APPROXIMATE LENGTH, DIVIDE BY 3.281", width/2, height/2);
}

float selectNum() {
return (random(minNum, maxNum));
}

float footToMeter(float ft) {
return ft/3.281;
}
// Conversion Function
// The Coding Train / Daniel Shiffman
// Processing Intro Series

float measurement, footLength, meterLength;
float minNum = 20; //min measurement
float maxNum = 330; //max measurement

void setup() {
size(640, 360);
pixelDensity(2);
measurement = selectNum();
}

void mousePressed() {
measurement = selectNum();
}

void draw() {
background(240);
//Position of lines
float x1 = width/6;
float x2 = width - width/6;
float footLineY = height/4; //top line
float meterLineY = height - height/4; //bottom line

strokeWeight(80);
/*blendMode() blends the pixels in the display window according to a defined mode.
blendMode(BLEND) is the default. */

//Gray lines
blendMode(BLEND);
stroke(230);
line(x1, footLineY, x2, footLineY);
line(x1, meterLineY, x2, meterLineY);

//Black lines (update with every new measurement)
stroke(0);

/*lerp() calculates a number between two numbers at a specific increment.
It is convenient for creating motion along a straight path.
Try un-commenting the alternative to see how the line length changes from
a gradual motion to instantaneous update.*/
footLength = lerp(footLength, measurement, 0.05);
meterLength = lerp(meterLength, footToMeter(measurement), 0.05);

//----ALTERNATIVE-----
//footLength = measurement;
//meterLength = footToMeter(measurement);

line(x1, footLineY, x1 + footLength, footLineY);
line(x1, meterLineY, x1 + meterLength, meterLineY);



textSize(14);
fill(255);
/*blendMode(DIFFERENCE) subtracts colors from the underlying image/shape so
when the black line is over the text, the text is white. Otherwise,
the text is black (legibility). */
blendMode(DIFFERENCE);
textAlign(CENTER, CENTER);

//Use the round() function to round to the nearest foot/meter.
text(round(footToMeter(measurement)) + " METERS", width/2, meterLineY);
text(round(measurement) + " FEET", width/2, footLineY);
text("FOR APPROXIMATE LENGTH, DIVIDE BY 3.281", width/2, height/2);
}

float selectNum() {
return (random(minNum, maxNum));
}

float footToMeter(float ft) {
return ft/3.281;
}
181 changes: 72 additions & 109 deletions 08_Functions/lollipops/lollipops.pde
Original file line number Diff line number Diff line change
@@ -1,109 +1,72 @@
// Lollipops
// The Coding Train / Daniel Shiffman
// Processing Intro Series

/*Another way to represent colors instead of RGB, is by using
hexidecimal notation (ex: #FFFFFF is the color white). Many
color palettes found online use hex codes to denote colors.
You can convert RGB to Hex or vice versa using online tools. */
color[] rainbowColors = new int[]{#9A56FF, #527AF2, #F2B807, #F28907, #F2220F};

void setup() {
size(640, 360);

/*Use hue, saturation and brightness to specify
color with a minimum value of 0 and maximum value of
255.*/
colorMode(HSB, 255);
}

void draw() {
background(150, 30, 255);

sunshine();
cloud(110, 75, 100);
cloud(450, 95, 50);

for (int i = 0; i < rainbowColors.length; i++) {
int fillCol = (rainbowColors[i]); // color of lollipop
float size = 100; //size of lollipop
float posX = size/2 + 15 + (width/rainbowColors.length)*i; //x position

/*Start each lollipop at a different point in the sine function by offsetting the phase.
When removing phase in the sine function below, all lollipops move in unison*/
float phase = PI/rainbowColors.length * i;

/*frameCount contains the number of frames displayed since the program started.
It can be used with an oscillating function like sine or cosine, which returns a
number between -1 and 1. Try multiplying frameCount by a number.*/
float oscillate = easeIn(abs(sin(radians(frameCount) + phase)));
float posY = height/2 + 50 - 100*oscillate; // y position

lollipop(posX, posY, size, fillCol, oscillate);
}
}

float easeIn(float num) {
/*"Easing functions specify the rate of change of a parameter over time.
Objects in real life don’t just start and stop instantly, and almost
never move at a constant speed." Reference the following resource for
more information about different easing functions: https://easings.net*/

//num should be a number between 0 and 1.

float easing = 0;
if (num < 0.5) {
easing = (1 - sqrt(1 - pow(2 * num, 2))) / 2;
} else {
easing = (sqrt(1 - pow(-2 * num + 2, 2)) + 1) / 2;
}

return easing;
}

void lollipop(float x, float y, float size, int fillCol, float oscillate) {
//shaddow
fill(0, 20);
noStroke();

/*use the oscillation to vary the size of the shaddow. The closer the lollipop
is to the ground, the bigger the shaddow gets*/
ellipse(x, height-20, (size*(1-oscillate)), (size*(1-oscillate)/2));

//stem
strokeWeight(1);
fill(255);
noStroke();
rect(x -5, y, 10, height-y-20);

//lollipop
noStroke();
fill(fillCol);
circle(x, y, size);
float rectSize = size + 10; //lollipop center shape
rect(x-rectSize/2, y-rectSize/8, rectSize, rectSize/4, 5);
stroke(hue(fillCol), saturation(fillCol) - 100, brightness(fillCol)); //highlight
line(x - rectSize/2 + rectSize/8, y-rectSize/8, x+rectSize/2 - rectSize/8, y-rectSize/8);
stroke(hue(fillCol), saturation(fillCol), brightness(fillCol) - 100); //shaddow
line(x - rectSize/2 + rectSize/8, y+rectSize/8, x+rectSize/2 - rectSize/8, y+rectSize/8);

//white arc for glossy effect
noFill();
strokeWeight(5);
stroke(255);
arc(x, y, size - 20, size - 20, PI, PI+PI/3);
}

void sunshine() {
noStroke();
fill(35, 255, 255);
circle(60, 60, 100);
}

void cloud(float posX, float posY, float size) {
for (int i = 0; i < 3; i++) {
fill(255);
noStroke();
circle(posX + size/2*i, posY, size);
}
}
// Lollipops
// The Coding Train / Daniel Shiffman
// Processing Intro Series

/*Another way to represent colors instead of RGB, is by using
hexidecimal notation (ex: #FFFFFF is the color white). Many
color palettes found online use hex codes to denote colors.
You can convert RGB to Hex or vice versa using online tools. */
color[] rainbowColors = new int[]{#9A56FF, #527AF2, #F2B807, #F28907, #F2220F};

void setup() {
size(640, 360);
pixelDensity(2);

/*Use hue, saturation and brightness to specify
color with a minimum value of 0 and maximum value of
255.*/
colorMode(HSB, 255);
}

void draw() {
background(150, 30, 255);

sunshine();
cloud(110, 75, 100);
cloud(450, 95, 50);
lollipop(100, 180, 100, rainbowColors[0]);
lollipop(210, 180, 150, rainbowColors[1]);
lollipop(320, 180, 70, rainbowColors[2]);
lollipop(430, 180, 120, rainbowColors[3]);
lollipop(540, 180, 80, rainbowColors[4]);
}

void lollipop(float x, float y, float size, int fillCol) {

//stem
strokeWeight(1);
fill(255);
noStroke();
rect(x -5, y, 10, height-y-20);

//lollipop
noStroke();
fill(fillCol);
circle(x, y, size);
float rectSize = size + 10; //lollipop center shape
rect(x-rectSize/2, y-rectSize/8, rectSize, rectSize/4, 5);
stroke(hue(fillCol), saturation(fillCol) - 100, brightness(fillCol)); //highlight
line(x - rectSize/2 + rectSize/8, y-rectSize/8, x+rectSize/2 - rectSize/8, y-rectSize/8);
stroke(hue(fillCol), saturation(fillCol), brightness(fillCol) - 100); //shaddow
line(x - rectSize/2 + rectSize/8, y+rectSize/8, x+rectSize/2 - rectSize/8, y+rectSize/8);

//white arc for glossy effect
noFill();
strokeWeight(5);
stroke(255);
arc(x, y, size - 20, size - 20, PI, PI+PI/3);
}

void sunshine() {
noStroke();
fill(35, 255, 255);
circle(60, 60, 100);
}

void cloud(float posX, float posY, float size) {
for (int i = 0; i < 3; i++) {
fill(255);
noStroke();
circle(posX + size/2*i, posY, size);
}
}
Loading