0% found this document useful (0 votes)
27 views

Tinywow - Computer Graphics Experiments (2) - 41880454

The document contains the lab file for Computer Graphics for the 5th semester submitted by Jeetesh Saini. It includes the objectives and implementations of various computer graphics algorithms like the basics of OpenGL, Digital Differential Analyzer (DDA) line drawing algorithm, Bresenham's line drawing algorithm, circle midpoint algorithm, and more. Experiments were conducted to draw lines, circles, and patterns using these algorithms. Output screenshots are also included.

Uploaded by

xedewej976
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

Tinywow - Computer Graphics Experiments (2) - 41880454

The document contains the lab file for Computer Graphics for the 5th semester submitted by Jeetesh Saini. It includes the objectives and implementations of various computer graphics algorithms like the basics of OpenGL, Digital Differential Analyzer (DDA) line drawing algorithm, Bresenham's line drawing algorithm, circle midpoint algorithm, and more. Experiments were conducted to draw lines, circles, and patterns using these algorithms. Output screenshots are also included.

Uploaded by

xedewej976
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 77

CS55-Computer Graphics

Lab File
(2023 - 2024)
For
5th Semester

Submitted To: Submitted By:


Dr. Nancy Singla Jeetesh Saini-211436
TABLE OF CONTENTS

BASICS OF OPENGL........................................................................................I

DIGITAL DIFFERENTIAL ANALYZER...................................................................II

BRESENHAM’S LINE DRAWING ALGORITHM...................................................... III

BRESNEHAM’S CIRCLE DRAWING ALGORITHM.................................................IV

CIRCLE MIDPOINT ALGORITHM........................................................................V

ELLIPSE DRAWING ALGORITHM....................................................................... VI

BOUNDARY FILL ALGORITHM..........................................................................VII

FLOOD FILL ALGORITHM.................................................................................VII

COHEN SUTHERLAND LINE CLIPPING ALGORITHM............................................IX

LIANG BARSKY LINE CLIPPING ALGORITHM......................................................X

NICHOLL-LEE-NICHOLL LINE CLIPPING ALGORITHM.......................................... XI


Date- 04-08-2023 Jeetesh Saini -211436

Experiment 1
Objective: Basic of Opengl

Implementation:
1. Draw the following
a. Points
b. Line
c. Rectangle
d. Polyline using Line_Strip
e. Closed Polyline using Line_Loop

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

void setup(){
glClearColor(1.0f, 0.0f, 0.0f, 0.0f); // backgrouning
glPointSize(20);

void display(){

glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0f, 1.0f, 1.0f); // coloring rect
glRectf(-0.75f, 0.75f, 0.75f, -0.75f); // draw rect

glBegin( GL_POINTS);
glColor3f(0.0f, 0.0f, 0.0f);
glVertex2f(0.6f, -0.6f);
glVertex2f(0.5f, -0.6f);
glVertex2f(0.4f, -0.6f);
glEnd();

glBegin(GL_LINES);
glColor3f(0.0f, 0.0f, 0.0f); // coloring rect
glVertex2f(.25,0.25);
glVertex2f(.75,.75);
glEnd();

glBegin(GL_TRIANGLES); // Each set of 3 vertices form a triangle


glColor3f(0.f, 1.f, 0.f);
glVertex2f(0.0f, 0.3f);
glVertex2f(-0.2f, 0.0f);
glVertex2f(0.2f, 0.0f);
glEnd();

glBegin(GL_POLYGON);
glColor3f(0.0f, 1.0f, 0.0f);
Jeetesh Saini-211436

glVertex3f(-0.1, -0.1,0.0);
glVertex3f(-0.6, -0.1,0.0);
glVertex3f(-0.8,-0.3,0.0);
glVertex3f(-0.6,-0.6,0.0);
glVertex3f(-0.1,-0.6,0.0);
glVertex3f(-.0,-0.3,0.0);
glEnd();

glFlush();
glutSwapBuffers();
}

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

glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(400, 300); // horizontal, vertical
glutInitWindowPosition(200, 100); // horizontal, vertical
glutCreateWindow("Hello World"); // title of the window
setup();
glutDisplayFunc(display);
glutMainLoop();

return 0;
}
Output:
Jeetesh Saini-211436

Implementation: Draw the two specific patterns.


#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h> // Use this if you're using GLUT
#include <cmath>

const int numCircleSegments = 100; // Number of segments for the circle approximation

// Function to draw a triangle


void drawTriangle() {
glBegin(GL_TRIANGLES);
glVertex2f(-0.5f, -0.5f);
glVertex2f(0.5f, -0.5f);
glVertex2f(0.0f, 0.5f);
glEnd();
}

// Function to draw a square


void drawSquare() {
glBegin(GL_QUADS);
glVertex2f(-0.5f, -0.5f);
glVertex2f(0.5f, -0.5f);
glVertex2f(0.5f, 0.5f);
glVertex2f(-0.5f, 0.5f);
glEnd();
}

// Function to draw an approximate circle


void drawCircle(float radius, int numSegments) {
glBegin(GL_TRIANGLE_FAN);
glVertex2f(0.0f, 0.0f); // Center of the circle
for (int i = 0; i <= numSegments; ++i) {
float angle = 2.0f * 3.14159265358979323846f * static_cast<float>(i) /
static_cast<float>(numSegments);
float x = radius * std::cos(angle);
float y = radius * std::sin(angle);
glVertex2f(x, y);
}
glEnd();
}

// Display function
void display() {
glClear(GL_COLOR_BUFFER_BIT);

glColor3f(1.0f, 0.0f, 0.0f); // Set color to red

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

// Draw the triangle


Jeetesh Saini-211436

glPushMatrix();
glTranslatef(-0.5f, 0.5f, 0.0f);
drawTriangle();
glPopMatrix();

// Draw the square


glPushMatrix();
glTranslatef(0.5f, 0.5f, 0.0f);
drawSquare();
glPopMatrix();

// Draw the circle


glPushMatrix();
glTranslatef(0.0f, -0.7f, 0.0f);
drawCircle(0.3f, numCircleSegments);
glPopMatrix();

glFlush();
}

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


glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(800, 600);
glutCreateWindow("OpenGL Patterns");
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-1.0, 1.0, -1.0, 1.0);
glutDisplayFunc(display);
glutMainLoop();

return 0;
}

Output:
Jeetesh Saini-211436

Experiment 2
Objective: Implement DDA Line drawing algorithm.

Implementation:
#include <windows.h>
#include <iostream>
#include <GL/glut.h>
using namespace std;
void init(){
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0,640,0,480);

}
void dda_line(int x1, int y1, int x2, int y2){
int dx =x2 -x1; int dy= y2-y1;
int steps =1, length,x =x1,y=y1;
length =max(abs(dx), abs(dy));
int xinc = dx/length, yinc= dy/length;
while(steps<=length){
glBegin(GL_POINTS);
glVertex2d(x,y);
glEnd();
x+= xinc; y+=yinc;
steps++;
}
glutSwapBuffers();
}

void display(){
Jeetesh Saini-211436

glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0, 0, 0);
glPointSize(2.0);
dda_line(0,0,250,250);
}

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

glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(400, 300);
glutInitWindowPosition(200, 100);
glutCreateWindow("DDA LINE DRWAING");
glutDisplayFunc(display);
init();
glutMainLoop();
return 1;
}

Output:
Jeetesh Saini-211436
Jeetesh Saini-211436

Experiment 3
Objective: Bresenham’s Line Drawing Algorithm

Implementation:

#include <stdio.h>
#include <GL/gl.h>
#include <GL/glut.h>
float x1,y1,x2,y2,m,i,j,p;
int dx=0,dy=0;
void display(void)
{
glClear (GL_COLOR_BUFFER_BIT);
glEnd();

glColor3f (0.0, 1.0, 0.0);


glBegin(GL_POINTS);
p=(2*dy)-dx;
for(i=x1,j=y1;i<=x2,j<=y2; ){
if(p>=0){
i=i+1;
j=j+1;
if((i>x2)||(j>y2)){
break;
}
printf("%0.2f %0.2f\n",i,j);
glVertex3f ((i/100), (j/100), 0.0);
p=p+(2*dy)-(2*dx);
}
else if(p<0){
i=i+1;
if((i>x2)||(j>y2)){
Break;

}
printf("%0.2f %0.2f\n",i,j);
glVertex3f ((i/100), (j/100), 0.0);
p=p+(2*dy);
}
}
glEnd();
glFlush ();
}
void init (void)
{
glClearColor (0.0, 0.0, 0.0, 0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
Jeetesh Saini-211436

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

printf("Enter first point: ");


scanf("%f %f",&x1,&y1);
printf("Enter second point: ");
scanf("%f %f",&x2,&y2);
dx=x2-x1;
dy=y2-y1;
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize (500, 500);
glutInitWindowPosition (100, 100);
glutCreateWindow ("hello");
init ();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}

Output:
Jeetesh Saini-211436

Experiment 4
Objective:Circle Midpoint Algorithm
A:Display a circle

Implementation:
#include <GL/glut.h>
#include <cmath>

int width = 800;


int height = 800;

void plotPixel(int x, int y) {


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

void drawCircle(int centerX, int centerY, int radius) {


int x = 0;
int y = radius;
int p = 1 - radius;

while (x <= y) {
plotPixel(centerX + x, centerY + y);
plotPixel(centerX - x, centerY + y);
plotPixel(centerX + x, centerY - y);
plotPixel(centerX - x, centerY - y);
plotPixel(centerX + y, centerY + x);
plotPixel(centerX - y, centerY + x);
plotPixel(centerX + y, centerY - x);
plotPixel(centerX - y, centerY - x);

x++;

if (p < 0) {
p += 2 * x + 1;
} else {
y--;
p += 2 * (x - y) + 1;
}
}
}

void display() {
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 1.0, 1.0);

int centerX = width / 2; // Center of the display


int centerY = height / 2;
Jeetesh Saini-211436

int radius = 100;

drawCircle(centerX, centerY, radius);

glFlush();
}

void reshape(int w, int h) {


glViewport(0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, width, 0, height);
glMatrixMode(GL_MODELVIEW);
}

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


glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(width, height);
glutCreateWindow("Midpoint Circle Drawing
Algorithm");
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();
return 0;
}

Output:
Jeetesh Saini-211436

B:Display Concentric circles


Implementation:
#include <GL/glut.h>
#include <cmath>

int width = 800;


int height = 800;

void plotPixel(int x, int y) {


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

void drawCircle(int centerX, int centerY, int radius) {


int x = 0;
int y = radius;
int p = 1 - radius;

while (x <= y) {
plotPixel(centerX + x, centerY + y);
plotPixel(centerX - x, centerY + y);
plotPixel(centerX + x, centerY - y);
plotPixel(centerX - x, centerY - y);
plotPixel(centerX + y, centerY + x);
plotPixel(centerX - y, centerY + x);
plotPixel(centerX + y, centerY - x);
plotPixel(centerX - y, centerY - x);

x++;

if (p < 0) {
p += 2 * x + 1;
} else {
y--;
p += 2 * (x - y) + 1;
}
}
}

void drawConcentricCircles(int numCircles, int centerX, int centerY) {


for (int i = 0; i < numCircles; ++i) {
int radius = 50 * (i + 1); // You can adjust the spacing between circles here
drawCircle(centerX, centerY, radius);
}
}

void display() {
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 1.0, 1.0);
Jeetesh Saini-211436

int centerX = width / 2;


int centerY = height / 2;
int numCircles = 5; // Number of concentric circles

drawConcentricCircles(numCircles, centerX, centerY);

glFlush();
}

void reshape(int w, int h) {


glViewport(0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, width, 0, height);
glMatrixMode(GL_MODELVIEW);
}

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


glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(width, height);
glutCreateWindow("Concentric Circles using Midpoint Circle Drawing
Algorithm");
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();
return 0;
}

Output:

C: Display a display pattern


Implementation:
#include <GL/glut.h>
#include <cmath>
Jeetesh Saini-211436

int width = 800;


int height = 800;

void plotPixel(int x, int y) {


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

void drawCircle(int centerX, int centerY, int radius) {


int x = 0;
int y = radius;
int p = 1 - radius;

while (x <= y) {
plotPixel(centerX + x, centerY + y);
plotPixel(centerX - x, centerY + y);
plotPixel(centerX + x, centerY - y);
plotPixel(centerX - x, centerY - y);

x++;

if (p < 0) {
p += 2 * x + 1;
} else {
y--;
p += 2 * (x - y) + 1;
}
}
}

void drawConcentricCircles(int numCircles, int centerX, int centerY) {


for (int i = 0; i < numCircles; ++i) {
int radius = 50 * (i + 1); // You can adjust the spacing between circles here
drawCircle(centerX, centerY, radius);
}
}

void display() {
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 1.0, 1.0);

int centerX = width / 2;


int centerY = height / 2;
int numCircles = 5; // Number of concentric circles

drawConcentricCircles(numCircles, centerX, centerY);


drawConcentricCircles(numCircles, centerX, centerY); // Draw the opposite part

glFlush();
Jeetesh Saini-211436

void reshape(int w, int h) {


glViewport(0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, width, 0, height);
glMatrixMode(GL_MODELVIEW);
}

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


glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(width, height);
glutCreateWindow("Concentric Circles (Opposite Parts)");
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();
return 0;
}

Output:
Jeetesh Saini-211436

Experiment 5
Objective: Circle Bresenham’s Drawing Algorithm

Implementation:
1.
#include <stdio.h>
#include <iostream>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
#include <cmath>

int s1 = 250;
int s2 = 250;
int radius = 80;
void drawPixel(int x, int y) {
glBegin(GL_POINTS);
glVertex2i(x, y);
glEnd();
glFlush();
}
void drawCircle(int s1, int s2, int radius) {
int x = 0, y = radius;
int d = 3 - 2 * radius;

while (x <= y) {
drawPixel(s1 + x, s2 + y);
drawPixel(s1 - x, s2+ y);
drawPixel(s1 + x, s2 - y);
drawPixel(s1 - x, s2 - y);
drawPixel(s1 + y, s2 + x);
drawPixel(s1 - y, s2 + x);
drawPixel(s1+ y, s2 - x);
drawPixel(s1 - y,s2 - x);

if (d < 0) {
d = d + 4 * x + 6;
} else {
d = d + 4 * (x - y) + 10;
y--;
}
x++;
}
}
Jeetesh Saini-211436

void display() {
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 1.0, 1.0);
glPointSize(2.0);
drawCircle(s1, s2, radius);

glFlush();
}

void init() {
glClearColor(1.0, 0.0, 0.0, 0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, 500, 0, 500);
}

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


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

return 0;
}

output:
Jeetesh Saini-211436

2:
#include <stdio.h>
#include <iostream>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
#include <cmath>
int c1 = 280;
int c2 = 200;
int c3 = 240;
int c4 = 230;
int c5 = 310;
int c6 = 230;
int c7 = 280;
int c8 = 270;
int radius = 100;
int radius1 = 15;
int radius2 = 15;
int radius3 = 15;
void drawPixel(int x, int y) {

glBegin(GL_POINTS);

glVertex2i(x, y);

glEnd();

glFlush();

}
void drawCircle(int c1, int c2, int radius) {

int x = 0, y = radius;

int d = 3 - 2 * radius;

while (x <= y) {

drawPixel(c1 + x, c2 + y);

drawPixel(c1 - x, c2 + y);

drawPixel(c1 + x, c2 - y);

drawPixel(c1 - x, c2 - y);

drawPixel(c1 + y, c2 + x);

drawPixel(c1 - y, c2 + x);
Jeetesh Saini-211436

drawPixel(c1 + y, c2 - x);

drawPixel(c1 - y, c2 - x);

if (d < 0) {

d = d + 4 * x + 6;

} else {

d = d + 4 * x - 4*y + 10;

y--;

x++;

void display() {

glClear(GL_COLOR_BUFFER_BIT);

glColor3f(1.0, 1.0, 1.0);

glPointSize(2.0);

drawCircle(c1, c2, radius);

drawCircle(c3, c4, radius1);

drawCircle(c5, c6, radius2);

drawCircle(c7, c8, radius3);

glFlush();

}
void init() {

glClearColor(0.0, 0.0, 0.0, 0.0);

glMatrixMode(GL_PROJECTION);
Jeetesh Saini-211436

glLoadIdentity();

gluOrtho2D(0, 500, 0, 500);

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

glutInit(&argc, argv);

glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);

glutInitWindowSize(500, 500);

glutInitWindowPosition(100, 100);

glutCreateWindow("Bresenham's Circle Drawing


Algorithm");

init();

glutDisplayFunc(display);

glutMainLoop();
return 0;

3:
#include <stdio.h>
#include <iostream>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
#include <cmath>

int s1 = 270;
int s2 = 270;
int radius = 80;
Jeetesh Saini-211436

void drawPixel(int x, int y) {


glBegin(GL_POINTS);
glVertex2i(x, y);
glEnd();
glFlush();
}
void drawCircle(int s1, int s2, int radius) {
int x = 0, y = radius;
int d = 3 - 2 * radius;

while (x <= y) {
drawPixel(s1 + x, s2 + y);
drawPixel(s1 - x, s2+ y);
drawPixel(s1 + x, s2 - y);
drawPixel(s1 - x, s2 - y);
drawPixel(s1 + y, s2 + x);
drawPixel(s1 - y, s2 + x);
drawPixel(s1+ y, s2 - x);
drawPixel(s1 - y,s2 - x);

if (d < 0) {
d = d + 4 * x + 6;
} else {
d = d + 4 * (x - y) + 10;
y--;
}
x++;
}
}

void display() {
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 1.0, 1.0);
glPointSize(2.0);
drawCircle(s1, s2, radius);
drawCircle(220, 220, radius);
drawCircle(300, 220, radius);

glFlush();
}

void init() {
glClearColor(1.0, 0.0, 0.0, 0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, 500, 0, 500);
}

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


Jeetesh Saini-211436

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

return 0;
}

Output:
Jeetesh Saini-211436

Experiment 6
Objective:Ellipse mid point drawing algorithm
Implementation
A.
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
#include <cmath>

void plotEllipsePoints(int x, int y, int centerX, int centerY) {


glBegin(GL_POINTS);
glVertex2i(centerX + x, centerY + y);
glVertex2i(centerX - x, centerY + y);
glVertex2i(centerX + x, centerY - y);
glVertex2i(centerX - x, centerY - y);
glEnd();
}

void drawEllipse(int centerX, int centerY, int radiusX, int radiusY) {


int radiusX2 = radiusX * radiusX;
int radiusY2 = radiusY * radiusY;
int twoRadiusX2 = 2 * radiusX2;
int twoRadiusY2 = 2 * radiusY2;

int x = 0;
int y = radiusY;
int p;

int px = 0;
int py = twoRadiusX2 * y;

plotEllipsePoints(x, y, centerX, centerY);

// Region 1
p = round(radiusY2 - radiusX2 * radiusY + 0.25 * radiusX2);
while (px < py) {
x++;
px += twoRadiusY2;
if (p < 0) {
p += radiusY2 + px;
} else {
y--;
py -= twoRadiusX2;
p += radiusY2 + px - py;
}
plotEllipsePoints(x, y, centerX, centerY);
}
Jeetesh Saini-211436

// Region 2
p = round(radiusY2 * (x + 0.5) * (x + 0.5) + radiusX2 * (y - 1) * (y - 1) - radiusX2 * radiusY2);
while (y > 0) {
y--;
py -= twoRadiusX2;
if (p > 0) {
p += radiusX2 - py;
} else {
x++;
px += twoRadiusY2;
p += radiusX2 - py + px;
}
plotEllipsePoints(x, y, centerX, centerY);
}
}

void display() {
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 1.0, 1.0);
glPointSize(2.0);

// Call the ellipse drawing function with center (400, 300), radii 200 and 100
drawEllipse(400, 300, 200, 100);

glFlush();
}

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


glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(800, 600);
glutCreateWindow("Midpoint Ellipse Drawing Algorithm");
glClearColor(0.0, 0.0, 0.0, 1.0);
gluOrtho2D(0, 800, 0, 600);
glutDisplayFunc(display);
glutMainLoop();
return 0;
}

