0% found this document useful (0 votes)
26 views22 pages

First Internals Optimized Code

Uploaded by

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

First Internals Optimized Code

Uploaded by

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

1.

Design a line using DDA line drawing algorithm

dda
#include<stdio.h>
#include<stdlib.h>
#include<GL/glut.h>

float x1, x2, y1, y2;

void display(void) {
float dx = x2 - x1;
float dy = y2 - y1;
float steps;

if (abs(dx) > abs(dy)) {


steps = abs(dx);
}
else {
steps = abs(dy);
}

float xIncrement = dx / steps;


float yIncrement = dy / steps;

float x = x1;
float y = y1;
glPointSize(5.0);
glBegin(GL_POINTS);
glVertex2i(x, y);
glEnd();

for (int k = 1; k <= steps; k++) {


x += xIncrement;
y += yIncrement;

glBegin(GL_POINTS);
glVertex2i(x, y);
glEnd();
}

glFlush();
}

void myInit(void) {
glClearColor(0.0, 0.0, 0.0, 0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();

float margin = 10.0; // Adjust this margin as needed for better visibility

gluOrtho2D(x1 - margin, x2 + margin, y1 - margin, y2 + margin);


}

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


printf("Value of x1,y1: ");
scanf_s("%f%f", &x1, &y1);
printf("Value of x2,y2: ");
scanf_s("%f%f", &x2, &y2);
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(400, 400);
glutInitWindowPosition(400, 350);
glutCreateWindow("");
myInit();
glutDisplayFunc(display);
glutMainLoop();

return 0;
}

2.Implement Bresenham‟s line drawing algorithm for all types of slope


Bresenham
#include <math.h>
#include <stdio.h>
#include <GL/glut.h>
#include <Windows.h>

void plotPixel(int x, int y) {

glColor3f(1, 0, 0);
glPointSize(5);
glBegin(GL_POINTS);
glVertex2i(x, y);
glEnd();
glFlush();
Sleep(100);
}

void bresenham(int x0, int y0, int x1, int y1) {


int dx, dy;
int xInc, yInc;
int x, y;
int e, inc1, inc2; // decision parameter and increments

dx = x1 - x0;
dy = y1 - y0;

dx = dx < 0 ? -dx : dx;


dy = dy < 0 ? -dy : dy;

xInc = (x1 - x0) < 0 ? -1 : 1;


yInc = (y1 - y0) < 0 ? -1 : 1;

x = x0;
y = y0;

if (dx > dy) {


// slope m <= 0
plotPixel(x, y);
e = 2 * dy - dx;
inc1 = 2 * (dy - dx);
inc2 = 2 * dy;

for (int i = 0; i < dx; i++) {


if (e >= 0) {
y += yInc;
e += inc1;
}
else {
e += inc2;
}
x += xInc;
plotPixel(x, y);
}
}
else {
// slope m > 0
plotPixel(x, y);
e = 2 * dx - dy;
inc1 = 2 * (dx - dy);
inc2 = 2 * dx;

for (int i = 0; i < dy; i++) {


if (e >= 0) {
x += xInc;
e += inc1;
}
else {
e += inc2;
}
y += yInc;
plotPixel(x, y);
}
}
}

void display() {
glClear(GL_COLOR_BUFFER_BIT);
glPointSize(2.0);
bresenham(40, 120, 100, 20);
printf("done");
}

void init() {
glClearColor(1.0, 1.0, 1.0, 1.0);
gluOrtho2D(0, 200, 0, 200);
}

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


glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowPosition(10, 10);
glutInitWindowSize(500, 500);
glutCreateWindow("Bresenham algorithm");
init();
glutDisplayFunc(display);
glutMainLoop();

return 0;
}

3.Design a real world picture using primitives such as points, lines, triangles and
polygons.
#include <GL/glut.h>
#include <math.h>
#include <stdlib.h>
#include <time.h>

const int numStars = 100;

void drawMountain(int x, int y, int height) {


glBegin(GL_TRIANGLES);
glVertex2i(x, y);
glVertex2i(x + 50, y);
glVertex2i(x + 25, y + height);
glEnd();
}

