Uday - CG Lab PDF
Uday - CG Lab PDF
_____________________________________________________________
UNIVERSITY OF PETROLEUM & ENERGY STUDIES
School of Computer Science
Dehradun
2019-20
______________________________________________________________
0
INDEX
4 Filling the objects using flood fill, boundary fill and scan 13-19
line fill algorithm
EXPERIMENT 1
1
Name: Kunwar Uday Singh
Branch: CSE OGI (B2)
Roll no: R970216036
EXPERIMENT 2
Aim: Implement the DDA and Bresenham’s Line Drawing Algorithm in OpenGL.
Steps: To draw a line, you need two points between which you can draw a line.
DDA Line Drawing Algorithm
Digital Differential Analyzer (DDA) algorithm is the simple line generation algorithm.
Step 1 – Load the values of two end points (X0, Y0) and (X1, Y1).
Step 2 − Calculate the difference between two end points.
dx = X1 - X0; dy = Y1 - Y0
Step 3 − Based on the calculated difference in step-2, identify the number of steps to put
pixel. The largest among dx and dy is assigned to ‘steps’.
Step 4 − Calculate the increment in x coordinate and y coordinate.
Xincrement = dx / (float) steps; Yincrement = dy / (float) steps;
Step 5 – Add the Xincrement and Yincrement to the initial point.
Step 6 – Add the Xincrement and Yincrement to the result from step-5 and keep adding to the results
till the final point is reached.
Code:
#include<GL/freeglut.h>
#include<stdlib.h>
#include<stdio.h>
float x1,x2,y3,y2;
void display(void){
float dy,dx,step,x,y,k,Xin,Yin;
dx=x2-x1;
dy=y2-y3;
if(abs(dx)> abs(dy)){
step = abs(dx);
}
else
step = abs(dy);
Xin = dx/step;
Yin = dy/step;
x= x1;
y=y3;
glBegin(GL_POINTS);
glVertex2i(x,y);
2
Name: Kunwar Uday Singh
Branch: CSE OGI (B2)
Roll no: R970216036
glEnd();
for (k=1 ;k<=step;k++){
x= x + Xin;
y= y + Yin;
glBegin(GL_POINTS);
glVertex2i(x,y);
glEnd();
}
glFlush();
}
void init(void){
glClearColor(0.7,0.7,0.7,0.7);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-100,100,-100,100);
}
int main(int argc, char** argv) {
printf("Enter the value of x1 : ");
scanf("%f",&x1);
printf("Enter the value of y1 : ");
scanf("%f",&y3);
printf("Enter the value of x2 : ");
scanf("%f",&x2);
printf("Enter the value of y2 : ");
scanf("%f",&y2);
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize (500, 500);
glutInitWindowPosition (100,100);
glutCreateWindow ("DDA");
init();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
Output:
3
Name: Kunwar Uday Singh
Branch: CSE OGI (B2)
Roll no: R970216036
Code:
#include<Windows.h>
#include<gl/GL.h>
#include<gl/GLU.h>
#include<gl/glut.h>
#include <GL/freeglut.h>
#include <stdio.h>
4
Name: Kunwar Uday Singh
Branch: CSE OGI (B2)
Roll no: R970216036
5
Name: Kunwar Uday Singh
Branch: CSE OGI (B2)
Roll no: R970216036
draw_pixel(x, y);
}
}
}
void myDisplay() {
draw_line(x1, x2, y1, y2);
glFlush();
}
void main(int argc, char **argv) {
printf("Enter two end points of the line to be drawn:\n");
printf("\nEnter Point1 ( X1 , Y1):\n");
scanf("%d%d", &x1, &y1);
printf("\nEnter Point2 ( X2 , Y2):\n");
scanf("%d%d", &x2, &y2);
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(640, 480);
glutInitWindowPosition(0, 0);
glutCreateWindow("Bresenham's Line Drawing");
myInit();
glutDisplayFunc(myDisplay);
glutMainLoop();
}
Output:
6
Name: Kunwar Uday Singh
Branch: CSE OGI (B2)
Roll no: R970216036
EXPERIMENT 3
Theory:
Mid-Point Circle Drawing Algorithm
This algorithm is used to calculate all the perimeter points of the circle in the first octant and
then print them along with their mirror points in the other octants. This will work only
because a circle is symmetric about its center.
Step 1 − Input radius r and circle center (Xc, Yc) and obtain the first point on the
circumference of the circle centered on the origin as
(X0, Y0) = (0, r)
Step 2 − Calculate the initial value of decision parameter as P0 = 5/4 – r
Step 3 − At each Xk position starting at k=0, perform the following test −
If PK < 0, then next point on circle (0, 0) is (XK+1, YK) and PK+1 = PK + 2XK+1 + 1
Else, PK+1 = PK + 2XK+1 + 1 – 2YK+1
Where, 2XK+1 = 2XK+2 and 2YK+1 = 2YK-2.
Step 4 − Determine the symmetry points in other seven octants.
Step 5 − Move each calculate pixel position (X, Y) onto the circular path centered on (Xc,
Yc) and plot the coordinate values.
X = X + XC, Y = Y + YC
Step 6 − Repeat step-3 through 5 until X >= Y.
Code:
#include<Windows.h>
#include<gl/GL.h>
#include<gl/GLU.h>
#include<gl/glut.h>
#include <GL/freeglut.h>
#include <math.h>
#include <stdio.h>
void myInit(void)
{
7
Name: Kunwar Uday Singh
Branch: CSE OGI (B2)
Roll no: R970216036
void midPointCircleAlgo()
{
int x = 0;
int y = r;
float decision = 5 / 4 - r;
plot(x, y);
while (y > x)
{
if (decision < 0)
{
x++;
decision += 2 * x + 1;
}
else
{
y--;
x++;
decision += 2 * (x - y) + 1;
}
plot(x, y);
plot(x, -y);
plot(-x, y);
plot(-x, -y);
plot(y, x);
plot(-y, x);
plot(y, -x);
plot(-y, -x);
}
void myDisplay(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.5, 0.0, 1.0);
glPointSize(1.0);
midPointCircleAlgo();
glFlush();
}
8
Name: Kunwar Uday Singh
Branch: CSE OGI (B2)
Roll no: R970216036
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(640, 480);
glutInitWindowPosition(0, 0);
glutCreateWindow("Line Drawing Alogrithms");
glutDisplayFunc(myDisplay);
myInit();
glutMainLoop();
Output:
9
Name: Kunwar Uday Singh
Branch: CSE OGI (B2)
Roll no: R970216036
Step 1 − Take the input and ellipse center and obtain the first point on an ellipse centered on
the origin as a (x,y0)= (0,ry).
Step 2 −Now calculate the initial decision parameter in region 1 as: p10=ry2+1/4rx2-rx2ry
Step 3 − At each xk position in region 1 perform the following task. If p1k<0 then the next
point along the ellipse centered on (0,0) is (xk+1,yk).
i.e. p1k+1=p1k+2ry2xk+1+ry2
Otherwise the next point along the circle is (xk+1,yk -1)
i.e. p1k+1=p1k+2ry2xk+1 – 2rx2yk+1+ry2
Step 4 − Now, again calculate the initial value in region 2 using the last point (x0,y0)
calculated in a region 1 as : p20=ry2(x0+1/2)2+rx2(y0-1)2-rx2ry2
Code:
#include<Windows.h>
#include<gl/GL.h>
#include<gl/GLU.h>
#include<gl/glut.h>
#include <GL/freeglut.h>
#include <math.h>
#include <stdio.h>
float xc;
float yc;
float rx;
float ry;
void Init(void){
glClearColor(1.0, 1.0, 1.0, 0.0); //Background Color
glColor3f(0.0f, 0.0f, 0.0f); //Black Color
glPointSize(4.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, 640.0, 0.0, 480.0); //Coordinate System
}
10
Name: Kunwar Uday Singh
Branch: CSE OGI (B2)
Roll no: R970216036
11
Name: Kunwar Uday Singh
Branch: CSE OGI (B2)
Roll no: R970216036
void myDisplay(void){
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.5, 0.0, 1.0);
glPointSize(1.0);
ellipse();
glFlush();
}
int main(int argc, char** argv){
printf("Enter the coordinates of the centre of the ellipse: \n");
scanf("%f%f", &xc, &yc);
printf("Enter the radius of the x-axis and y-axis: \n");
scanf("%f%f", &rx, &ry);
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(640, 480);
glutInitWindowPosition(0, 0);
glutCreateWindow("Ellipse");
Init();
glutDisplayFunc(myDisplay);
glutMainLoop();
return 0;
}
Output:
12
Name: Kunwar Uday Singh
Branch: CSE OGI (B2)
Roll no: R970216036
EXPERIMENT 4
Aim: Filling the objects using flood fill, boundary fill and scan line fill algorithm.
Code:
#include<Windows.h>
#include<gl/GL.h>
#include<gl/GLU.h>
#include<gl/glut.h>
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
13
Name: Kunwar Uday Singh
Branch: CSE OGI (B2)
Roll no: R970216036
glBegin(GL_POLYGON);
glVertex2i(x1, y1);
glVertex2i(x1, y2);
glVertex2i(x2, y2);
glVertex2i(x2, y1);
glEnd();
glFlush();
}
void display()
{
glClearColor(1.0, 1.0, 1.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
drawPolygon(150, 250, 200, 300);
glFlush();
}
void floodfill4(int x, int y, float oldcolor[3], float newcolor[3])
{
float color[3];
getPixel(x, y, color);
if (color[0] == oldcolor[0] && (color[1]) == oldcolor[1] && (color[2]) == oldcolor[2])
{
setPixel(x, y, newcolor);
floodfill4(x + 1, y, oldcolor, newcolor);
floodfill4(x - 1, y, oldcolor, newcolor);
floodfill4(x, y + 1, oldcolor, newcolor);
floodfill4(x, y - 1, oldcolor, newcolor);
}
}
void mouse(int btn, int state, int x, int y)
{
if (btn == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
{
int xi = x;
int yi = (wh - y);
floodfill4(xi, yi, intCol, fillCol);
}
}
void myinit()
{
glViewport(0, 0, ww, wh);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, (GLdouble)ww, 0.0, (GLdouble)wh);
glMatrixMode(GL_MODELVIEW);
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(ww, wh);
glutCreateWindow("Flood-Fill-Recursive");
glutDisplayFunc(display);
myinit();
glutMouseFunc(mouse);
glutMainLoop();
return 0;
14
Name: Kunwar Uday Singh
Branch: CSE OGI (B2)
Roll no: R970216036
Output:
This algorithm picks a point inside an object and starts to fill until it hits the boundary of the
object. The color of the boundary and the color that we fill should be different for this
algorithm to work. In this algorithm, we assume that color of the boundary is same for the
entire object.
Step 1 − It takes an interior point(x, y), a fill color, and a boundary color as the input.
Step 2 − The algorithm starts by checking the color of (x, y). If it’s color is not equal to the fill
color and the boundary color, then it is painted with the fill color and the function is called for
all the neighbours of (x, y).
Step 3 −If a point is found to be of fill color or of boundary color, the function does not call
its neighbours and returns.
Step 4 −This process continues until all points up to the boundary color for the region have
been tested.
Code:
#include<Windows.h>
#include<gl/GL.h>
#include<gl/GLU.h>
#include<gl/glut.h>
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include <time.h>
15
Name: Kunwar Uday Singh
Branch: CSE OGI (B2)
Roll no: R970216036
while (goal>clock());
}
void init(){
glClearColor(1.0, 1.0, 1.0, 0.0);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0, 640, 0, 480);
}
void world(){
glLineWidth(3);
glPointSize(2);
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1, 0, 0);
glBegin(GL_LINE_LOOP);
glVertex2i(150, 100);
glVertex2i(300, 300);
glVertex2i(450, 100);
glEnd();
glFlush();
}
16
Name: Kunwar Uday Singh
Branch: CSE OGI (B2)
Roll no: R970216036
glutInitWindowSize(640, 480);
glutInitWindowPosition(200, 200);
glutCreateWindow("Boundary Fill");
glutDisplayFunc(world);
glutMouseFunc(mouse);
init();
glutMainLoop();
return 0;
}
Output:
Code:
#include<Windows.h>
#include<gl/GL.h>
#include<gl/GLU.h>
#include<gl/glut.h>
#include <time.h>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
int h, k, r;
void scanline(int, int);
void circle(void);
void Axes(void);
void display(void)
17
Name: Kunwar Uday Singh
Branch: CSE OGI (B2)
Roll no: R970216036
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.5, 0.0, 1.0);
glBegin(GL_POINTS);
circle();
glEnd();
glFlush();
}
void scanline(int ax, int by)
{
int varx;
for (varx = h + ax; varx >= (h - ax); varx--)
glVertex2s(varx, by);
}
void circle(void)
{
double XEnd, J;
int i, j;
XEnd = (r / 1.414);
for (i = 0; i <= XEnd; i++)
{
J = sqrt(r*r - i*i);
j = (int)(J);
scanline(i, j);
scanline(j, i);
scanline(j, -i);
scanline(i, -j);
}
Axes();
glVertex3s(h, k, -25);
}
void Axes(void)
{
int i;
glColor3f(0.0, 0.0, 0.0);
for (i = -100; i <= 100; i++)
{
glVertex2s(i, 0);
glVertex2s(0, i);
}
for (i = -2; i <= 2; i++)
{
glVertex2s(95 + i, 4 + i);
glVertex2s(95 - i, 4 + i);
}
for (i = 0; i <= 2; i++)
{
glVertex2s(4 + i, 95 + i);
glVertex2s(4 - i, 95 + i);
glVertex2s(4, 95 - i);
}
}
void init(void)
{
glClearColor(1.0, 1.0, 1.0, 1.0);
glOrtho(-100.0, 100.0, -100.0, 100.0, -1.0, 1.0);
18
Name: Kunwar Uday Singh
Branch: CSE OGI (B2)
Roll no: R970216036
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(640, 480);
glutInitWindowPosition(0, 0);
printf("Enter the center of circle:\n");
scanf("%d %d", &h, &k);
printf("Enter the radius:\n");
scanf("%d", &r);
glutCreateWindow("Circle : Scanline Filling Method ");
init();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
Output:
19
Name: Kunwar Uday Singh
Branch: CSE OGI (B2)
Roll no: R970216036
Experiment 5 & 6
Aim: Performing Clipping operation on line and polygon using Cohen Sutherland and
Sutherland Hodgeman algorithms respectively.
Cohen Sutherland
Code:
#include<Windows.h>
#include<gl/GL.h>
#include<gl/GLU.h>
#include<gl/glut.h>
#include <time.h>
#include <stdio.h>
#include <math.h>
#define outcode int
double xmin = 50, ymin = 50, xmax = 100, ymax = 100;// Windows boundaries
double xvmin = 200, yvmin = 200, xvmax = 300, yvmax = 300; // Viewport
const int RIGHT = 8; // bit codes for the right
const int LEFT = 2; //bit codes for the left
const int TOP = 4; // bit codes for the top
const int BOTTOM = 1; //bit codes for the bottom
outcode ComputeOutCode(double x, double y);
20
Name: Kunwar Uday Singh
Branch: CSE OGI (B2)
Roll no: R970216036
else{
y = y0 + (y1 - y0)*(xmin - x0) / (x1 - x0);
x = xmin;
}
if (outcodeOut == outcode0){
x0 = x;
y0 = y;
outcode0 = ComputeOutCode(x0, y0);
}
else{
x1 = x;
y1 = y;
outcode1 = ComputeOutCode(x1, y1); }
}
} while (!done);
if (accept){
double sx = (xvmax - xvmin) / (xmax – xmin);
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;
21
Name: Kunwar Uday Singh
Branch: CSE OGI (B2)
Roll no: R970216036
glVertex2d(60, 20);
glVertex2d(80, 120);
glEnd();
glColor3f(0.0, 0.0, 1.0); // draw a blue colored window
glBegin(GL_LINE_LOOP);
glVertex2f(xmin, ymin);
glVertex2f(xmax, ymin);
glVertex2f(xmax, ymax);
glVertex2f(xmin, ymax);
glEnd();
CohenSutherlandLineClipAnddraw(x0, y0, x1, y1);
CohenSutherlandLineClipAnddraw(60, 20, 80, 120);
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){
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutInitWindowPosition(0, 0);
glutCreateWindow("cohen Sutherland line clipping algorithm");
glutDisplayFunc(display);
myinit();
glutMainLoop();
return 0;
}
Output:
22
Name: Kunwar Uday Singh
Branch: CSE OGI (B2)
Roll no: R970216036
Sutherland Hodgeman
Code:
#include<Windows.h>
#include<gl/GL.h>
#include<gl/GLU.h>
#include<gl/glut.h>
#include <time.h>
#include <stdio.h>
#include <math.h>
23
Name: Kunwar Uday Singh
Branch: CSE OGI (B2)
Roll no: R970216036
glClearColor(0.0, 0, 0, 0);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(-300, 300, -300, 300);
}
int main(int argc, char** argv){
printf("Enter the points for left bottom of polygon: \n");
scanf("%d%d", &xmi, &ymi);
printf("\nEnter the points for right up of the polygon: \n");
scanf("%d%d", &xma, &yma);
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(600, 600);
glutInitWindowPosition(0, 0);
glutCreateWindow("Clipping");
glutDisplayFunc(display);
glutKeyboardFunc(mykey);
init();
glutMainLoop();
return 0;
}
Output:
24
Name: Kunwar Uday Singh
Branch: CSE OGI (B2)
Roll no: R970216036
EXPERIMENT 7 & 8
Scaling : Scaling refers to zooming in and out an object in different scales across axes.
Formula: X = x*sx, Y = y*sy, sx, sy being scaling factors.
The OpenGL function is glScalef(float x, float y, float z)
Code:
#include<Windows.h>
#include<gl/GL.h>
#include<gl/GLU.h>
#include<gl/glut.h>
#include <time.h>
#include <stdio.h>
#include <math.h>
#include <iostream>
#include <vector>
using namespace std;
25
Name: Kunwar Uday Singh
Branch: CSE OGI (B2)
Roll no: R970216036
26
Name: Kunwar Uday Singh
Branch: CSE OGI (B2)
Roll no: R970216036
}
else if (choice == 3){
drawPolygon();
drawPolygonRotation(angleRad);
}
glFlush();
}
void main(int argc, char** argv){
cout << "1. Translation" << endl;
cout << "2. Scaling" << endl;
cout << "3. Rotation" << endl;
cout << "4. Exit" << endl;
cout << "\n\nEnter your choice: " << endl;
cin >> choice;
if (choice == 6) {
return;
}
cout << "\n\nFor Polygon:\n" << endl;
cout << "Enter no of edges: "; cin >> edges;
for (int i = 0; i < edges; i++){
cout << "Enter co-ordinates for vertex " << i + 1 << " : "; cin >> pntX1 >> pntY1;
pntX.push_back(pntX1);
pntY.push_back(pntY1);
}
if (choice == 1){
cout << "Enter the translation factor for X and Y: "; cin >> transX >> transY;
}
else if (choice == 2){
cout << "Enter the scaling factor for X and Y: "; cin >> scaleX >> scaleY;
}
else if (choice == 3){
cout << "Enter the angle for rotation: "; cin >> angle;
angleRad = angle * 3.1416 / 180;
}
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(640, 480);
glutInitWindowPosition(100, 150);
glutCreateWindow("Extended Basic Transformations");
glutDisplayFunc(myDisplay);
myInit();
glutMainLoop();
27
Name: Kunwar Uday Singh
Branch: CSE OGI (B2)
Roll no: R970216036
Output:
Translation -
Scaling -
Rotation -
28