Output:
Jeetesh Saini-211436

b
.#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
#include <cmath>

void plotEllipsePoints(int x, int y, int centerX, int centerY) {


glBegin(GL_POINTS);
glVertex2i(centerX + y, centerY + x);
glVertex2i(centerX - y, centerY + x);
glVertex2i(centerX + y, centerY - x);
glVertex2i(centerX -y, centerY - x);
glEnd();
}

void drawEllipse(int centerX, int centerY, int radiusX, int radiusY) {


int radiusX2 = radiusX * radiusX;
int radiusY2 = radiusY * radiusY;
int twoRadiusX2 = 2 * radiusX2;
int twoRadiusY2 = 2 * radiusY2;

int x = 0;
int y = radiusY;
int p;

int px = 0;
int py = twoRadiusX2 * y;

plotEllipsePoints(x, y, centerX, centerY);

p = round(radiusY2 - radiusX2 * radiusY + 0.25 * radiusX2);


while (px < py) {
x++;
px += twoRadiusY2;
if (p < 0) {
p += radiusY2 + px;
Jeetesh Saini-211436

} else {
y--;
py -= twoRadiusX2;
p += radiusY2 + px - py;
}
plotEllipsePoints(x, y, centerX, centerY);
}

p = round(radiusY2 * (x + 0.5) * (x + 0.5) + radiusX2 * (y - 1) * (y - 1) - radiusX2 * radiusY2);


while (y > 0) {
y--;
py -= twoRadiusX2;
if (p > 0) {
p += radiusX2 - py;
} else {
x++;
px += twoRadiusY2;
p += radiusX2 - py + px;
}
plotEllipsePoints(x, y, centerX, centerY);
}
}

void display() {
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 1.0, 1.0);
glPointSize(2.0);

drawEllipse(400, 300, 200, 100);

glFlush();
}

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


glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(800, 600);
glutCreateWindow("Midpoint Ellipse Drawing Algorithm");
glClearColor(0.0, 0.0, 0.0, 1.0);
gluOrtho2D(0, 800, 0, 600);
glutDisplayFunc(display);
glutMainLoop();
return 0;
}

Output:
Jeetesh Saini-211436

C.
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
#include <cmath>

void plotEllipsePoints(int x, int y, int centerX, int centerY) {


glBegin(GL_POINTS);
glVertex2i(centerX + y, centerY + x);
glVertex2i(centerX - y, centerY + x);
glVertex2i(centerX + y, centerY - x);
glVertex2i(centerX - y, centerY - x);
glEnd();
}
void plotEllipsePoint1(int x, int y, int centerX, int centerY) {
glBegin(GL_POINTS);
glVertex2i(centerX + x, centerY + y);
glVertex2i(centerX - x, centerY + y);
glVertex2i(centerX + x, centerY - y);
glVertex2i(centerX - x, centerY - y);
glEnd();
}

void drawEllipse(int centerX, int centerY, int radiusX, int radiusY) {


int radiusX2 = radiusX * radiusX;
int radiusY2 = radiusY * radiusY;
int twoRadiusX2 = 2 * radiusX2;
int twoRadiusY2 = 2 * radiusY2;

int x = 0;
int y = radiusY;
int p;

int px = 0;
Jeetesh Saini-211436

int py = twoRadiusX2 * y;

plotEllipsePoints(x, y, centerX, centerY);

plotEllipsePoint1(x, y, centerX, centerY);

// Region 1
p = round(radiusY2 - radiusX2 * radiusY + 0.25 * radiusX2);
while (px < py) {
x++;
px += twoRadiusY2;
if (p < 0) {
p += radiusY2 + px;
} else {
y--;
py -= twoRadiusX2;
p += radiusY2 + px - py;
}
plotEllipsePoints(x, y, centerX, centerY);
plotEllipsePoint1(x, y, centerX, centerY);
}

// Region 2
p = round(radiusY2 * (x + 0.5) * (x + 0.5) + radiusX2 * (y - 1) * (y - 1) - radiusX2 * radiusY2);
while (y > 0) {
y--;
py -= twoRadiusX2;
if (p > 0) {
p += radiusX2 - py;
} else {
x++;
px += twoRadiusY2;
p += radiusX2 - py + px;
}
plotEllipsePoints(x, y, centerX, centerY);
plotEllipsePoint1(x, y, centerX, centerY);
}
}

void display() {
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 1.0, 1.0);
glPointSize(2.0);

drawEllipse(400, 300, 200, 100);

glFlush();
}

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


Jeetesh Saini-211436

glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(800, 600);
glutCreateWindow("Midpoint Ellipse Drawing Algorithm");
glClearColor(0.0, 0.0, 0.0, 1.0);
gluOrtho2D(0, 800, 0, 600);
glutDisplayFunc(display);
glutMainLoop();
return 0;
}

Output:
Jeetesh Saini-211436

Experiment: 7
Objective: BOUNDARY FILLING ALGORITHM
Implementation:
#include <windows.h>

#include <gl/glut.h>

#include <stdio.h>

typedef struct pixel

GLubyte red, green, blue;

} pixel;

void setPixel(int x, int y, pixel fill)

glBegin(GL_POINTS);

glColor3ub(fill.red, fill.green, fill.blue);

glVertex2i(x, y);

glEnd();

glFlush();

pixel getPixel(int x, int y)

pixel c;

glReadPixels(x, y, 1, 1, GL_RGB, GL_UNSIGNED_BYTE, &c);

return c;

void boundaryFill(int x, int y, pixel fill, pixel boundary)

pixel current = getPixel(x, y);

if (current.red != boundary.red || current.green != boundary.green || current.blue !=

boundary.blue)

