0% found this document useful (0 votes)
78 views7 pages

Experiment: Program For Drawing Sine and Tangent Waves Using Opengl

The document describes an experiment to draw sine and tangent waves using OpenGL functions. It outlines the steps of the algorithm which include: 1) Dividing the window area, 2) Running a loop from 0 to 360 degrees, 3) Converting the angle to radians, 4) Incrementing X and calculating Y based on sine or tangent of the angle. The code provided draws the sine and tangent waves by running through a loop, calculating the X and Y coordinates, and rendering the lines.

Uploaded by

Sahib Sodhi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
78 views7 pages

Experiment: Program For Drawing Sine and Tangent Waves Using Opengl

The document describes an experiment to draw sine and tangent waves using OpenGL functions. It outlines the steps of the algorithm which include: 1) Dividing the window area, 2) Running a loop from 0 to 360 degrees, 3) Converting the angle to radians, 4) Incrementing X and calculating Y based on sine or tangent of the angle. The code provided draws the sine and tangent waves by running through a loop, calculating the X and Y coordinates, and rendering the lines.

Uploaded by

Sahib Sodhi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

EXPERIMENT

Aim: Draw sine wave and tangent wave using OpenGL


functions
Algorithm:
Step 1:- Start by dividing the window client area in two in
both X and Y axis.
Step 2:- Then run a loop of some variable from 0 to 360 - that
would be your degree value. The degree step may be 5
degrees or whatever you choose. if you choose less, like 1,
then curve will be more smooth, if more, like 10 - curve may
become more like a set of lines (which is what it is, but you
will not see it if degree step is low - you can experiment with
step to see how it will affect the line).
Step 3:- Next step is to convert angle into radians.
Step 4:- For each step increase in angle by 1 degree, X value
is incremented by 0.1 and Y value is calculated by taking sine
of angle.
Step 5:- Similar step are followed for drawing tangent wave
but instead of taking sine of angle for calculating Y value,
tangent of angle is calculated.

Program for drawing sine and tangent waves using


OpenGL
Code:
1. For Sine wave

#include <windows.h>
#include <GL/glu.h>
#include <GL/glut.h>

using namespace std;

float rotate =0.0;


float start = 0.0;

void drawSinWave()
{
float radius = 7.5;
float x2=0,y2,cx,cy,fx,fy;
float cos_y,cache_cos_y;
int cache = 0;
start += 1.0;
if(start >360) {
start = 0;
}
glBegin(GL_LINES);
float angle = 0;
for(angle=start; ; angle+=1.0) {
if(angle>1020) {
break;
angle = 0.0;
}
float rad_angle = angle * 3.14 / 180;
x2 = x2+0.1;
y2 = radius * sin((double)rad_angle);
cos_y = radius * sin((double)-rad_angle);
if (cache) {
glVertex2f(cx,cy);
glVertex2f(x2,y2);

glVertex2f(cx,cache_cos_y);
glVertex2f(x2,cos_y);

} else {
fx = x2;
fy = y2;
}
cache = 1;
cx = x2;
cy = y2;
cache_cos_y = cos_y;
}
glEnd();
}

void display(void) {
glClearColor (0.0,0.0,0.0,1.0);
glClear (GL_COLOR_BUFFER_BIT);
glLineWidth(10);
glLoadIdentity();
glTranslatef(-40,0,-30);
glColor3f(1,0,0);

drawSinWave();
glutSwapBuffers();
}

void reshape (int w, int h) {


glViewport (0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
gluPerspective (60, (GLfloat)w / (GLfloat)h, 0.1, 100.0);
glMatrixMode (GL_MODELVIEW);
}

void keyboard(unsigned char key, int x, int y)


{
switch (key) {
case 27:
exit(0);
break;
}
}

int main (int argc, char **argv) {


glutInit (&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE);
glutInitWindowSize (1200, 800);
glutInitWindowPosition (0, 0);
glutCreateWindow ("A Sine wave");

glutDisplayFunc (display);
glutIdleFunc (display);
glutReshapeFunc (reshape);
glutKeyboardFunc (keyboard);
glutMainLoop ();
return 0;
}

2. For Tangent wave

#include <iostream>
#include <stdlib.h>
#include <math.h>
#include <windows.h>
#include <GL/glu.h>
#include <GL/glut.h>

using namespace std;

float rotate =0.0;


float start = 0.0;

void drawSinWave()
{
float radius = 7.5;
float x2=0,y2,cx,cy,fx,fy;
//float cos_y,cache_cos_y;
int cache = 0;
start += 1.0;
if(start >360) {
start = 0;
}
glBegin(GL_LINES);
float angle = 0;
for(angle=start; ; angle+=1.0) {
if(angle>1020) {
break;
angle = 0.0;
}
float rad_angle = angle * 3.14 / 180;
x2 = x2+0.1;
y2 = radius * tan((double)rad_angle);
//cos_y = radius * tan((double)-rad_angle);
if (cache) {
glVertex2f(cx,cy);
glVertex2f(x2,y2);

//glVertex2f(cx,cache_cos_y);
//glVertex2f(x2,cos_y);

} else {
fx = x2;
fy = y2;
}
cache = 1;
cx = x2;
cy = y2;
cache_cos_y = cos_y;
}
glEnd();
}

void display(void) {
glClearColor (0.0,0.0,0.0,1.0);
glClear (GL_COLOR_BUFFER_BIT);
glLineWidth(10);
glLoadIdentity();
glTranslatef(-40,0,-30);
glColor3f(1,0,0);

drawSinWave();
glutSwapBuffers();
}

void reshape (int w, int h) {


glViewport (0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
gluPerspective (60, (GLfloat)w / (GLfloat)h, 0.1, 100.0);
glMatrixMode (GL_MODELVIEW);
}

void keyboard(unsigned char key, int x, int y)


{
switch (key) {
case 27:
exit(0);
break;
}
}

int main (int argc, char **argv) {


glutInit (&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE);
glutInitWindowSize (1200, 800);
glutInitWindowPosition (0, 0);
glutCreateWindow ("A Tan wave");

glutDisplayFunc (display);
glutIdleFunc (display);
glutReshapeFunc (reshape);
glutKeyboardFunc (keyboard);
glutMainLoop ();
return 0;
}

Screenshots:

You might also like