void drawRiver() {
glBegin(GL_POLYGON);
glVertex2i(0, 50);
glVertex2i(500, 50);
glVertex2i(500, 0);
glVertex2i(0, 0);
glEnd();
}

void drawStars() {
glColor3f(1.0, 1.0, 1.0); // White color for stars
glPointSize(2.0);
glBegin(GL_POINTS);
for (int i = 0; i < numStars; ++i) {
int x = rand() % 500; // Random x-coordinate
int y = rand() % 750; // Random y-coordinate
glVertex2i(x, y);
}
glEnd();
}

void myDisplay() {
glClear(GL_COLOR_BUFFER_BIT);

// Draw Stars
drawStars();

// Blue color for the river


glColor3f(0.0, 0.0, 1.0);
drawRiver();

// Draw Landscape
glColor3f(0.5, 0.5, 0.5); // Gray color for mountains
drawMountain(50, 100, 150);
drawMountain(200, 150, 100);
drawMountain(350, 120, 120);

glFlush();
}

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


glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutInitWindowPosition(100, 150);
glutCreateWindow("OpenGL Landscape and Starry Night");

glClearColor(0.0, 0.7, 1.0, 1.0); // Light blue background color

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, 500, 0, 500);

//srand(time(NULL)); // Seed for random number generation

glutDisplayFunc(myDisplay);
glutMainLoop();

return 0;
}

4.Design, develop and implement recursively subdivide a tetrahedron to form 3D


sierpinski gasket. The number of recursive steps is to be specified by the user

#include <stdio.h>
#include <Windows.h>
#include <GL/glut.h>

float vertices[4][3] = { {10, 10, -490}, {490, 10, -490}, {240, 490, -490}, {240,
150, 490} };

void drawTriangle(float* a, float* b, float* c) {


glBegin(GL_TRIANGLES);
glVertex3fv(a);
glVertex3fv(b);
glVertex3fv(c);
glEnd();
Sleep(100);
}
void drawTetraheadron(float* a, float* b, float* c, float* d) {
glColor3f(1, 0, 0);
drawTriangle(a, b, c);
glColor3f(0, 1, 0);
drawTriangle(a, c, d);
glColor3f(0, 0, 1);
drawTriangle(a, d, b);
glColor3f(1, 0, 1);
drawTriangle(b, d, c);
glFlush();

void drawTetra(float* a, float* b, float* c, float* d, int n) {


if (n == 0) {
drawTetraheadron(a, b, c, d);
}
else {
float m[6][3];
for (int i = 0; i < 3; i++) {
m[0][i] = (a[i] + b[i]) / 2; //ab
m[1][i] = (a[i] + c[i]) / 2; //ac
m[2][i] = (b[i] + c[i]) / 2; //bc
m[3][i] = (a[i] + d[i]) / 2; //ad
m[4][i] = (b[i] + d[i]) / 2; //bd
m[5][i] = (c[i] + d[i]) / 2; //cd
}

drawTetra(a, m[0], m[1], m[3], n - 1);


drawTetra(m[0], b, m[2], m[4], n - 1);
drawTetra(m[1], m[2], c, m[5], n - 1);
drawTetra(m[3], m[4], m[5], d, n - 1);
}
}

void init() {
glClearColor(1, 1, 1, 1);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, 500, 0, 500, -500, 500);
glMatrixMode(GL_MODELVIEW);
}

void display() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
drawTetra(vertices[0], vertices[1], vertices[2], vertices[3], 2);
glFlush();
}

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


glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowPosition(0, 0);
glutInitWindowSize(500, 500);
glutCreateWindow("Sierpinski Tetrahedron");
glutDisplayFunc(display);
//glEnable(GL_DEPTH_TEST);
init();
glutMainLoop();
return 0;
}

2d triangle
#include<GL/glut.h>
#include<stdio.h>

float y[3][2] = { {0,0}, {25,50}, {50,0} };


int n;

void display_triangle(float* a, float* b, float* c)


{
glVertex2fv(a);
glVertex2fv(b);
glVertex2fv(c);
}

void divide_triangle(float* a, float* b, float* c, int m)


{
float ab[2], bc[2], ac[2];
if (m > 0)
{
for (int j = 0; j < 2; j++)
{
ab[j] = (a[j] + b[j]) / 2;
bc[j] = (b[j] + c[j]) / 2;
ac[j] = (a[j] + c[j]) / 2;
}
divide_triangle(a, ab, ac, m - 1);
divide_triangle(b, ab, bc, m - 1);
divide_triangle(c, ac, bc, m - 1);
}
else
display_triangle(a, b, c);
}

void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_TRIANGLES);
glColor3f(1.0, 0.0, 0.0);
divide_triangle(y[0], y[1], y[2], n);
glEnd();
glFlush();
}

void init()
{
glClearColor(1.0, 1.0, 1.0, 1.0);
gluOrtho2D(-10, 60, -10, 60);
}

void main(int argc, char** argv)


{
printf("Enter the number of divisions: ");
scanf_s("%d", &n);
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutInitWindowPosition(0, 0);
glutCreateWindow("Sierpinsky Gasket Triangle");
init();
glutDisplayFunc(display);
glutMainLoop();
}

5.Implement a circle drawing algorithm.


#include <GL/glut.h>
#include <stdio.h>

int xc, yc, r;

void drawPixel(int x, int y) {


glBegin(GL_POINTS);
glVertex2i(x, y);
glEnd();
}

void drawCircle(int xc, int yc, int x, int y) {


drawPixel(xc + x, yc + y);
drawPixel(xc + x, yc - y);
drawPixel(xc - x, yc + y);
drawPixel(xc - x, yc - y);
drawPixel(xc + y, yc + x);
drawPixel(xc + y, yc - x);
drawPixel(xc - y, yc + x);
drawPixel(xc - y, yc - x);
}

void midpointCircle() {
int x = 0;
int y = r;
float decision = 1 - r;

drawCircle(xc, yc, x, y);

while (y > x) {
if (decision < 0) {
decision += 2 * x + 3;
}
else {
decision += 2 * (x - y) + 5;
y--;
}
x++;
drawCircle(xc, yc, x, y);
}
}

void display() {
glClear(GL_COLOR_BUFFER_BIT);
midpointCircle();
glFlush();
}

void myInit() {
glClearColor(1.0, 1.0, 1.0, 0.0);
glColor3f(0.0, 0.0, 0.0);
gluOrtho2D(0, 500, 0, 500);
}

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


printf("Enter the center(h,k) and radius of the circle: ");
scanf_s("%d %d %d", &xc, &yc, &r);

glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutInitWindowPosition(100, 100);
glutCreateWindow("Circle Drawing Algorithm");
myInit();
glutDisplayFunc(display);
glutMainLoop();

return 0;
}

6.Develop a menu driven program to fill the polygon using scan line algorithm
#include<stdio.h>
#include<GL/glut.h>

void edgeDetect(int x1, int y1, int x2, int y2, int* le, int* re) {
float mx;
float x;
int i, temp;

if ((y2 - y1) < 0) {


temp = y2; y2 = y1; y1 = temp;
temp = x2; x2 = x1; x1 = temp;
}

mx = (y2 - y1) != 0 ? ((x2 - x1) / (y2 - y1)) : (x2 - x1);

x = x1;
for (i = y1; i <= y2; i++) {
if (x < le[i])
le[i] = x;
if (x > re[i])
re[i] = x;
x = x + mx;
}
}
void drawPixel(int x, int y) {
glBegin(GL_POINTS);
glVertex2i(x, y);
glEnd();
glFlush();
}
void scanfill(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4) {
int le[500], re[500];
int i, y;
for (int i = 0; i < 500; i++) {
le[i] = 500;
re[i] = 0;
}
edgeDetect(x1, y1, x2, y2, le, re);
edgeDetect(x2, y2, x3, y3, le, re);
edgeDetect(x3, y3, x4, y4, le, re);
edgeDetect(x4, y4, x1, y1, le, re);

for (y = 0; y < 500; y++) {


if (le[y] <= re[y]) {
for (i = le[y]; i <= re[y]; i++) {
drawPixel(i, y);
}
}
}
}
void init() {
glClearColor(1, 1, 1, 1);
gluOrtho2D(0, 500, 0, 500);
}
void display() {
glClear(GL_COLOR_BUFFER_BIT);
}
int x0, y0, x1, y1, x2, y2, x3, y3;
void select(int ch) {
x0 = 250; y0 = 200;
x1 = 150; y1 = 300;
x2 = 250; y2 = 400;
x3 = 350; y3 = 300;

switch (ch)
{
case 1: glColor3f(1, 0, 0); break;
case 2: glColor3f(0, 0, 1); break;
case 3: glColor3f(0, 1, 0); break;
}
glBegin(GL_LINE_LOOP);
glVertex2i(x0, y0);
glVertex2i(x1, y1);
glVertex2i(x2, y2);
glVertex2i(x3, y3);
glEnd();
glFlush();
scanfill(x0, y0, x1, y1, x2, y2, x3, y3);
glutPostRedisplay();
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutInitWindowPosition(10, 10);
glutCreateWindow("Scanfill");
init();
glutDisplayFunc(display);
glutCreateMenu(select);
glutAddMenuEntry("red", 1);
glutAddMenuEntry("blue", 2);
glutAddMenuEntry("green", 3);
glutAttachMenu(GLUT_LEFT_BUTTON);
glutMainLoop();
return 0;
}

7.Implement the program to draw a polygon that interact with interact with input
functions
//Submenu - optimised
#include<GL/glut.h>

GLfloat red = 1.0, green = 1.0, blue = 0.0;


GLenum renderingMode = GL_SMOOTH;
void init(void)
{
glClearColor(0.7, 0.7, 0.7, 1.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, 300.0, 0.0, 300.0);
}
void mainMenu(GLint renderingOption)
{
switch (renderingOption)
{
case 1: renderingMode = GL_FLAT;
break;
case 2: renderingMode = GL_SMOOTH;
break;
}
glutPostRedisplay();
}

void colorSubMenu(GLint colorOption)


{
switch (colorOption)
{
case 1: red = 0.0; green = 0.0; blue = 1.0;
break;
case 2: red = 0.0; green = 1.0; blue = 0.0;
break;
case 3: red = 1.0; green = 1.0; blue = 1.0;
}
glutPostRedisplay();
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glShadeModel(renderingMode);
glColor3f(red, green, blue);
glBegin(GL_TRIANGLES);
glVertex2i(40 , 50);
glVertex2i(140 , 250);
glColor3f(0.9, 0.2, .3);
glVertex2i(240, 50);

glEnd();
glFlush();
}
void main(int argc, char** argv)
{
GLint subMenu;
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowPosition(200, 200);
glutInitWindowSize(400, 400);
glutCreateWindow("Submenu Example");
init();
glutDisplayFunc(display);
subMenu = glutCreateMenu(colorSubMenu);
glutAddMenuEntry("Blue", 1);
glutAddMenuEntry("Green", 2);
glutAddMenuEntry("White", 3);

glutCreateMenu(mainMenu);
glutAddMenuEntry("Solid-Color Fill", 1);
glutAddMenuEntry("Color-Interpolation Fill", 2);
glutAddSubMenu("Color", subMenu);
glutAttachMenu(GLUT_RIGHT_BUTTON);
glutMainLoop();
}
8.Create and rotate a triangle about the origin and a fixed point.
GLint s1[] = { 150,200 };
GLint s2[] = { 250,200 };
GLint s3[] = { 250,400 };
GLshort angle = 15;

void before_rotation_pivot() {
glColor3ub(130, 133, 131);
glBegin(GL_LINE_LOOP);
glVertex2iv(s1);
glVertex2iv(s2);
glVertex2iv(s3);
glEnd();
}

void after_rotation_pivot() {
glColor3ub(247, 190, 47);
glTranslatef(s1[0], s1[1], 0);
glRotatef(angle, 0, 0, 1);
glTranslatef(-s1[0], -s1[1], 0);
glBegin(GL_LINE_LOOP);
glVertex2iv(s1);
glVertex2iv(s2);
glVertex2iv(s3);
glEnd();
}

void rotate_pivot() {
glClear(GL_COLOR_BUFFER_BIT);
before_rotation_pivot();
after_rotation_pivot();
glFlush();
}

GLint q1[] = { 400,400 };


GLint q2[] = { 550,400 };
GLint q3[] = { 550,550 };
GLint q4[] = { 400,550 };

void before_rotation() {
glColor3ub(130, 133, 131);
glBegin(GL_LINE_LOOP);
glVertex2iv(q1);
glVertex2iv(q2);
glVertex2iv(q3);
glVertex2iv(q4);
glEnd();
}

void after_rotation() {
glColor3ub(0, 255, 0);
glRotatef(angle, 0, 0, 1);
glBegin(GL_LINE_LOOP);
glVertex2iv(q1);
glVertex2iv(q2);
glVertex2iv(q3);
glVertex2iv(q4);
glEnd();
}

void rotate_origin() {
glClear(GL_COLOR_BUFFER_BIT);
before_rotation();
after_rotation();
glFlush();
}

9.2D Tranformations - builtin function - change the calling function to rotate or


scale or translate */
#include<stdio.h>
#include<GL/glut.h>

int v[3][2] = { {10, 10}, {240, 10}, {115, 220} };


void drawTriangle(int* a, int* b, int* c) {
glColor3f(1, 0, 0);
glBegin(GL_LINE_LOOP); //change it to GL_TRIANLGES if required
glVertex2iv(a);
glVertex2iv(b);
glVertex2iv(c);
glEnd();
glFlush();
}

void scale_triangle() {
float scaleFactor = .3;
drawTriangle(v[0], v[1], v[2]); //Before
glScalef(scaleFactor, scaleFactor, scaleFactor);
drawTriangle(v[0], v[1], v[2]); //After
}
void rotate_triangle() {
drawTriangle(v[0], v[1], v[2]); //Before
glRotatef(45, 0, 0, 1);
drawTriangle(v[0], v[1], v[2]); //After
}
void translate_triangle() {
float translateX = -100, translateY = -80;
drawTriangle(v[0], v[1], v[2]); //Before
glTranslatef(translateX, translateY, 0);
drawTriangle(v[0], v[1], v[2]); //After
}
void display() {
glClear(GL_COLOR_BUFFER_BIT);
translate_triangle();
glFlush();
}
void init() {
glClearColor(1, 1, 1, 1);
gluOrtho2D(-250, 250, -250, 250);
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutInitWindowPosition(10, 10);
glutCreateWindow("Triangle Rotation");
glutDisplayFunc(display);
init();
glutMainLoop();
return 0;
}

// User defined - Translate


#include <GL/glut.h>

float rectangleVertices[4][2] = { {50, 50}, {150, 50}, {150, 100}, {50, 100} };
float translationOffsetX = 50.0;
float translationOffsetY = 50.0;

void drawRectangle() {
glBegin(GL_QUADS);
for (int i = 0; i < 4; ++i) {
glVertex2f(rectangleVertices[i][0], rectangleVertices[i][1]);
}
glEnd();
}

void translateRectangle(float offsetX, float offsetY) {


for (int i = 0; i < 4; ++i) {
rectangleVertices[i][0] += offsetX;
rectangleVertices[i][1] += offsetY;
}
}

void display() {
glClear(GL_COLOR_BUFFER_BIT);

glColor3f(1.0, 0.0, 0.0); // Red color

// Original Rectangle
drawRectangle();

// Translated Rectangle
glColor3f(0.0, 0.0, 1.0); // Blue color
translateRectangle(translationOffsetX, translationOffsetY);
drawRectangle();

glFlush();
}

void init() {
glClearColor(1.0, 1.0, 1.0, 1.0);
gluOrtho2D(0, 300, 0, 300);
}

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


glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(300, 300);
glutInitWindowPosition(100, 100);
glutCreateWindow("Translation without Inbuilt Function");
init();
glutDisplayFunc(display);
glutMainLoop();