{
Jeetesh Saini-211436

if (current.red != fill.red || current.green != fill.green || current.blue != fill.blue)

setPixel(x, y, fill);

boundaryFill(x + 1, y, fill, boundary);

boundaryFill(x - 1, y, fill, boundary);

boundaryFill(x, y + 1, fill, boundary);

boundaryFill(x, y - 1, fill, boundary);

void drawSquare()

glColor3ub(255, 0, 0); // Red color for the square boundary

glBegin(GL_LINE_LOOP);

glVertex2i(100, 100);

glVertex2i(300, 100);

glVertex2i(300, 300);

glVertex2i(100, 300);

glEnd();

void drawCircle()

int radius = 50;

int centerX = 200;

int centerY = 200;

int x = 0;

int y = radius;

int p = 1 - radius;

glColor3ub(255, 255, 0); // Yellow color for the circle boundary

glBegin(GL_POINTS);

while (x <= y)
Jeetesh Saini-211436

glVertex2i(centerX + x, centerY + y);

glVertex2i(centerX - x, centerY + y);

glVertex2i(centerX + x, centerY - y);

glVertex2i(centerX - x, centerY - y);

glVertex2i(centerX + y, centerY + x);

glVertex2i(centerX - y, centerY + x);

glVertex2i(centerX + y, centerY - x);

glVertex2i(centerX - y, centerY - x);

if (p < 0)

p += 2 * x + 1;

else

y--;

p += 2 * (x - y) + 1;

x++;

glEnd();

void fillCircle()

pixel fill, boundary;

fill.red = 255;

fill.green = 0;

fill.blue = 0;

boundary.red = 255;

boundary.green = 255;

boundary.blue = 0;
Jeetesh Saini-211436

int startX = 200;

int startY = 200;

boundaryFill(startX, startY, fill, boundary);

void mydisplay()

glClear(GL_COLOR_BUFFER_BIT);

drawSquare();

drawCircle();

fillCircle();

int main(int argc, char **argv)

glutInit(&argc, argv);

glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);

glutInitWindowSize(400, 400);

glutInitWindowPosition(540, 320);

glutCreateWindow("Boundary Fill Algorithm");

glClearColor(1.0f, 1.0f, 1.0f, 0.0f);

glClear(GL_COLOR_BUFFER_BIT);

glutDisplayFunc(mydisplay);

gluOrtho2D(0.0, 400.0, 0.0, 400.0);

glutMainLoop();

return 0;

Output:
Jeetesh Saini-211436

Experiment: 8
Objective: Flood FILLING ALGORITHM
Implementation:

#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
#include <cmath>
#include <stdio.h>

int screen[400][400];
void floodFill(int x, int y, int oldColor, int newColor) {
if (x < 0 || x >= 400 || y < 0 || y >= 400) {
return;
}

if (screen[x][y] != oldColor) {
return; // Color already changed or not the target color
}

screen[x][y] = newColor; // Set new color

// Recursively fill in all directions


floodFill(x + 1, y, oldColor, newColor);
floodFill(x - 1, y, oldColor, newColor);
floodFill(x, y + 1, oldColor, newColor);
floodFill(x, y - 1, oldColor, newColor);
}

// Function to draw a rectangle


void drawRectangle(int x1, int y1, int x2, int y2) {
glColor3f(0.0, 0.0, 0.0); // Set color to black
glBegin(GL_POLYGON);
glVertex2i(x1, y1);
glVertex2i(x2, y1);
glVertex2i(x2, y2);
glVertex2i(x1, y2);
glEnd();
glFlush();
}

// Display callback function


void display() {
glClear(GL_COLOR_BUFFER_BIT);

// Draw the rectangle


drawRectangle(250, 250, 300, 300);

glFlush();
Jeetesh Saini-211436

// Mouse click callback function


void mouseClick(int button, int state, int x, int y) {
if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) {
// Perform flood fill starting from the clicked point
floodFill(x, 400 - y, 0, 1); // 0 is the old color, 1 is the new color (green)
glutPostRedisplay();
}
}

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


glutInit(&argc, argv);
glutInitWindowSize(400, 400);
glutCreateWindow("Flood Fill Example");
glClearColor(1.0, 1.0, 1.0, 1.0); // Set background color to black
gluOrtho2D(0, 400, 0, 400);
glutDisplayFunc(display);
glutMouseFunc(mouseClick);
glutMainLoop();
return 0;
}

Output:
Jeetesh Saini-211436

Experiment: 9
Objective: Sutherland line clipping ALGORITHM
Implementation::

#include <GL/glut.h>
#include <GL/gl.h>
#include <iostream>
#include <cmath>

using namespace std;

const int INSIDE = 0; // 0000


const int LEFT = 1; // 0001
const int RIGHT = 2; // 0010
const int BOTTOM = 4; // 0100
const int TOP = 8; // 1000

const int x_max = 400;


const int y_max = 300;
const int x_min = 100;
const int y_min = 100;

int computeCode(double x, double y) {


int code = INSIDE;

if (x < x_min) {
code |= LEFT;
} else if (x > x_max) {
code |= RIGHT;
}

if (y < y_min) {
code |= BOTTOM;
} else if (y > y_max) {
code |= TOP;
}

return code;
}

void cohenSutherland(double x1, double y1, double x2, double y2)


{
int code1 = computeCode(x1, y1);
int code2 = computeCode(x2, y2);
bool accept = false;

while (true) {
Jeetesh Saini-211436

if ((code1 == 0) && (code2 == 0)) {


accept = true;
break;
} else if (code1 & code2) {
break;
} else {
int code_out;
double x, y;

if (code1 != 0) {
code_out = code1;
} else {
code_out = code2;
}

if (code_out & TOP) {


x = x1 + (x2 - x1) * (y_max - y1) / (y2 - y1);
y = y_max;
} else if (code_out & BOTTOM) {
x = x1 + (x2 - x1) * (y_min - y1) / (y2 - y1);
y = y_min;
} else if (code_out & RIGHT) {
y = y1 + (y2 - y1) * (x_max - x1) / (x2 - x1);
x = x_max;
} else {
y = y1 + (y2 - y1) * (x_min - x1) / (x2 - x1);
x = x_min;
}

if (code_out == code1) {
x1 = x;
y1 = y;
code1 = computeCode(x1, y1);
} else {
x2 = x;
y2 = y;
code2 = computeCode(x2, y2);
}
}
}

if (accept) {
glBegin(GL_LINES);
glVertex2d(x1, y1);
glVertex2d(x2, y2);
glEnd();
}
}

void display() {
glClear(GL_COLOR_BUFFER_BIT);
Jeetesh Saini-211436

glColor3f(0.0, 0.0, 0.0);


glBegin(GL_LINE_LOOP);
glVertex2i(x_min, y_min);
glVertex2i(x_max, y_min);
glVertex2i(x_max, y_max);
glVertex2i(x_min, y_max);
glEnd();

cohenSutherland(50, 200, 300, 200);


cohenSutherland(200, 50, 200, 300);
cohenSutherland(0, 0, 640, 480);

glFlush();
}

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


glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(640, 480);
glutCreateWindow("Cohen-Sutherland Line Clipping Algorithm");
glClearColor(1.0, 1.0, 1.0, 0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, 640, 0, 480);
glutDisplayFunc(display);
glutMainLoop();
return 0;
}

Output:
Jeetesh Saini-211436

Experiment 10

Objective: Liang Barsky Line clipping algorithm


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

double xmin=50, ymin=50, xmax=100,ymax=100; //window boundaries


double xvmin=200,yvmin=200,xvmax=300, yvmax=300; //viewport boundaries
int x1,y1,x2,y2;
int cliptest(double p,double q,double *t1,double *t2)
{
double t=q/p;
if(p<0.0)
{ if(t>*t1) *t1=t;
if(t>*t2) return(0);
}
else if(p>0.0)
{
if(t<*t2) *t2=t;
if(t<*t1) return(0); //line portion is outside
}
else if(p==0.0)
{ if(q<0.0)
return(0);//line parallel to edge but outside
}
return(1);
}
void liangbaraskylineclipanddraw(double x0,double y0,double x1,double y1)
{
double dx= x1-x0,dy=y1-y0, te=0.0,tl=1.0;if(cliptest(-dx,x0-xmin,&te,&tl))
if(cliptest(dx,xmax-x0,&te,&tl))
if(cliptest(-dy,y0-ymin,&te,&tl))
if(cliptest(dy,ymax-y0,&te,&tl))
{
if(tl<1.0)
{
x1=x0+tl*dx;
y1=y0+tl*dy;
}
if(te>0.0)
{
x0=x0+te*dx;
y0=y0+te*dy;
}
Jeetesh Saini-211436

}
//window to viewport mappings
double sx= (xvmax-xvmin)/(xmax-xmin);// scale parameters
double sy= (yvmax-yvmin)/(ymax-ymin);
double vx0= xvmin+(x0-xmin)*sx;
double vy0= yvmin+(y0-ymin)*sy;
double vx1= xvmin+(x1-xmin)*sx;
double vy1= yvmin+(y1-ymin)*sy;
// draw a red colored viewport
glColor3f(1.0,0.0,0.0);
glBegin(GL_LINE_LOOP);
glVertex2f(xvmin,yvmin);
glVertex2f(xvmax,yvmin);
glVertex2f(xvmax,yvmax);
glVertex2f(xvmin,yvmax);
glEnd();
glColor3f(0.0,0.0,1.0); //draw blue colored clip line
glBegin(GL_LINES);
glVertex2d(vx0,vy0);
glVertex2f(vx1,vy1);
glEnd();

}
void display()
{
double x0=60,y0=20,x1=80,y1=120;
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0,0.0,0.0); //draw a line with red color
glBegin(GL_LINES);
glVertex2d(x0,y0);
glVertex2d(x1,y1);
glEnd();
glColor3f(0.0,0.0,1.0); //draw a blue color window
glBegin(GL_LINE_LOOP);
glVertex2f(xmin,ymin);
glVertex2f(xmax,ymin);
glVertex2f(xmax,ymax);
glVertex2f(xmin,ymax);
glEnd();
liangbaraskylineclipanddraw(x0,y0,x1,y1);
glFlush();
}
void myinit()
{ glClearColor(1.0,1.0,1.0,1.0);
glColor3f(1.0,0.0,0.0);
glPointSize(1.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0,499.0,0.0,499.0);
}
int main(int argc, char** argv)
Jeetesh Saini-211436

