Download presentation
Presentation is loading. Please wait.
1
Trigonometry & Random March 2, 2010
2
Trigonometry
3
Angles and Waves Angles can be measured in degrees or radians
Pi is the ratio of the circumference of a circle to its diameter Angles can be converted from degrees to radians with radians() Angles can be converted from radians to degrees with degrees()
4
Sine
5
Sine Wave in Processing
Angles are specified in radians in sin() and cos() functions size(700, 100); noStroke(); fill(0); float angle = 0.0; for (int x = 0; x <= width; x += 5) { float y = map(sin(angle), -1, 1, 2*height/3, height/3); rect(x, y, 2, 4); angle += PI/20.0; }
6
Following the sine void setup() { size(700, 100); ellipseMode(CENTER);
fill(0); } float angle = 0.0; float time = 0; float yPos = 0; void draw() { background(255); yPos = map(sin(angle), -1, 1, 0.8*height, 0.2*height); ellipse(time, yPos, 20, 20); if (time>width) { time=0; time += 5; angle += PI/20.0;
7
In a spiral void setup() { size(200,200); noStroke(); smooth(); }
float radius = 1.0; int deg = 0; float increment=random(20); void draw() { float angle = radians(deg); float x = width/2 + (cos(angle) * radius); float y = height/2 + (sin(angle) * radius); ellipse(x, y, 6, 6); deg += 11; if(radius>width/2+1) { radius=0; increment=random(20); radius = radius ;
8
Earth and Moon sort of void setup() { size(400,400); noStroke(); smooth(); ellipseMode(CENTER); } void drawBody(float angleRot, int radiusOrbit, int radiusDot) { float x = map(sin(angleRot), -1, 1, width/2-radiusOrbit, width/2+radiusOrbit) ; float y = map(cos(angleRot), -1, 1, width/2-radiusOrbit, width/2+radiusOrbit) ; ellipse(x, y, radiusDot, radiusDot) ; float angle = 0 ; void draw() { background(0) ; fill(0,255,0) ; drawBody(angle, width/10, width/10) ; fill(255,255,0) ; drawBody(angle+PI, 4*width/10, width/30) ; angle += 0.01 ;
9
Rotating Rectangles void drawBody(float angleRot, int radiusOrbit, int radiusDot) { int lowEnd = width/2-radiusOrbit ; int highEnd = width/2+radiusOrbit ; float x = map(sin(angleRot), -1, 1, lowEnd, highEnd) ; float y = map(cos(angleRot), -1, 1, lowEnd, highEnd) ; pushMatrix() ; translate(x, y) ; rotate(-angleRot) ; rect(0, 0, 2*radiusDot, radiusDot) ; popMatrix() ; } Be sure to add rectMode(CENTER) to setup()
10
Lab Exercise Fill the canvas with a regular, repeating pattern that uses the sine function.
11
Random Numbers Assume float f; To generate a pseudo random number between 0 and high and assign it to f f = random(high); To generate a pseudo random number between low and high f = random(low, high);
12
Printing Random Numbers
for (int i=0; i<10; i++) { print(i + ". "); println(random(100)); } How do the print statements print and println differ? What’s the + inside of a print
13
Sample Code float f = random(5.2);
// Assign f a float value from 0 to 5.2 int i = random(5.2); // ERROR! Can't assign a float to an int int j = (int)random(5.2); // Assign j an int value from 0 to 5
14
Perlin Noise A smoother noise void setup() { size(800,200);
noStroke(); noiseSeed(0) ; fill(0) ; for (int x=0; x<width; x++) { float yN = noise(x*0.1) * 100 ; ellipse(x,yN, 5, 5) ; float yR = random(1.0) * 100 ; ellipse(x,yR+100, 5, 5) ; }
15
Noisy Mountains void setup() { size(800,200); background(0,0,200) ; noiseSeed(0) ; stroke(0, 150,0) ; for (int x=0; x<width; x++) { float yN = noise(x*0.01) * 150 ; line(x, yN, x, height) ; }
16
In-class Lab Modify the groundhog to be a bit more random or noisy
Or modify anything else
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.