CG Lab4 Assignment 2021IMT015 APOORV JAIN
CG Lab4 Assignment 2021IMT015 APOORV JAIN
Course: IPG-MTECH
Semester: V
Q1. Create a display window of resolution of 512x512 on your
• Using points
• using line loop
Using points
Code(a):
#include<windows.h>
#include<iostream>
#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;
{
glVertex2f(r*cos(d)+x, r*sin(d)+y);
d+=3.14159/500;
glEnd();
glFlush();
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(512, 512);
glutCreateWindow("OpenGL");
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glutDisplayFunc(drawCircle);
glutMainLoop();
return 0;
Output:
Code(b):
#include<windows.h>
#include<iostream>
#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;
glVertex2f(r*cos(d)+x, r*sin(d)+y);
d+=3.14159/500;
glEnd();
glFlush();
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(512, 512);
glutCreateWindow("OpenGL");
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glutDisplayFunc(drawCircle);
glutMainLoop();
return 0;
Output:
Using line loop
Code(a):
#include<windows.h>
#include<iostream>
#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;
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");
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glutDisplayFunc(drawCircle);
glutMainLoop();
return 0;
Output:
Code(b):
#include<windows.h>
#include<iostream>
#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;
glVertex2f(r*cos(d)+x, r*sin(d)+y);
d+=3.14159/500;
glEnd();
glFlush();
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(512, 512);
glutCreateWindow("OpenGL");
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glutMainLoop();
return 0;
Output:
Q2. Draw a line using Bresenham's line algorithm , following line segments
Code:
#include<windows.h>
#include<iostream>
#include<GL/freeglut.h>
#include<math.h>
while (true) {
glVertex2i(x, y);
if (x == x2 && y == y2)
break;
int e2 = 2 * err;
err -= dy;
x += sx;
err += dx;
y += sy;
void display() {
glClear(GL_COLOR_BUFFER_BIT);
glPointSize(2.0);
glBegin(GL_POINTS);
glEnd();
glFlush();
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(640, 480);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glutDisplayFunc(display);
glutMainLoop();
return 0;
Output:
Code:
#include<windows.h>
#include<iostream>
#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();
while (true) {
glVertex2i(x, y);
if (x == x2 && y == y2)
break;
int e2 = 2 * err;
err -= dy;
x += sx;
err += dx;
y += sy;
void display() {
glClear(GL_COLOR_BUFFER_BIT);
glPointSize(1.5);
glBegin(GL_POINTS);
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);
glColor3f(0, 0, 1.0);
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(640, 480);
glutCreateWindow("Bresenham");
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glutDisplayFunc(display);
glutMainLoop();
return 0;
Output:
Code:
#include<windows.h>
#include<iostream>
#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();
while (true) {
glVertex2i(x, y);
if (x == x2 && y == y2)
break;
int e2 = 2 * err;
err -= dy;
x += sx;
err += dx;
y += sy;
void display() {
glClear(GL_COLOR_BUFFER_BIT);
glPointSize(1.3);
glBegin(GL_POINTS);
makeStar();
glEnd();
glFlush();
void makeStar()
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(640, 480);
glutCreateWindow("Bresenham");
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
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>
int x = 0;
int y = radius;
int pk = 1 - radius;
while (x < y) {
x++;
if (pk < 0) {
pk += 2 * x + 1;
} else {
y--;
pk += 2 * (x - y) + 1;
void display() {
glClear(GL_COLOR_BUFFER_BIT);
glPointSize(2.0);
glBegin(GL_POINTS);
glEnd();
glFlush();
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(400, 400);
glutCreateWindow("Bresenham Circle");
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glutDisplayFunc(display);
glutMainLoop();
return 0;
Output:
For Concentric circles
Code:
#include <GL/freeglut.h>
#include <iostream>
#include <cmath>
#include<windows.h>
int x = 0;
int y = radius;
int pk = 1 - radius;
while (x < y) {
x++;
if (pk < 0) {
pk += 2 * x + 1;
} else {
y--;
pk += 2 * (x - y) + 1;
}
// Plot points for the eight octants
void display() {
glClear(GL_COLOR_BUFFER_BIT);
glPointSize(2.0);
glBegin(GL_POINTS);
glEnd();
glFlush();
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(400, 400);
glutCreateWindow("Bresenham Circle");
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glutDisplayFunc(display);
glutMainLoop();
return 0;
Output: