diff --git a/08_Functions/conversion_function/conversion_function.pde b/08_Functions/conversion_function/conversion_function.pde index 067ab1b..da01cd5 100644 --- a/08_Functions/conversion_function/conversion_function.pde +++ b/08_Functions/conversion_function/conversion_function.pde @@ -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; +} diff --git a/08_Functions/lollipops/lollipops.pde b/08_Functions/lollipops/lollipops.pde index 8ce42f9..0706e63 100644 --- a/08_Functions/lollipops/lollipops.pde +++ b/08_Functions/lollipops/lollipops.pde @@ -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); + } +} diff --git a/08_Functions/rainbow_unicorn_puppy/rainbow_unicorn_puppy.pde b/08_Functions/rainbow_unicorn_puppy/rainbow_unicorn_puppy.pde index a3a7e7d..b01dbf7 100644 --- a/08_Functions/rainbow_unicorn_puppy/rainbow_unicorn_puppy.pde +++ b/08_Functions/rainbow_unicorn_puppy/rainbow_unicorn_puppy.pde @@ -1,110 +1,111 @@ -// Rainbow, Unicorn, Puppy Functions -// The Coding Train / Daniel Shiffman -// Processing Intro Series - -color[] rainbowColors = new int[]{#9A56FF, #527AF2, #F2B807, #F28907, #F2220F}; - - -void setup() { - size(640, 360); - colorMode(HSB, 255); - rectMode(CENTER); -} - -void draw() { - background(150, 30, 255); - rainbow(); - puppy(); - unicorn(); - noLoop(); -} - -void rainbow() { - float sw = 15; - strokeWeight(sw); - noFill(); - - for (int i = 0; i < rainbowColors.length; i++) { - stroke(rainbowColors[i]); //pick a color from the array - arc(width/2, (height + 100) - sw*i, width + sw*i, width, PI, PI*2); - } -} - -void unicorn() { - float sw = 3; //strokeWeight - float posX = width/2 - 110; - float posY = height/2 + 50; - stroke(0); - strokeWeight(sw); - float triSize = 50; //size of triangle - //pick a random horn color - fill(rainbowColors[int(random(rainbowColors.length))]); - triangle(posX - triSize/2, posY - 100, posX + triSize, posY - 90, posX+triSize/2, posY -100 - triSize*2); - - //unicorn "mane" - for (int i = 0; i < 7; i++) { - fill(130, 100, 255); - circle((posX - 40) - 10*i, posY - 50 + 40*i, 100); - } - - //head - fill(255); - rect(posX, posY, 150, 215, 100); - - //nose - fill(0, 120, 255); - ellipse(posX, posY+60, 150, 110); - - // nostrils - fill(0, 50, 255); - ellipse(posX + 10, posY + 60, 10, 10); - ellipse(posX - 10, posY + 60, 10, 10); - - //eyes - fill(0); - ellipse(posX, posY - 50, 5, 5); - ellipse(posX + 10, posY - 50, 8, 8); -} - -void puppy() { - float sw = 3; - float posX = width/2 + 110; - float posY = height/2 + 50; - stroke(0); - strokeWeight(sw); - - //ears - for (float i = 0; i < 3; i+=0.3) { - fill(15, 150, 100); - noStroke(); - circle((posX - 50) - 10*i, posY - 55 + 40*i, 100); - circle((posX + 50) + 10*i, posY - 55 + 40*i, 100); - } - - //head - stroke(0); - fill(15, 75, 200); - rect(posX, posY, 150, 215, 100); - - //nose - fill(35, 75, 255); - ellipse(posX, posY+60, 150, 110); - - //mouth - fill(0); - ellipse(posX, posY+25, 70, 35); - line(posX, posY+25, posX, posY+75); - noFill(); - arc(posX - 20, posY+75, 40, 25, 0, PI); - arc(posX + 20, posY+75, 40, 25, 0, PI); - - //eyes - fill(0); - ellipse(posX - 25, posY - 25, 20, 20); - ellipse(posX + 25, posY - 25, 20, 20); - - //eyebrows - noFill(); - arc(posX - 25, posY - 50, 40, 25, PI, 2*PI - PI/4); - arc(posX + 25, posY - 50, 40, 25, PI + PI/4, 2*PI); -} +// Rainbow, Unicorn, Puppy Functions +// The Coding Train / Daniel Shiffman +// Processing Intro Series + +color[] rainbowColors = new int[]{#9A56FF, #527AF2, #F2B807, #F28907, #F2220F}; + + +void setup() { + size(640, 360); + pixelDensity(2); + colorMode(HSB, 255); + rectMode(CENTER); +} + +void draw() { + background(150, 30, 255); + rainbow(); + puppy(); + unicorn(); + noLoop(); +} + +void rainbow() { + float sw = 15; + strokeWeight(sw); + noFill(); + + for (int i = 0; i < rainbowColors.length; i++) { + stroke(rainbowColors[i]); //pick a color from the array + arc(width/2, (height + 100) - sw*i, width + sw*i, width, PI, PI*2); + } +} + +void unicorn() { + float sw = 3; //strokeWeight + float posX = width/2 - 110; + float posY = height/2 + 50; + stroke(0); + strokeWeight(sw); + float triSize = 50; //size of triangle + //pick a random horn color + fill(rainbowColors[int(random(rainbowColors.length))]); + triangle(posX - triSize/2, posY - 100, posX + triSize, posY - 90, posX+triSize/2, posY -100 - triSize*2); + + //unicorn "mane" + for (int i = 0; i < 7; i++) { + fill(130, 100, 255); + circle((posX - 40) - 10*i, posY - 50 + 40*i, 100); + } + + //head + fill(255); + rect(posX, posY, 150, 215, 100); + + //nose + fill(0, 120, 255); + ellipse(posX, posY+60, 150, 110); + + // nostrils + fill(0, 50, 255); + ellipse(posX + 10, posY + 60, 10, 10); + ellipse(posX - 10, posY + 60, 10, 10); + + //eyes + fill(0); + ellipse(posX, posY - 50, 5, 5); + ellipse(posX + 10, posY - 50, 8, 8); +} + +void puppy() { + float sw = 3; + float posX = width/2 + 110; + float posY = height/2 + 50; + stroke(0); + strokeWeight(sw); + + //ears + for (float i = 0; i < 3; i+=0.3) { + fill(15, 150, 100); + noStroke(); + circle((posX - 50) - 10*i, posY - 55 + 40*i, 100); + circle((posX + 50) + 10*i, posY - 55 + 40*i, 100); + } + + //head + stroke(0); + fill(15, 75, 200); + rect(posX, posY, 150, 215, 100); + + //nose + fill(35, 75, 255); + ellipse(posX, posY+60, 150, 110); + + //mouth + fill(0); + ellipse(posX, posY+25, 70, 35); + line(posX, posY+25, posX, posY+75); + noFill(); + arc(posX - 20, posY+75, 40, 25, 0, PI); + arc(posX + 20, posY+75, 40, 25, 0, PI); + + //eyes + fill(0); + ellipse(posX - 25, posY - 25, 20, 20); + ellipse(posX + 25, posY - 25, 20, 20); + + //eyebrows + noFill(); + arc(posX - 25, posY - 50, 40, 25, PI, 2*PI - PI/4); + arc(posX + 25, posY - 50, 40, 25, PI + PI/4, 2*PI); +} diff --git a/08_Functions/unicorn_only/unicorn_only.pde b/08_Functions/unicorn_only/unicorn_only.pde new file mode 100644 index 0000000..f9929f7 --- /dev/null +++ b/08_Functions/unicorn_only/unicorn_only.pde @@ -0,0 +1,110 @@ +// Rainbow, Unicorn, Puppy Functions +// The Coding Train / Daniel Shiffman +// Processing Intro Series + +color[] rainbowColors = new int[]{#9A56FF, #527AF2, #F2B807, #F28907, #F2220F}; + + +void setup() { + size(640, 360); + pixelDensity(2); + colorMode(HSB, 255); + rectMode(CENTER); +} + +void draw() { + background(150, 30, 255); + rainbow(); + unicorn(); + noLoop(); +} + +void rainbow() { + float sw = 15; + strokeWeight(sw); + noFill(); + + for (int i = 0; i < rainbowColors.length; i++) { + stroke(rainbowColors[i]); //pick a color from the array + arc(width/2, (height + 100) - sw*i, width + sw*i, width, PI, PI*2); + } +} + +void unicorn() { + float sw = 3; //strokeWeight + float posX = width/2 - 110; + float posY = height/2 + 50; + stroke(0); + strokeWeight(sw); + float triSize = 50; //size of triangle + //pick a random horn color + fill(rainbowColors[int(random(rainbowColors.length))]); + triangle(posX - triSize/2, posY - 100, posX + triSize, posY - 90, posX+triSize/2, posY -100 - triSize*2); + + //unicorn "mane" + for (int i = 0; i < 7; i++) { + fill(130, 100, 255); + circle((posX - 40) - 10*i, posY - 50 + 40*i, 100); + } + + //head + fill(255); + rect(posX, posY, 150, 215, 100); + + //nose + fill(0, 120, 255); + ellipse(posX, posY+60, 150, 110); + + // nostrils + fill(0, 50, 255); + ellipse(posX + 10, posY + 60, 10, 10); + ellipse(posX - 10, posY + 60, 10, 10); + + //eyes + fill(0); + ellipse(posX, posY - 50, 5, 5); + ellipse(posX + 10, posY - 50, 8, 8); +} + +void puppy() { + float sw = 3; + float posX = width/2 + 110; + float posY = height/2 + 50; + stroke(0); + strokeWeight(sw); + + //ears + for (float i = 0; i < 3; i+=0.3) { + fill(15, 150, 100); + noStroke(); + circle((posX - 50) - 10*i, posY - 55 + 40*i, 100); + circle((posX + 50) + 10*i, posY - 55 + 40*i, 100); + } + + //head + stroke(0); + fill(15, 75, 200); + rect(posX, posY, 150, 215, 100); + + //nose + fill(35, 75, 255); + ellipse(posX, posY+60, 150, 110); + + //mouth + fill(0); + ellipse(posX, posY+25, 70, 35); + line(posX, posY+25, posX, posY+75); + noFill(); + arc(posX - 20, posY+75, 40, 25, 0, PI); + arc(posX + 20, posY+75, 40, 25, 0, PI); + + //eyes + fill(0); + ellipse(posX - 25, posY - 25, 20, 20); + ellipse(posX + 25, posY - 25, 20, 20); + + //eyebrows + noFill(); + arc(posX - 25, posY - 50, 40, 25, PI, 2*PI - PI/4); + arc(posX + 25, posY - 50, 40, 25, PI + PI/4, 2*PI); +}