Week 3 Assignment
Yasmin Elayat
Problem 1. Create a function the takes as input a wavelength in pixels (float) and an amplitude (float) and draws a series of crested (upward pointing) waves.
|
void drawWaves(float wavlength, float amp) {
glBegin(GL_LINE_STRIP);
for(int i =0; i <720; i++){
float x = i;
//float t = i;
//float x = t + 30 * dsin(t/10 * 360.0 );
//dsin(2*x) compresses wav, ie higher frequency
//float y = amp * dsin(x*freq); //using freq
float y = amp * dsin((x/wavlength) * 360.0);
glVertex2f(x+20,y+300);
}
glEnd();
}
|
 |
Problem 2. Create a function that draws a repeating hearbeat pattern, as seen on an EKG. Do this by compositing a series of sin waves.
|
void heartbeat() {
//drawing sin wav
float amp = 40; //mouseY
float amp2 =10;
float amp3 = 40;
float phase = 0; //90;
float wavelength = 130; // 100; //wav to repeat every 100 pixels
float wavelength2 = 140;
float wavelength3 = 100;
glBegin( GL_LINE_STRIP);
for(int i =0; i <720; i++){
float x = i;
float y = amp * dsin(2* x/wavelength * 360.0 + phase); //using wav
y += amp2 * dsin(x/wavelength2 * 360.0 + phase); //using wav
glVertex2f(x+20,y+300);
}
glEnd();
}
|
 |
Problem 3. Create a function that takes as input a number of petals (int), an inner radius (float), and a petal length (float), and draws a daisy-like flower using a single continuous line loop.
|
void drawFlower(int petals, float radInner, float petalLen, int numPoints = 360) {
glBegin(GL_LINE_LOOP);
for(int i = 0; i
|
 |
Problem 4. Create a function that takes as input a number of teeth (int), an inner radius and an outer radius, and draws a flat-tooth gear as specified.
|
void drawGear(int teethNo, int radI, int radO) {
Vec2d P;
Vec2d center = Vec2d(windowW/2+250,windowH/2);
int numPoints = 1000;
glBegin(GL_LINE_LOOP);
for(int i = 0; i < numPoints; i++) {
float A = 2*PI / numPoints * i;
int x =i;
int y = (radO - radI) * ((2* x/(numPoints/teethNo)) %2);
P.x = cos(A) * (radI + y);
P.y = sin(A) * (radI + y);
P = P + center;
glVertex2f(P.x,P.y);
}
glEnd();
}
|
 |
Problem 5. Create a function that takes as input a seed number (int) and generates a random blobby form using a contouring function that combines three or more low-freqency sin waves. Be sure that the sin waves make complete cycles to ensure a smooth transition from the last point to the first.
|
void drawBlob(int seedNo) {
srand ( seedNo );
int numPoints = 360;
float amp = rand() % 10; //10;
float amp2 = rand() % 45; //20;
float amp3 = rand() % 80; //40;
float phase = 0; //90;
float wavelength = numPoints/2; //180
float wavelength2 = numPoints/3; //120
float wavelength3 = numPoints/6; //60
Vec2d P;
Vec2d center = Vec2d(windowW/2,windowH/2);
glBegin(GL_LINE_LOOP);
for(int i = 0; i < numPoints; i++) {
float A = 2*PI / numPoints * i;
int x =i;
float y = amp * dsin(x/wavelength * 360.0 + phase);
y += amp2 * dsin(x/wavelength2 * 360.0 + phase);
y += amp3 * dsin(x/wavelength3 * 360.0 + phase);
P.x = dcos(A) * (100 + y);
P.y = dsin(A) * (100 + y);
P = P + center;
glVertex2f(P.x,P.y);
}
glEnd();
}
|
 |
Problem 6. Find 3 photographs of either natural phenomena or industrial products that involve repeating contours or surface profiles, and create three functions to model each of them. Include the photographs in your assignment postings.
|
void drawPhoto1()
{
Vec2d P;
Vec2d center = Vec2d(windowW/2,windowH/2);
int numPoints = 360 * 3; //3 circles
float rad = 0;
float Rad1 = 0;
float Rad2 = 50;
float amp = 30;
float counter = 1;
glBegin(GL_LINE_STRIP);
for(int i = 0; i < numPoints; i++) {
int A = i % 360;
int x =i;
if((i%360)==0){ counter++; amp *= counter; }
float y = amp * dsin(x * 10 * 360.0 + 0);
rad = i/4;
P.x = dcos(A) * (rad +y);//(radI + y);
P.y = dsin(A) * (rad +y);//(radI + y);
P = P + center;
glVertex2f(P.x,P.y);
}
glEnd();
}
|
 |
|