{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(500,500);
glutInitWindowPosition(0,0);
glutCreateWindow("liang barsky line clipping algorithm");
glutDisplayFunc(display);
myinit();
glutMainLoop();
return 0;
}

Output:
Jeetesh Saini-211436

Experiment 11: Nicoll-Lee-Nicoll Line


Clipping Algorithm
Implementation:
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
#include <stdio.h>

int xmin, ymin, xmax, ymax;


int x1, y1, x2, y2;

void drawRectangle() {
glBegin(GL_LINE_LOOP);
glVertex2f(xmin, ymin);
glVertex2f(xmax, ymin);
glVertex2f(xmax, ymax);
glVertex2f(xmin, ymax);
glEnd();
}

void drawLine(int x1, int y1, int x2, int y2) {


glBegin(GL_LINES);
glVertex2f(x1, y1);
glVertex2f(x2, y2);
glEnd();
}

void display() {
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 1.0, 1.0);

drawRectangle();

glColor3f(1.0, 0.0, 0.0);


drawLine(x1, y1, x2, y2);

// Implement your clipping logic and draw the clipped line here
// You can replace the clipping functions with OpenGL drawing functions
// For example, drawLine(clippedX1, clippedY1, clippedX2, clippedY2);

glFlush();
}

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


switch (key) {
case 'q':
case 'Q':
case 27: // Escape key
Jeetesh Saini-211436

exit(0);
break;
}
}

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


printf("\nEnter the xmin:->");
scanf("%d", &xmin);
printf("\nEnter the ymin:->");
scanf("%d", &ymin);
printf("\nEnter the xmax:->");
scanf("%d", &xmax);
printf("\nEnter the ymax:->");
scanf("%d", &ymax);

printf("Enter the x1:->");


scanf("%d", &x1);
printf("Enter the y1:->");
scanf("%d", &y1);
printf("Enter the x2:->");
scanf("%d", &x2);
printf("Enter the y2:->");
scanf("%d", &y2);

glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(400, 400);
glutCreateWindow("Line Clipping");

// Set the coordinate system


glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(xmin - 1, xmax + 1, ymin - 1, ymax + 1);

glutDisplayFunc(display);
glutKeyboardFunc(keyboard);

glutMainLoop();

return 0;
}

Output:
Jeetesh Saini-211436

Experiment 12: Sutherland Hodgeman


Polygon Clipping Algorithm.
Implementation:
#include <iostream>

#include <math.h>

#include <time.h>

#include <GL/glut.h>

#include <GL/glu.h>

#include <GL/gl.h>

#include <list>

using namespace std;