return 0;
}

//User Defined Scale


#include <GL/glut.h>
#include <math.h>

float rectangleVertices[4][2] = { {50, 50}, {150, 50}, {150, 100}, {50, 100} };
float scaleX=2, scaleY=2; // Rotation angle in degrees

void drawRectangle() {
glBegin(GL_QUADS);
glVertex2fv(rectangleVertices[0]);
glVertex2fv(rectangleVertices[1]);
glVertex2fv(rectangleVertices[2]);
glVertex2fv(rectangleVertices[3]);
glEnd();
}
void scaleRectangle(float xScale, float yScale) {
for (int i = 0; i < 4; i++) {
float x = rectangleVertices[i][0];
float y = rectangleVertices[i][1];

rectangleVertices[i][0] = xScale * x; //x' = {x*S(x)} - scaleValue of X


rectangleVertices[i][1] = yScale * y; //y' = {y*S(y)} - scaleValue of Y
}
}

void display() {
glClear(GL_COLOR_BUFFER_BIT);

glColor3f(1.0, 0.0, 0.0);


drawRectangle(); //before
glColor3f(0.0, 0.0, 1.0);
scaleRectangle(scaleX, scaleY);
drawRectangle();
glFlush();
}

void init() {
glClearColor(1.0, 1.0, 1.0, 1.0);
gluOrtho2D(0, 500, 0, 500);
}

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


glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(400, 400);
glutInitWindowPosition(100, 100);
glutCreateWindow("Rotation without Inbuilt Function");
init();
glutDisplayFunc(display);
glutMainLoop();

return 0;
}

// User defined rotate


#include <GL/glut.h>
#include <math.h>

float rectangleVertices[4][2] = { {50, 50}, {150, 50}, {150, 100}, {50, 100} };
float rotationAngle = 45.0; // Rotation angle in degrees

void drawRectangle() {
glBegin(GL_QUADS);
glVertex2fv(rectangleVertices[0]);
glVertex2fv(rectangleVertices[1]);
glVertex2fv(rectangleVertices[2]);
glVertex2fv(rectangleVertices[3]);
glEnd();
}

void rotateRectangle(float angle) {


float radians = angle * (3.14159 / 180.0); //degree to radian
for (int i = 0; i < 4; ++i) {
float x = rectangleVertices[i][0];
float y = rectangleVertices[i][1];
rectangleVertices[i][0] = x * cos(radians) - y * sin(radians); //x' =
x*cos(theta) - y*sin(theta)
rectangleVertices[i][1] = x * sin(radians) + y * cos(radians); //y' =
x*sin(theta) + y*cos(theta)
}
}

void display() {
glClear(GL_COLOR_BUFFER_BIT);

glColor3f(1.0, 0.0, 0.0);


drawRectangle(); //before
glColor3f(0.0, 0.0, 1.0);
rotateRectangle(rotationAngle); //after rotation
drawRectangle();
glFlush();
}

void init() {
glClearColor(1.0, 1.0, 1.0, 1.0);
gluOrtho2D(-200, 200, -200, 200);
}

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


glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(400, 400);
glutInitWindowPosition(100, 100);
glutCreateWindow("Rotation without Inbuilt Function");
init();
glutDisplayFunc(display);
glutMainLoop();

return 0;
}

//10. Mouse Function to plot Points


#include<GL/glut.h>
GLsizei winWidth = 400, winHeight = 300;
void init(void)
{
glClearColor(0.3, 0.3, 0.3, 0.3);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0.0, winWidth, 0.0, winHeight);
}
void displayFcn(void)
{
glClear(GL_COLOR_BUFFER_BIT); // Clear display window.
glColor3f(1.0, 1.0, 1.0); // Set point color to white.
glPointSize(10.0); // Set point size to 10.0.
}

void mousePtPlot(GLint button, GLint action, GLint xMouse, GLint yMouse)


{
if (button == GLUT_LEFT_BUTTON && action == GLUT_DOWN)
{
glBegin(GL_POINTS);
glVertex2i(xMouse, winHeight - yMouse);
glEnd();
}
glFlush();
}

void main(int argc, char** argv)


{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowPosition(100, 100);
glutInitWindowSize(winWidth, winHeight);
glutCreateWindow("Mouse Plot Points");
init();
glutDisplayFunc(displayFcn);
glutMouseFunc(mousePtPlot);
glutMainLoop();
}

//11. Mouse Function to plot LINES


#include<GL/glut.h>
int winWidth = 400, winHeight = 300;
int vertices[100][2] = { {10,10} }; // initialise a point
int n = 1; //size (variable)
void init()
{
glClearColor(0.3, 0.3, 0.3, 0.3);
gluOrtho2D(0.0, winWidth, 0.0, winHeight);
}
void displayFcn()
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 1.0, 1.0);
glPointSize(10.0);
}

void mouseLinePlot(int button, int action, int x, int y)


{
if (button == GLUT_LEFT_BUTTON && action == GLUT_DOWN)
{
// n = 1
vertices[n][0] = x;
vertices[n][1] = winHeight - y;
n++;
glBegin(GL_LINE_STRIP);
for (int j = 0; j < n; j++)
{
glVertex2i(vertices[j][0], vertices[j][1]);
}
glEnd();
}
glFlush();
}

void main(int argc, char** argv)


{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowPosition(100, 100);
glutInitWindowSize(winWidth, winHeight);
glutCreateWindow("Mouse Plot Points");
init();
glutDisplayFunc(displayFcn);
glutMouseFunc(mouseLinePlot);
glutMainLoop();
}

//12. keyboard to plot points


#include<stdio.h>
#include <GL/glut.h>
GLsizei winWidth = 400, winHeight = 300;
float ptSize = 10.0;

void init(void)
{
glClearColor(0.6, 0.3, 1, 0.4);

glMatrixMode(GL_PROJECTION);
gluOrtho2D(0.0, 200.0, 0.0, 150.0);
}
void displayFcn(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 0.0, 1.0);
glPointSize(3.0);
}
void winReshapeFcn(GLint newWidth, GLint newHeight)
{
glViewport(0, 0, newWidth, newHeight);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, (GLdouble)(newWidth), 0.0, (GLdouble)(newHeight));
winWidth = newWidth; winHeight = newHeight;
}
void plotPoint(GLint x, GLint y)
{
glPointSize(ptSize);
glBegin(GL_POINTS);
glVertex2i(x, y);
glEnd();
}
void curveDrawing(GLubyte curvePlotKey, GLint xMouse, GLint yMouse)
{
GLint x = xMouse;
GLint y = winHeight - yMouse;
switch (curvePlotKey)
{
case 'c': plotPoint(x, y);
break;
case '+': ptSize += 10;
break;
case '-': ptSize -= 10;
break;
default: break;
}
glFlush();
}
void main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowPosition(100, 100);
glutInitWindowSize(winWidth, winHeight);
glutCreateWindow("Keyboard Curve-Drawing Example");
init();
glutDisplayFunc(displayFcn);
glutReshapeFunc(winReshapeFcn);
glutKeyboardFunc(curveDrawing);
glutMainLoop();
}

13.Using recti function plot the bar graph to show average temp in every month.
#include<GL/glut.h>

int temp[12] = { 22, 29, 17, 19, 35, 42, 50, 32, 21, 23, 18, 45 };
char month[12][3] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug",
"Sep", "Oct", "Nov", "Dec" };
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_LINES);
glColor3f(0, 0, 0);
glVertex2f(50, 50);
glVertex2f(480, 50);
glVertex2f(50, 50);
glVertex2f(50, 480);
glEnd();
glColor3f(1, 0, 0);
for (int i = 0; i < 12; i++)
{
glRecti(60 + 30 * i, 50, 80 + 30 * i, temp[i] * (480 / 50));
}
for (int i = 0; i < 12; i++)
{
glColor3f(0, 0, 0);
glRasterPos2f(60 + 30 * i, 30);
for (int j = 0; j < 3; j++)
{
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12, month[i][j]);
}
}
glFlush();
}

