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

CG Lab4 Assignment 2021IMT015 APOORV JAIN

The document discusses drawing various shapes like circles and lines using OpenGL and Bresenham's algorithms. Code examples are provided to draw circles using points and line loops as well as drawing a line and letter A using Bresenham's line algorithm. Further code draws concentric circles and a star shape using Bresenham's circle algorithm.

Uploaded by

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

CG Lab4 Assignment 2021IMT015 APOORV JAIN

The document discusses drawing various shapes like circles and lines using OpenGL and Bresenham's algorithms. Code examples are provided to draw circles using points and line loops as well as drawing a line and letter A using Bresenham's line algorithm. Further code draws concentric circles and a star shape using Bresenham's circle algorithm.

Uploaded by

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

Computer Graphics Lab-4 Assignment

Name: APOORV JAIN

Roll no.: 2021IMT015

Course: IPG-MTECH

Semester: V
Q1. Create a display window of resolution of 512x512 on your

computer screen. On that display window, draw the following circle:

(a) r=1, center=(0,0)

(b) r=50, center=(20,20)

• Using points
• using line loop

Using points
Code(a):
#include<windows.h>

#include<iostream>

using namespace std;

#include<GL/freeglut.h>

#include<math.h>

void drawCircle()

float x, y, r;

x = 0;

y = 0,

r = 1;

glClear(GL_COLOR_BUFFER_BIT);

glBegin(GL_POINTS);

float d = 0.0;

for(int i=0; i<1000; i++)

{
glVertex2f(r*cos(d)+x, r*sin(d)+y);

d+=3.14159/500;

glEnd();

glFlush();

int main(int argc, char** argv )

glutInit(&argc, argv);

glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);

glutInitWindowSize(512, 512);

glutCreateWindow("OpenGL");

glClearColor(0.1, 0.5, 0.6, 0.2);

glMatrixMode(GL_PROJECTION);

glLoadIdentity();

// Set up the viewing area

glOrtho(-10.0, 10.0, -10.0,10.0, -10.0, 10.0);

glutDisplayFunc(drawCircle);

glutMainLoop();

return 0;

Output:
Code(b):
#include<windows.h>

#include<iostream>

using namespace std;

#include<GL/freeglut.h>

#include<math.h>

void drawCircle()

float x, y, r;
x = 20;

y = 20,

r = 50;

glClear(GL_COLOR_BUFFER_BIT);

glBegin(GL_POINTS);

float d = 0.0;

for(int i=0; i<1000; i++)

glVertex2f(r*cos(d)+x, r*sin(d)+y);

d+=3.14159/500;

glEnd();

glFlush();

int main(int argc, char** argv )

glutInit(&argc, argv);

glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);

glutInitWindowSize(512, 512);

glutCreateWindow("OpenGL");

glClearColor(0.1, 0.5, 0.6, 0.2);

glMatrixMode(GL_PROJECTION);

glLoadIdentity();

// Set up the viewing area


glOrtho(-100.0, 100.0, -100.0,100.0, -100.0, 100.0);

glutDisplayFunc(drawCircle);

glutMainLoop();

return 0;

Output:
Using line loop
Code(a):
#include<windows.h>

#include<iostream>

using namespace std;

#include<GL/freeglut.h>

#include<math.h>

void drawCircle()

float x, y, r;

x = 0;

y = 0,

r = 1;

glClear(GL_COLOR_BUFFER_BIT);

glBegin(GL_LINE_LOOP);

float d = 0.0;

for(int i=0; i<1000; i++)

glVertex2f(r*cos(d)+x, r*sin(d)+y);

d+=3.14159/500;

glEnd();

glFlush();

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

glutInit(&argc, argv);

glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);

glutInitWindowSize(512, 512);

glutCreateWindow("OpenGL");

glClearColor(0.1, 0.5, 0.6, 0.2);

glMatrixMode(GL_PROJECTION);

glLoadIdentity();

// Set up the viewing area

glOrtho(-10.0, 10.0, -10.0,10.0, -10.0, 10.0);

glutDisplayFunc(drawCircle);

glutMainLoop();

return 0;

Output:
Code(b):
#include<windows.h>

#include<iostream>

using namespace std;

#include<GL/freeglut.h>

#include<math.h>

void drawCircle()

float x, y, r;

x = 20;
y = 20,

r = 50;

glClear(GL_COLOR_BUFFER_BIT);

glBegin(GL_LINE_LOOP);

float d = 0.0;

for(int i=0; i<1000; i++)

glVertex2f(r*cos(d)+x, r*sin(d)+y);

d+=3.14159/500;

glEnd();

glFlush();

int main(int argc, char** argv )

glutInit(&argc, argv);

glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);

glutInitWindowSize(512, 512);

glutCreateWindow("OpenGL");

glClearColor(0.1, 0.5, 0.6, 0.2);

glMatrixMode(GL_PROJECTION);

glLoadIdentity();

// Set up the viewing area

glOrtho(-100.0, 100.0, -100.0,100.0, -100.0, 100.0);


glutDisplayFunc(drawCircle);

glutMainLoop();

return 0;

Output:
Q2. Draw a line using Bresenham's line algorithm , following line segments

and note dawn the time taken by each of the algorithms.

Code:
#include<windows.h>

#include<iostream>

using namespace std;

#include<GL/freeglut.h>

#include<math.h>

// Function to draw a line using Bresenham's algorithm

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

int dx = abs(x2 - x1);

int dy = abs(y2 - y1);

int x = x1, y = y1;

int sx = (x1 < x2) ? 1 : -1;

int sy = (y1 < y2) ? 1 : -1;

int err = dx - dy;

while (true) {

glVertex2i(x, y);

if (x == x2 && y == y2)

break;
int e2 = 2 * err;

if (e2 > -dy) {

err -= dy;

x += sx;

if (e2 < dx) {

err += dx;

y += sy;

// Function to display the scene

void display() {

glClear(GL_COLOR_BUFFER_BIT);

glPointSize(2.0);

glBegin(GL_POINTS);

glColor3f(1.0, 1.0, 0.0);

int x1 = 10, y1 = 10;

int x2 = 40, y2 = 30;

bresenhamLine(x1, y1, x2, y2);

glEnd();

glFlush();

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

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

glutInitWindowSize(640, 480);

glutCreateWindow("Bresenham Line Drawing");

glClearColor(0.1, 0.5, 0.6, 0.2);

glMatrixMode(GL_PROJECTION);

glLoadIdentity();

glOrtho(0.0, 60.0, 0.0,60.0, -10.0, 10.0);

glutDisplayFunc(display);

glutMainLoop();

return 0;

Output:
Code:
#include<windows.h>

#include<iostream>

using namespace std;

#include<GL/freeglut.h>

#include<math.h>

void makeA(float x1, float y1, float x2, float y2, float x3, float y3,float x4,float y4, float x5,float y5);

void display();

void bresenhamLine(int x1, int y1, int x2, int y2);

// Function to draw a line using Bresenham's algorithm

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

int dx = abs(x2 - x1);

int dy = abs(y2 - y1);

int x = x1, y = y1;

int sx = (x1 < x2) ? 1 : -1;

int sy = (y1 < y2) ? 1 : -1;

int err = dx - dy;

while (true) {
glVertex2i(x, y);

if (x == x2 && y == y2)

break;

int e2 = 2 * err;

if (e2 > -dy) {

err -= dy;

x += sx;

if (e2 < dx) {

err += dx;

y += sy;

// Function to display the scene

void display() {

glClear(GL_COLOR_BUFFER_BIT);

glPointSize(1.5);

glBegin(GL_POINTS);

glColor3f(1.0, 1.0, 0.0);

makeA(50, 100, 25, 0, 75, 0, 0, -100, 100, -100);

glEnd();

glFlush();

void makeA(float x1, float y1, float x2, float y2, float x3, float y3,float x4,float y4, float x5,float y5)
{

glColor3f(0, 0, 1.0);

bresenhamLine(x1, y1, x2, y2);

glColor3f(0, 1.0, 0.4);

bresenhamLine(x1, y1, x3, y3);

glColor3f(1.0f, 0.5f, 0.0f);

bresenhamLine(x3, y3, x2, y2);

glColor3f(0, 0, 1.0);

bresenhamLine(x2, y2, x4, y4);

glColor3f(0, 1.0, 0.4);

bresenhamLine(x3, y3, x5, y5);

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

glutInit(&argc, argv);

glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);

glutInitWindowSize(640, 480);

glutCreateWindow("Bresenham");

glClearColor(0.1, 0.5, 0.6, 0.2);

glMatrixMode(GL_PROJECTION);

glLoadIdentity();

glOrtho(-10.0, 150.0, -150.0, 150.0, -10.0, 10.0);

glutDisplayFunc(display);

glutMainLoop();

return 0;

Output:
Code:
#include<windows.h>

#include<iostream>

using namespace std;


#include<GL/freeglut.h>

#include<math.h>

void makeA(float x1, float y1, float x2, float y2, float x3, float y3,float x4,float y4, float x5,float y5);

void display();

void makeStar();

// Function to draw a line using Bresenham's algorithm

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

int dx = abs(x2 - x1);

int dy = abs(y2 - y1);

int x = x1, y = y1;

int sx = (x1 < x2) ? 1 : -1;

int sy = (y1 < y2) ? 1 : -1;

int err = dx - dy;

while (true) {

glVertex2i(x, y);

if (x == x2 && y == y2)

break;

int e2 = 2 * err;

if (e2 > -dy) {

err -= dy;

x += sx;

if (e2 < dx) {

err += dx;
y += sy;

// Function to display the scene

void display() {

glClear(GL_COLOR_BUFFER_BIT);

glPointSize(1.3);

glBegin(GL_POINTS);

glColor3f(1.0, 1.0, 0.0);

makeStar();

//makeA(50, 100, 25, 0, 75, 0, 0, -100, 100, -100);

glEnd();

glFlush();

void makeStar()

bresenhamLine(-60, -60, 60,-60);

bresenhamLine(60, -60, 0, 90);

bresenhamLine(0, 90, -60, -60);

bresenhamLine(-60, 60, 60,60);

bresenhamLine(60, 60, 00, -90);

bresenhamLine(0, -90, -60, 60);

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

glutInit(&argc, argv);

glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);

glutInitWindowSize(640, 480);

glutCreateWindow("Bresenham");

glClearColor(0.1, 0.5, 0.6, 0.2);

glMatrixMode(GL_PROJECTION);

glLoadIdentity();

glOrtho(-100.0, 150.0, -150.0, 150.0, -10.0, 10.0);

glutDisplayFunc(display);

glutMainLoop();

return 0;

Output:
Q3. Draw a circle using Bresenham's circle algorithm and two

concentric circles.

For circle
Code:
#include <GL/freeglut.h>

#include <iostream>

#include <cmath>

#include<windows.h>

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

int x = 0;

int y = radius;

int pk = 1 - radius;

// Plot the initial points to cover all eight octants

glVertex2i(centerX, centerY + radius);

glVertex2i(centerX, centerY - radius);

glVertex2i(centerX + radius, centerY);

glVertex2i(centerX - radius, centerY);

while (x < y) {

x++;

if (pk < 0) {

pk += 2 * x + 1;

} else {

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

// Plot points for the eight octants

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);

void display() {

glClear(GL_COLOR_BUFFER_BIT);

glPointSize(2.0);

glBegin(GL_POINTS);

glColor3f(1.0, 1.0, 0.0);

int centerX = 20;

int centerY = 20;

int radius = 50;

drawCircle(centerX, centerY, radius);

glEnd();

glFlush();

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

glutInit(&argc, argv);

glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);

glutInitWindowSize(400, 400);

glutCreateWindow("Bresenham Circle");

glClearColor(0.1, 0.5, 0.6, 0.2);

glMatrixMode(GL_PROJECTION);

glLoadIdentity();

// Set up the viewing area

glOrtho(-50.0, 100.0, -50.0, 100.0, -100.0, 10.0);

glutDisplayFunc(display);

glutMainLoop();

return 0;

Output:
For Concentric circles

Code:
#include <GL/freeglut.h>

#include <iostream>

#include <cmath>

#include<windows.h>

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

int x = 0;

int y = radius;

int pk = 1 - radius;

// Plot the initial points to cover all eight octants

glVertex2i(centerX, centerY + radius);

glVertex2i(centerX, centerY - radius);

glVertex2i(centerX + radius, centerY);

glVertex2i(centerX - radius, centerY);

while (x < y) {

x++;

if (pk < 0) {

pk += 2 * x + 1;

} else {

y--;

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

}
// Plot points for the eight octants

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);

void display() {

glClear(GL_COLOR_BUFFER_BIT);

glPointSize(2.0);

glBegin(GL_POINTS);

glColor3f(1.0, 1.0, 0.0);

drawCircle(20, 20, 50);

drawCircle(20, 20, 35);

glEnd();

glFlush();

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

glutInit(&argc, argv);

glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);

glutInitWindowSize(400, 400);
glutCreateWindow("Bresenham Circle");

glClearColor(0.1, 0.5, 0.6, 0.2);

glMatrixMode(GL_PROJECTION);

glLoadIdentity();

// Set up the viewing area

glOrtho(-50.0, 100.0, -50.0, 100.0, -100.0, 10.0);

glutDisplayFunc(display);

glutMainLoop();

return 0;

Output:

You might also like