void init(){

glClearColor(1.0,1.0,1.0,1.0);

glMatrixMode(GL_PROJECTION);

gluOrtho2D(0,640,0,480);

int xmin = 0,ymin = 0,xmax = 0,ymax = 0;

int enter = 1,sz,st_flag=1;

float** pts;

class points{

int x;

int y;

public:

points(int x,int y){

this->x = x;
Jeetesh Saini-211436

this->y = y;

int getx(){

return x;

int gety(){

return y;

};

class tryo{

int x;

int y;

public:

void setx(int x){this->x = x;}

int getx(){return x;}

};

points *s,*p;

list <points*> in;

list <points*> outer;

void delay(float ms){

clock_t goal = ms + clock();

while(goal>clock());

void drawPolygon(){

glBegin(GL_LINE_LOOP);

pts = new float*[in.size()];

for(int i=0; i<in.size(); i++){

pts[i] = new float[2];

sz = in.size();

while(in.size()>0){
Jeetesh Saini-211436

points* temp = in.front();

pts[in.size()-1][0] = temp->getx();

pts[in.size()-1][1] = temp->gety();

glVertex2i(temp->getx(),temp->gety());

in.pop_front();

glEnd();

glFlush();

void redraw(){

glClear(GL_COLOR_BUFFER_BIT);

glBegin(GL_LINE_LOOP);

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

glVertex2i(pts[i][0],pts[i][1]);

glEnd();

glFlush();

glColor3f(0,0,0);

glBegin(GL_LINE_LOOP);

glVertex2i(xmin,ymin);

glVertex2i(xmin,ymax);

glVertex2i(xmax,ymax);

glVertex2i(xmax,ymin);

glEnd();

glFlush();

glColor3f(1,0,0);

glLineWidth(1.0);

int inside(int x, int y, int clip_edge){

switch(clip_edge){

case 1: if(x<xmax) return 1; else return 0;break;


Jeetesh Saini-211436

case 2: if(y>ymax) return 1; else return 0;break;

case 3: if(x>xmin) return 1; else return 0;break;

case 4: if(y<ymin) return 1; else return 0;break;

default: return 0;break;

points* intersect(points* S, points* P, int clip_edge){

float m; //div by zero error earlier

if((P->getx()-S->getx())==0)

m = 0;

else

m = (float) (P->gety()-S->gety())/(P->getx()-S->getx());

float c = (float) (S->gety()) - (m*S->getx());

if(clip_edge==1){int x = xmax; int y = (m*x)+c;return (new points(x,y));} //bug wasbecause of m=0


thing again

if(clip_edge==2){int y = ymax; int x; if(m==0) x = P->getx(); else x = (y-c)/m;return (new

points(x,y));}

if(clip_edge==3){int x = xmin; int y = (m*x)+c;return (new points(x,y));}

if(clip_edge==4){int y = ymin; int x; if(m==0) x = P->getx(); else x = (y-c)/m;return (new

points(x,y));}

float** out_to_in(float** inner, list<points*> out){

inner = new float*[out.size()];

for(int i=0; i<out.size(); i++){

inner[i] = new float[2];

sz = out.size();

while(out.size()>0){

points* temp = out.front();

inner[out.size()-1][0] = temp->getx();

inner[out.size()-1][1] = temp->gety();
Jeetesh Saini-211436

out.pop_front();

out.empty();

return inner;

float** SHPC(float** inva, list<points*> out,int clip_edge){

s = new points(inva[sz-1][0],inva[sz-1][1]);

for(int j=0; j<sz; j++){

p = new points(inva[j][0],inva[j][1]);

if(inside(p->getx(),p->gety(),clip_edge))

if(inside(s->getx(),s->gety(),clip_edge)){

out.push_front(new points(p->getx(),p->gety()));

else{ // case 4

points* temp = intersect(s,p,clip_edge);

out.push_front(temp);

out.push_front(p);

else if(inside(s->getx(),s->gety(),clip_edge)){ //case 2

points* temp = intersect(s,p,clip_edge);

out.push_front(temp);

else{

s = p;

inva = out_to_in(inva,out);

return inva;

}
Jeetesh Saini-211436

void key(unsigned char key_t, int x, int y){

if(key_t=='d'){

enter = -1;

drawPolygon();

in.empty();

if(key_t=='c'){

pts = SHPC(pts,outer,1);

pts = SHPC(pts,outer,2);

pts = SHPC(pts,outer,3);

pts = SHPC(pts,outer,4);

redraw(); }}

void mouse(int btn, int state, int x, int y){

y = 480-y;

if(btn==GLUT_LEFT_BUTTON && state==GLUT_DOWN && enter) {

points* temp = new points(x,y);

in.push_front(temp); }}

void drag_start(GLint x, GLint y){

y = 480-y;

if(enter==-1&&st_flag){

xmin = x;

ymin = y;

st_flag = 0;

else{

xmax = x;

ymax = y;

redraw();

void drag_end(GLint x, GLint y){


Jeetesh Saini-211436

y = 480-y;

if(enter==-1&&st_flag==0){

xmax = x;

ymax = y;

st_flag = 1;

redraw();}}

void world(){

glPointSize(2);

glClear(GL_COLOR_BUFFER_BIT);

glColor3f(1,0,0);

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

glutInit(&argc, argv);

glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);

glutInitWindowSize(640,480);

glutInitWindowPosition(200,200);

glutCreateWindow("polygon clipping");

glutDisplayFunc(world);

glutMouseFunc(mouse);

glutMotionFunc(drag_start);

glutPassiveMotionFunc(drag_end);

glutKeyboardFunc(key);

init();

glutMainLoop();

return 0;

Output:
Jeetesh Saini-211436

Experiment 13: Weiler Atherton


Algorithm
Implementation:
#include<stdio.h>
#include<GL/gl.h>
#include<GL/glu.h>
#include<GL/glut.h>
#include<math.h>

typedef struct // structure that holds the information of points


{
float x;
float y;
}PT;

int n;
int i,j;
PT p1,p2,p[20],pp[20];
void left() // left clipper
{
i=0;j=0;
for(i=0;i<n;i++)
{if(p[i].x<p1.x && p[i+1].x>=p1.x) //Case-1: outside to inside{
if(p[i+1].x-p[i].x!=0)
{
pp[j].y=(p[i+1].y-p[i].y)/(p[i+1].x-p[i].x)*(p1.x-p[i].x)+p[i].y;
// save point of intersection}
else
{
pp[j].y=p[i].y;
}
pp[j].x=p1.x;
j++;
pp[j].x=p[i+1].x; // save that point that lie
inside our clipping window // consult theory
pp[j].y=p[i+1].y;
Jeetesh Saini-211436

j++;

if(p[i].x>=p1.x && p[i+1].x>=p1.x) //Case-2: inside to inside


{
pp[j].y=p[i+1].y; // only save second point
that lie inside our clipping window // consult theory
pp[j].x=p[i+1].x;
j++;
}

if(p[i].x>=p1.x && p[i+1].x<p1.x) // Case-3: inside to outside


{
if(p[i+1].x-p[i].x!=0)
{
pp[j].y=(p[i+1].y-p[i].y)/(p[i+1].x-p[i].x)*(p1.x-p[i].x)+p[i].y;
// only save point of intersection
}
else
{
pp[j].y=p[i].y;
}
pp[j].x=p1.x;
j++;
}
}

for(i=0;i<j;i++)
{
p[i].x=pp[i].x;
p[i].y=pp[i].y;
}

p[i].x=pp[0].x;
p[i].y=pp[0].y;
n=j;
}

void right() // right clipper


{
i=0;j=0;

for(i=0;i<n;i++)
{
if(p[i].x>p2.x && p[i+1].x<=p2.x) //Case-1: outside to inside
{
if(p[i+1].x-p[i].x!=0)
{
pp[j].y=(p[i+1].y-p[i].y)/(p[i+1].x-p[i].x)*(p2.x-p[i].x)+p[i].y;
// save point of intersection
}
else
Jeetesh Saini-211436

{
pp[j].y=p[i].y;
}
pp[j].x=p2.x;
j++;
pp[j].x=p[i+1].x; // save that
point that lie inside our clipping window // consult theory
pp[j].y=p[i+1].y;
j++;
}

if(p[i].x<=p2.x && p[i+1].x<=p2.x) // Case-2: inside to inside


{
pp[j].y=p[i+1].y; // only save second point
that lie inside our clipping window // consult theory
pp[j].x=p[i+1].x;
j++;
}

if(p[i].x<=p2.x && p[i+1].x>p2.x) // Case-3: inside to outside


{
if(p[i+1].x-p[i].x!=0)
{
pp[j].y=(p[i+1].y-p[i].y)/(p[i+1].x-p[i].x)*(p2.x-p[i].x)+p[i].y;
// only save point of intersection
}
else
{
pp[j].y=p[i].y;
}
pp[j].x=p2.x;
j++;
}

for(i=0;i<j;i++)
{
p[i].x=pp[i].x;
p[i].y=pp[i].y;
}

p[i].x=pp[0].x;
p[i].y=pp[0].y;

void top() // top clipper


{
i=0;j=0;

for(i=0;i<n;i++)
{
if(p[i].y>p2.y && p[i+1].y<=p2.y) //Case-1: outside to inside
Jeetesh Saini-211436

{
if(p[i+1].y-p[i].y!=0)
{
pp[j].x=(p[i+1].x-p[i].x)/(p[i+1].y-p[i].y)*(p2.y-p[i].y)+p[i].x;
// save point of intersection
}
else
{
pp[j].x=p[i].x;
}
pp[j].y=p2.y;
j++;
pp[j].x=p[i+1].x; // save that point
that lie inside our clipping window // consult theory
pp[j].y=p[i+1].y;
j++;
}

if(p[i].y<=p2.y && p[i+1].y<=p2.y) // Case-2: inside to inside


{
pp[j].y=p[i+1].y; // only save
second point that lie inside our clipping window // consult theory
pp[j].x=p[i+1].x;
j++;
}

if(p[i].y<=p2.y && p[i+1].y>p2.y) // Case-3: inside to outside


{
if(p[i+1].y-p[i].y!=0)
{
pp[j].x=(p[i+1].x-p[i].x)/(p[i+1].y-p[i].y)*(p2.y-p[i].y)+p[i].x;
// only save point of intersection
}
else
{
pp[j].x=p[i].x;
}
pp[j].y=p2.y;
j++;
}
}

for(i=0;i<j;i++)
{
p[i].x=pp[i].x;
p[i].y=pp[i].y;
}

p[i].x=pp[0].x;
p[i].y=pp[0].y;
n=j;
}
Jeetesh Saini-211436

void bottom() // bottom clipper


{
i=0;j=0;

for(i=0;i<n;i++)
{
if(p[i].y<p1.y && p[i+1].y>=p1.y) // Case-1: outside to inside
{
if(p[i+1].y-p[i].y!=0)
{
pp[j].x=(p[i+1].x-p[i].x)/(p[i+1].y-p[i].y)*(p1.y-p[i].y)+p[i].x;
// save point of intersection
}
else
{
pp[j].x=p[i].x;
}
pp[j].y=p1.y;
j++;
pp[j].x=p[i+1].x; // save that
point that lie inside our clipping window // consult theory
pp[j].y=p[i+1].y;
j++;
}

if(p[i].y>=p1.y && p[i+1].y>=p1.y) // Case-2: inside to inside


{
pp[j].x=p[i+1].x; // only save
second point that lie inside our clipping window // consult theory
pp[j].y=p[i+1].y;
j++;
}

if(p[i].y>=p1.y && p[i+1].y<p1.y) // Case-3: inside to outside


{
if(p[i+1].y-p[i].y!=0)
{
pp[j].x=(p[i+1].x-p[i].x)/(p[i+1].y-p[i].y)*(p1.y-p[i].y)+p[i].x;
// only save point of intersection
}
else
{
pp[j].x=p[i].x;
}
pp[j].y=p1.y;
j++;
}
}

for(i=0;i<j;i++)
{
p[i].x=pp[i].x;
p[i].y=pp[i].y;
}
Jeetesh Saini-211436

p[i].x=pp[0].x;
p[i].y=pp[0].y;
n=j;
}

void drawpolygon()
{
glColor3f(1.0,0.0,0.0);
for(i=0;i<n-1;i++)
{
glBegin(GL_LINES);
glVertex2d(p[i].x,p[i].y);
glVertex2d(p[i+1].x,p[i+1].y);
glEnd();
}
glBegin(GL_LINES);
glVertex2d(p[i].x,p[i].y);
glVertex2d(p[0].x,p[0].y);
glEnd();
}

void myMouse(int button, int state, int x, int y)


{
if(button==GLUT_LEFT_BUTTON && state==GLUT_DOWN) //
On output, please left click on polygon then and only then clipping performs
{
glClear(GL_COLOR_BUFFER_BIT);

glBegin(GL_LINE_LOOP);
glVertex2f(p1.x,p1.y);
glVertex2f(p2.x,p1.y);
glVertex2f(p2.x,p2.y);
glVertex2f(p1.x,p2.y);
glEnd();
left();
right();
top();
bottom();
drawpolygon();
}
glFlush();}
void display(void){
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.4,1.0,0.0);
glBegin(GL_LINE_LOOP);
glVertex2f(p1.x, p1.y);
glVertex2f(p2.x,p1.y);
glVertex2f(p2.x,p2.y);
glVertex2f(p1.x,p2.y);
glEnd();
drawpolygon();
glFlush();}

void init(void){
Jeetesh Saini-211436

glClearColor(0.0,0.0,0.0,0.0); // clear screen usually black


gluOrtho2D(0,500,0,500);}

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


printf("Enter Window Coordinates:\n");
printf("Please Enter two Points:\n"); // P1(x,y) is the bottom
left point for clipping window
printf("Enter P1(x,y):\n");
scanf("%f", &p1.x); // if you don't know what value should be
given: enter 200
scanf("%f", &p1.y); // if you don't know what value should be
given: enter 200

printf("Enter P2(x,y):\n"); // P2(x,y) is


the top right point for clipping window
scanf("%f", &p2.x); // if you don't know what value should be
given: enter 400
scanf("%f", &p2.y); // if you don't know what value should be
given: enter 400

printf("\nEnter the no. of vertices:"); // if you don't know what value


should be given: enter 3
scanf("%d", &n);

for(i=0;i<n;i++){
printf("\nEnter V%d(x%d,y%d):\n" , i+1, i+1, i+1);
scanf("%f", &p[i].x); // if you don't know what value should be given:
enter V1(100,110), V2(340,210), V3(300,380)
scanf("%f", &p[i].y);}

p[i].x=p[0].x; // Assign last to first for connected


everything
p[i].y=p[0].y;

glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(640,480);
glutInitWindowPosition(0,0);
glutCreateWindow("Sutherland Hodgman Polygon Clipping Algorithm ");
init();

glutDisplayFunc(display);
glutMouseFunc(myMouse); // notice mouse movement and
call user defined function
glFlush();
glutMainLoop();
return 0;
}
Output:
Jeetesh Saini-211436

Experiment 14: 2D Translation


Implementation:

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

static float tx = 100.0;


static float ty = 50.0;
const int sides = 3;
const int radius = 100;
const int centerX = 10;
const int centerY = 10;

void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix();
glTranslatef(tx,ty,0);

glBegin(GL_TRIANGLES);
for (int i = 0; i < sides; i++) {
float angle = 2 * 3.14 * i / sides;
int x = centerX + radius * cos(angle);
int y = centerY + radius * sin(angle);
glVertex2i(x, y);
}
glEnd();

glBegin(GL_QUADS);
glColor3f(0.0, 1.0, 0.0);
glVertex2i(100, 100);
Jeetesh Saini-211436

glVertex2i(200, 100);
glVertex2i(200, 200);
glVertex2i(100, 200);
glEnd();

glPopMatrix();
glFlush();
}
void init(void)
{
glClearColor (1.0, 1.0, 1.0, 1.0);
glOrtho(-200.0, 200.0, -200.0, 200.0, -2.0, 2.0);
}
void spe_key(int key, int x, int y)
{
switch (key)
{
case GLUT_KEY_LEFT:
glColor3f(0,0,0);
tx -=5;
glutPostRedisplay();
break;

case GLUT_KEY_RIGHT:
glColor3f(0,0,0);
tx +=5;
glutPostRedisplay();
break;

case GLUT_KEY_UP:
glColor3f(0,0,0);
ty +=5;
glutPostRedisplay();
break;

case GLUT_KEY_DOWN:
glColor3f(0, 0, 0);
ty -=5;
glutPostRedisplay();
break;
default:
break;
}
}
int main()
{
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize (400, 400);
glutInitWindowPosition (100, 100);
glutCreateWindow ("2D Transformation");
init();
glutDisplayFunc(display);
glutSpecialFunc(spe_key);
glutMainLoop();
Jeetesh Saini-211436

return 0;
}

Output:

Experiment 15: 2D Scaling


Implementation:
A:- Scaling a Triangle

#include <iostream>
#include <windows.h>
#include <gl/glut.h>
#include <stdio.h>
#include <math.h>
#include <bits/stdc++.h>

using namespace std;

const int MAX_POINTS = 20;

void draw_polygon(int arr[][2], int n) {


int i;
glBegin(GL_POLYGON);
glColor3d(1.0, 1.0, 1.0);
for (i = 0; i < n; i++) {
glVertex2d(arr[i][0], arr[i][1]);
}
glEnd();
}

void translation(int poly_points[][2], int poly_size, int tx, int ty) {


int new_poly[poly_size][2];
int i;
for (i = 0; i < poly_size; i++) {
new_poly[i][0] = poly_points[i][0] + tx;
Jeetesh Saini-211436

new_poly[i][1] = poly_points[i][1] + ty;


}
draw_polygon(new_poly, poly_size);
}

void display_before(void) {
glClear(GL_COLOR_BUFFER_BIT);

int poly_size = 3;
int poly_points[20][2] = {{0, 0}, {40, 90}, {80, 0}};
draw_polygon(poly_points, poly_size);
glutSwapBuffers();
}

void display_after(void) {
glClear(GL_COLOR_BUFFER_BIT);

int poly_size = 3;
int poly_points[20][2] = {{0, 0}, {40, 90}, {80, 0}};
int tx = 20;
int ty = 100;
int scale_factor = 2;
for (int i = 0; i < poly_size; i++) {
poly_points[i][0] *= scale_factor;
poly_points[i][1] *= scale_factor;
}
translation(poly_points, poly_size, tx, ty);
glutSwapBuffers();
}

void init(void) {
glClearColor(0.0, 0.0, 0.0, 1.0);
glPointSize(2);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, 400.0, 0.0, 400.0, 0, 50);
}

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


glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);

glutInitWindowSize(400, 400);
glutInitWindowPosition(50, 50);
glutCreateWindow("BEFORE");
init();
glutDisplayFunc(display_before);

glutInitWindowSize(400, 400);
glutInitWindowPosition(450, 50);
glutCreateWindow("AFTER");
init();
glutDisplayFunc(display_after);
glutMainLoop();
Jeetesh Saini-211436

return 0;
}

B:- Translating and scaling a Square

#include<iostream>
#include <windows.h>
#include <gl/glut.h>
#include <stdio.h>
#include <math.h>
#include <bits/stdc++.h>\

using namespace std;

const int MAX_POINTS = 20;

void draw_polygon(int arr[][2],int n)


{
int i;
glBegin(GL_POLYGON);
glColor3d(1, 1, 1);
for(i=0; i<n; i++)
{
glVertex2d(arr[i][0],arr[i][1]);

}
glEnd();
}
void translation(int poly_points[][2], int poly_size,int tx,int ty, int sx,int sy)
{
int new_poly[poly_size][2];
int i;
for(i=0;i<poly_size;i++)
{
new_poly[i][0]=(poly_points[i][0] + tx) *sx;
new_poly[i][1]=(poly_points[i][1] + ty) *sy;
}
draw_polygon(new_poly,poly_size);
}
void display_before(void)
{
glClear(GL_COLOR_BUFFER_BIT);

int poly_size = 4;
int poly_points[20][2] = {{0,0},{0,90} ,{80,90},{80,0}};
draw_polygon(poly_points,poly_size);
glutSwapBuffers();

}
Jeetesh Saini-211436

void display_after(void)
{
glClear(GL_COLOR_BUFFER_BIT);

int poly_size = 4;
int poly_points[20][2] = {{0,0},{0,90} ,{80,90},{80,0}};
int tx=15;
int ty=10;
int sx=2;
int sy=1;
translation(poly_points,poly_size,tx,ty,sx,sy);

glutSwapBuffers();

void init(void)
{
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glPointSize(2);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, 200.0, 0.0, 200.0, 0, 50);
}

int main(int argc, char **argv)


{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);

glutInitWindowSize(400, 400);
glutInitWindowPosition(50, 50);
glutCreateWindow("BEFORE ");
init();
glutDisplayFunc(display_before);

glutInitWindowSize(400, 400);
glutInitWindowPosition(450, 50);
glutCreateWindow("AFTER");
init();
glutDisplayFunc(display_after);
glutMainLoop();
return 0;
}

Output:
Jeetesh Saini-211436

Experiment 16: 2D Rotation


Implementation:
#include <GL/glut.h>
#include <iostream>

float triangleVertices[] = { 0,0,1,0,1,1};


float angle =-45.0f;

void drawTriangle() {
glBegin(GL_TRIANGLES);
glVertex2f(triangleVertices[0], triangleVertices[1]);
glVertex2f(triangleVertices[2], triangleVertices[3]);
glVertex2f(triangleVertices[4], triangleVertices[5]);
glEnd();
}
void before_rotation() {
glClear(GL_COLOR_BUFFER_BIT);

glPushMatrix();
glColor3f(0.0f, 0.0f, 0.0f);
drawTriangle();
glPopMatrix();
glutSwapBuffers();
}
void after_rotation() {
glClear(GL_COLOR_BUFFER_BIT);
Jeetesh Saini-211436

glPushMatrix();
glRotatef(angle, 0.0f, 0.0f, 1.0f);
glColor3f(0.0f, 0.0f, 0.0f);
drawTriangle();
glPopMatrix();
glutSwapBuffers();
}

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


glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowPosition(100,50);
glutCreateWindow("Triangle Rotation");
gluOrtho2D(-2.0, 2.0, -2.0, 2.0);
glutDisplayFunc(before_rotation);
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);

glutInitWindowPosition(450,50);
glutCreateWindow("Triangle Rotation");
gluOrtho2D(-2.0, 2.0, -2.0, 2.0);
glutDisplayFunc(after_rotation);
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);

glutMainLoop();
return 0;
}

Output:
Jeetesh Saini-211436

Experiment 17: 2D Reflection


Implementation:
#include <GL/glut.h>

void drawSquare() {
glBegin(GL_QUADS);
glVertex2f(-0.5, -0.5);
glVertex2f(0.5, -0.5);
glVertex2f(0.5, 0.5);
glVertex2f(-0.5, 0.5);
glEnd();
}
Jeetesh Saini-211436

void display() {
glClear(GL_COLOR_BUFFER_BIT);

// Original square
glColor3f(0.0, 1.0, 0.0);
glPushMatrix();
drawSquare();
glPopMatrix();

// Reflection along the x-axis


glColor3f(0.0, 0.0, 1.0);
glPushMatrix();
glScalef(1.0, -1.0, 1.0);
glTranslatef(0.0, -1.0, 0.0); // Translate down to separate reflections
drawSquare();
glPopMatrix();

// Reflection along the y-axis


glColor3f(1.0, 0.0, 0.0); // Blue
glPushMatrix();
glScalef(-1.0, 1.0, 1.0);
glTranslatef(-1.0, 0.0, 0.0); // Translate left to separate reflections
drawSquare();
glPopMatrix();

// Reflection along the origin


glColor3f(1.0, 1.0, 0.0); // Yellow
glPushMatrix();
glScalef(-1.0, -1.0, 1.0);
glTranslatef(-1.0, -1.0, 0.0); // Translate left and down to separate reflections
drawSquare();
glPopMatrix();

glFlush();
}

void init() {
glClearColor(1.0, 1.0, 1.0, 1.0);
gluOrtho2D(-2.5, 2.5, -2.5, 2.5); // Adjusted viewing window
}

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


glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(400, 400);
glutCreateWindow("2D Reflections");
init();
Jeetesh Saini-211436

glutDisplayFunc(display);
glutMainLoop();
return 0;
}

Output:

Experiment 18: 2D Shearing


Implementation:
#include <GL/glut.h> // Assuming OpenGL/GLUT environment
void drawTriangle() {
glBegin(GL_TRIANGLES);
glColor3f(1.0, 1.0, 1.0);
glVertex3f(0, 0, 0);
glVertex3f(0.6, 0, 0);
glVertex3f(0, 0.6, 0);
glEnd();
glEnd();
}
void drawTriangle_after_Shearing() {
glBegin(GL_TRIANGLES);
glColor3f(1.0, 1.0, 1.0);
Jeetesh Saini-211436

// Original triangle vertices


GLfloat vertices[3][3] = {
{0, 0, 0},
{1, 0, 0},
{0, 1, 0}
};

// Shearing transformation matrix for x-direction shear


GLfloat shearMatrix[3][3] = {
{1, 0, 0},
{0, 1, 0},
{0, 0, 1}
};

// Apply shearing transformation to each vertex


for (int i = 0; i < 3; ++i) {
GLfloat result[3] = {0};
for (int j = 0; j < 3; ++j) {
for (int k = 0; k < 3; ++k) {
result[j] += shearMatrix[j][k] * vertices[i][k];
}
}
glVertex3fv(result);
}

glEnd();
}

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

void display_after_shearing()
{
glClear(GL_COLOR_BUFFER_BIT);
drawTriangle_after_Shearing();
glFlush();
}

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


glutInit(&argc, argv);

glutCreateWindow("Triangle");
glutDisplayFunc(display);

glutCreateWindow("Shearing Transformation");
glutDisplayFunc(display_after_shearing);

glutMainLoop();
return 0;
}
Jeetesh Saini-211436

Experiment 19: 3D Translation and


Scaling
Implementation:
#include <GL/glut.h>
void drawTriangle() {
glBegin(GL_TRIANGLES);
glColor3f(1.0, 0.0, 0.0);
glVertex3f(0, 0, 0);
glVertex3f(0.5, 0, 0);
glVertex3f(0, 0.5, 0);
glEnd();
glEnd();
}
Jeetesh Saini-211436

void drawTriangle_after_Scaling_and_Translation() {
glBegin(GL_TRIANGLES);
glColor3f(1.0, 0, 0);

GLfloat vertices[3][3] = {
{0, 0, 0},
{1, 0, 0},
{0, 1, 0}
};

// Scaling factors along x, y, and z axes


GLfloat scaleX = 1.0;
GLfloat scaleY = 1.0;
GLfloat scaleZ = 0.0;

// Scaling transformation matrix


GLfloat scaleMatrix[3][3] = {
{scaleX, 0, 0},
{0, scaleY, 0},
{0, 0, scaleZ}
};

// Translation values along x, y, and z axes


GLfloat translateX = -0.5;
GLfloat translateY = -0.5;
GLfloat translateZ = 0.0;

// Apply scaling transformation to each vertex


for (int i = 0; i < 3; ++i) {
GLfloat scaledResult[3] = {0};

// Apply scaling transformation


for (int j = 0; j < 3; ++j) {
for (int k = 0; k < 3; ++k) {
scaledResult[j] += scaleMatrix[j][k] * vertices[i][k];
}
}

// Apply translation to the scaled vertices


scaledResult[0] += translateX;
scaledResult[1] += translateY;
scaledResult[2] += translateZ;

glVertex3fv(scaledResult);
}

glEnd();
}

void display() {
Jeetesh Saini-211436

glClear(GL_COLOR_BUFFER_BIT);
drawTriangle();
glFlush();
}
void display_after_Scaling() {
glClear(GL_COLOR_BUFFER_BIT);
drawTriangle_after_Scaling_and_Translation();
glFlush();
}

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


glutInit(&argc, argv);

glutCreateWindow("Triangle");
glutDisplayFunc(display);

glutCreateWindow("Scaling and translation Transformation");


glutDisplayFunc(display_after_Scaling);

glutMainLoop();
return 0;
}

output:

Experiment 20: 3D Rotation

Implementation:

#ifdef __APPLE__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif

#include <stdlib.h>
Jeetesh Saini-211436

static int slices = 16;


static int stacks = 16;

static double angleX = 60.0; // Initial rotation angle around X-axis


static double angleZ = 0.0; // Initial rotation angle around Z-axis

static void resize(int width, int height)


{
const float ar = (float)width / (float)height;

glViewport(0, 0, width, height);


glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-ar, ar, -1.0, 1.0, 2.0, 100.0);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

static void display(void)


{
const double t = glutGet(GLUT_ELAPSED_TIME) / 1000.0;
const double a = t * 90.0;

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3d(1, 0, 0);

glPushMatrix();
glTranslated(0, 1.2, -6);
glRotated(angleX, 1, 0, 0); // Apply X-axis rotation
glRotated(angleZ, 0, 0, 1); // Apply Z-axis rotation
glutSolidCone(1, 1, slices, stacks);
glPopMatrix();

glPushMatrix();
glTranslated(0, -1.2, -6);
glRotated(angleX, 1, 0, 0); // Apply X-axis rotation
glRotated(angleZ, 0, 0, 1); // Apply Z-axis rotation
glutWireCone(1, 1, slices, stacks);
glPopMatrix();

glutSwapBuffers();}
static void key(unsigned char key, int x, int y){
switch (key){
case 27:
case 'q':
exit(0);
break;
case '+':
slices++;
stacks++;
break;
case '-':
Jeetesh Saini-211436

if (slices > 3 && stacks > 3) {


slices--;
stacks--; }
break;
case 'a': // Rotate clockwise around Z-axis
angleZ += 5.0;
break;
case 'd': // Rotate counterclockwise around Z-axis
angleZ -= 5.0;
break;
case 'w': // Rotate clockwise around X-axis
angleX += 5.0;
break;
case 's': // Rotate counterclockwise around X-axis
angleX -= 5.0;
break;
}

glutPostRedisplay();
}

static void idle(void)


{
glutPostRedisplay();
}

/* Program entry point */

int main(int argc, char* argv[])


{
glutInit(&argc, argv);
glutInitWindowSize(640, 480);
glutInitWindowPosition(10, 10);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);

glutCreateWindow("GLUT Shapes");

glutReshapeFunc(resize);
glutDisplayFunc(display);
glutKeyboardFunc(key);
glutIdleFunc(idle);

glClearColor(1, 1, 1, 1);
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);

glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);

glEnable(GL_LIGHT0);
glEnable(GL_NORMALIZE);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_LIGHTING);
const GLfloat light_ambient[] = {0.0f, 0.0f, 0.0f, 1.0f};
Jeetesh Saini-211436

const GLfloat light_diffuse[] = {1.0f, 1.0f, 1.0f, 1.0f};


const GLfloat light_specular[] = {1.0f, 1.0f, 1.0f, 1.0f};
const GLfloat light_position[] = {2.0f, 5.0f, 5.0f, 0.0f};
const GLfloat mat_ambient[] = {0.7f, 0.7f, 0.7f, 1.0f};
const GLfloat mat_diffuse[] = {0.8f, 0.8f, 0.8f, 1.0f};
const GLfloat mat_specular[] = {1.0f, 1.0f, 1.0f, 1.0f};
const GLfloat high_shininess[] = {100.0f};
glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);
glutMainLoop();
return EXIT_SUCCESS;}
Output:

You might also like