void init()
{
glClearColor(1.0, 1.0, 1.0, 1.0);
gluOrtho2D(0, 500, 0, 500);
}

void main(int argc, char** argv)


{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(800, 500);
glutInitWindowPosition(0, 0);
glutCreateWindow("Graphs");
init();
glutDisplayFunc(display);
glutMainLoop();
}

theory part
THEORY:

1. DDA: The DDA (Digital Differential Analyzer) algorithm is a line-drawing


algorithm used in computer graphics to approximate the straight line between two
points in a discrete grid. It's a simple and efficient method for rasterizing
lines.
(followed by algorithm)

2. BRESENHAM: Bresenham's Line Drawing Algorithm is an efficient method used in


computer graphics to generate a pixel-perfect approximation of a straight line
between two endpoints on a discrete pixel grid. The algorithm excels in accuracy
and speed by leveraging integer arithmetic and a decision parameter to determine
the most appropriate pixels to plot at each step.(followed by algo)

3. different primitives and attributes:


Primitives:

Points:

Primitive Type: GL_POINTS


Description: Represents a single point in space.
Lines:

Primitive Types: GL_LINES, GL_LINE_STRIP, GL_LINE_LOOP


Description: Draws straight lines between pairs of vertices. GL_LINES connects
every two vertices independently, GL_LINE_STRIP connects consecutive vertices, and
GL_LINE_LOOP creates a closed loop by connecting the last vertex to the first.
Polygons:

Primitive Types: GL_POLYGON


Description: Renders a convex polygon defined by its vertices. Concave polygons may
not be rendered correctly.
Triangles:

Primitive Types: GL_TRIANGLES, GL_TRIANGLE_STRIP, GL_TRIANGLE_FAN


Description: Forms triangles using sets of three vertices. GL_TRIANGLES connects
every three vertices independently, GL_TRIANGLE_STRIP connects consecutive
vertices, and GL_TRIANGLE_FAN creates a fan-like arrangement.
Quads:

Primitive Types: GL_QUADS, GL_QUAD_STRIP


Description: Defines quadrilaterals using sets of four vertices. GL_QUADS connects
every four vertices independently, and GL_QUAD_STRIP connects consecutive vertices,
creating a strip of quadrilaterals.
Primitives for Geometry:

Primitive Types: GL_POINTS, GL_LINES, GL_TRIANGLES, etc.


Description: These include various combinations of points, lines, and triangles
used to represent more complex geometric shapes.

Attributes:

Color:

Attribute: glColor*
Description: Sets the color for subsequent primitives. Various functions like
glColor3f (for specifying RGB values) and glColor4f (including an alpha channel)
are used.
Vertex Coordinates:

Attribute: glVertex*
Description: Specifies the 2D or 3D coordinates of a vertex. Functions like
glVertex2f (for 2D) and glVertex3f (for 3D) are commonly used.
Line Width:

Attribute: glLineWidth
Description: Sets the width of lines drawn with GL_LINES and related primitives.
Polygon Mode:

Attribute: glPolygonMode
Description: Determines how polygons are rendered, allowing for wireframe (GL_LINE)
or filled (GL_FILL) modes.
Shading Model:

Attribute: glShadeModel
Description: Defines how colors are interpolated across primitives. Options include
GL_FLAT (constant color) and GL_SMOOTH (smooth color transition).
Texture Mapping:

Attributes: glTexCoord*
Description: Coordinates for texture mapping. Functions like glTexCoord2f are used
to assign texture coordinates.

4. SCANLINE ALGO:
The Scanline Algorithm is a graphics rendering technique used to fill the interiors
of polygons by scanning each horizontal line of the polygon and determining the
intersections of that line with the edges of the polygon. This algorithm is
particularly useful for rendering complex shapes with irregular boundaries.
(followed by algo)

5. CIRCLE DRAWING ALGO:A Circle Drawing Algorithm is a computational procedure used


in computer graphics to generate the coordinates of pixels that approximate a
circle when displayed on a discrete pixel grid. (followed by algo).

You might also like