Graphics Lab Report
Graphics Lab Report
Theory: The digital differential analyzer (DDA) is an incremental scan-conversation method. Such
an approach is characterized by performing calculations at each step using results from the preceding
step. Suppose at step i we have calculated (xi, yi) to be a point on the line. Since the next point (xi+1,
yi+1) should satisfy ∆y/∆x=m where ∆y=yi+1-yi and ∆x=xi+1-xi we have
yi+1=yi + m∆x
or xi+1=xi + ∆y/m
Algorithm:
int x=x’1, y=y’1;
int dx= x’2-x’1, dy= y’2-y’1, dT=2(dy-dx), dS=2dy;
int d=2dy-dx; setPixel(x,y);
while(x<x’2) {
x++;
if(d<0)
d=d+ds;
else {
y++;
d=d+dT;
}
setPixel(x,y);
}
Source Code:
#include<windows.h>
#include<stdio.h>
#include<math.h>
#include<GL/glut.h>
void myInit(void) {
glClearColor(1.0, 1.0, 1.0, 0.0);
glColor3f(0.0f, 0.0f, 0.0f);
glPointSize(4.0);
glLineWidth(4.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1.0,1.0,-1.0, 1.0, -1.0, 1.0);
gluOrtho2D(0.0, 640.0, 0.0, 480.0); }
void myDisplay(void) {
1
glClear(GL_COLOR_BUFFER_BIT);
glPointSize(20.0);
glBegin(GL_POINTS);
{
glVertex2i(20,10);
glVertex2i(50,10);
glVertex2i(20,80);
glVertex2i(50,80);
}
glEnd();
glFlush(); }
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(640,480);
glutInitWindowPosition(100,150);
glutCreateWindow("My First Window");
glutDisplayFunc(myDisplay);
myInit();
glutMainLoop();
Sample output:
Discussion:
This program is to implement DDA line algorithm.
2
Experiment Name: Write a program to implement Bresenham’s line algorithm.
Theory: Bresenham’s line algorithm is a highly efficient incremental method for scan-converting
lines. We want to scan-convert the line where 0<m<1. We start with pixel P’1(x’1, y’1), then select
subsequent pixels as we work our way to the right, one pixel position at a time in the horizontal
direction towards P’2(x’2, y’2).
Algorithm:
int x=x’1, y=y’1;
int dx= x’2-x’1, dy= y’2-y’1, dT=2(dy-dx), dS=2dy;
int d=2dy-dx; setPixel(x,y);
while(x<x’2) {
x++;
if(d<0)
d=d+ds;
else {
y++;
d=d+dT;
}
setPixel(x,y);
}
Source Code:
#include<windows.h>
#include<GL/glut.h>
void myInit(void)
{
glClearColor(1.0, 1.0, 1.0, 0.0);
glColor3f(0.0f, 0.0f, 0.0f);
glPointSize(4.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1.0,1.0,-1.0, 1.0, -1.0, 1.0);
gluOrtho2D(0.0, 640.0, 0.0, 480.0);
}
void myDisplay(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glPointSize(2.0);
3
GLint x,y, x1=50, x2=100, y1=100, y2=200;
GLint dx, dy, inc1, inc2, d;
x = x1; y = y1;
dx = x2-x1; dy = y2-y1;
inc1 = 2 * dy; inc2 = 2 * (dy-dx);
d = inc1 - dx;
while(x <= x2) {
glBegin(GL_POINTS);
{
glVertex2i(x,y);
}
glEnd();
x++;
if(d < 0)
d = d + inc1;
else {d = d + inc2;
y++;
glFlush();
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(640,480);
glutInitWindowPosition(100,150);
glutDisplayFunc(myDisplay);
myInit();
glutMainLoop();
4
}
Sample Output:
Discussion:
This program is to implement Bresenham’s line algorithm.
5
Experiment Name: Write a program to implement Bresenham’s circle algorithm.
Algorithm:
int x=0, y=r, d=3-2r;
while(x<=y) {
setPixel(x,y);
if(d<0)
d=d+4x+6;
else {
d=d+4(x-y)+10;
y--;
}
x++;
}
Source Code:
#include<windows.h>
#include<iostream.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<GL/gl.h>
#include<GL/glu.h>
#include<GL/glut.h>
void myInit(void)
{
glClearColor(1.0, 1.0, 1.0, 0.0);
glColor3f(0.0f, 0.0f, 0.0f);
glPointSize(4.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, 640.0, 0.0, 480.0);
}
6
void myDisplay(void) {
glClear(GL_COLOR_BUFFER_BIT);
glPointSize(2.0);
GLint h = 200,k = 200, x, y, r, d;
y = r = 100;
d = 3 - 2*r;
for(x=0; x<=y; x++)
{
glBegin(GL_POINTS);
{
glVertex2i(x+h,y+k);
glVertex2i(x+h,k-y);
glVertex2i(y+h,x+k);
glVertex2i(y+h,k-x);
glVertex2i(-y+h,x+k);
glVertex2i(-y+h,k-x);
glVertex2i(-x+h,y+k);
glVertex2i(-x+h,k-y);
}
glEnd();
if(d<0)
d = d + 4*x + 6;
else{
d = d + 4*(x-y) + 10;
y--;
}
}
glFlush();
}
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(640,480);
glutInitWindowPosition(100,150);
glutDisplayFunc(myDisplay);
myInit();
glutMainLoop();
7
}
Sample Output:
Discussion:
This program is to implement Bresenham’s circle algorithm.
8
Experiment Name: Write a program to implement midpoint ellipse drawing algorithm.
Objective: This objective of the program is to implement midpoint ellipse drawing algorithm.
Theory: The ellipse, like the circle, shows symmetry. In the case of an ellipse, however, symmetry
is four-rather than eight-way. There are two methods of mathematically defining a ellipse. The
polynomial method of defining an ellipse is given by the expression
(x-h)2/a2 + (y-k)2/b2 = 1
Source Code:
#include<windows.h>
#include<iostream.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<GL/gl.h>
#include<GL/glu.h>
#include<GL/glut.h>
const int screenWidth = 640*2;
const int screenHeight = 480*2;
GLdouble A,B,C,D;
void myInit(void)
{
glClearColor(1.0, 1.0, 1.0, 0.0);
glColor3f(0.0f, 0.0f, 0.0f);
glPointSize(4.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1.0,1.0,-1.0, 1.0, -1.0, 1.0);
A = screenWidth/4.0;
B= 0.0;
C=D=screenHeight/2.0;
gluOrtho2D(0.0, 640.0, 0.0, 480.0);
}
void myDisplay(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glPointSize(2.0);
GLint h = 300, k = 300, x, x2, y, i = 1, a = 150, b = 25; x2 = a;
for(x=1;x<=x2;x++) {
9
y = b * sqrt(1 - (pow(x,2)/pow(a,2)));
glBegin(GL_POINTS);
{
glVertex2i(x+h,y+k);
glVertex2i(-x+h,-y+k);
glVertex2i(-x+h,y+k);
glVertex2i(x+h,-y+k);
}
glEnd();
glFlush();
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(640,480);
glutInitWindowPosition(100,150);
glutDisplayFunc(myDisplay);
myInit();
glutMainLoop();
10
Sample Output:
Discussion:
This program is to implement ellipse drawing algorithm.
11
Experiment Name: Write a program to implement Cohen-Suntherland line clipping algorithm.
Theory: A rectangle whose sides are parallel to the coordinate axes may be constructed if the
locations of two vertices. The remaining corner points are then derived. Once the vertices are known,
the 4 sets of coordinates are sent to the line routine & the rectangle is scan-converted. In the case of
the rectangle, lines would be drawn as follows: line (x1,y1) to (x1,y1); line (x1,y2) to (x2,y2); line (x2,y2)
to (x2,y1); line (x2,y1) to (x1,y1).
Source Code:
#include<windows.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<GL/gl.h>
#include<GL/glu.h>
#include<GL/glut.h>
const int screenWidth = 640;
const int screenHeight = 480;
GLdouble A,B,C,D;
void myInit(void)
{
glClearColor(1.0, 1.0, 1.0, 0.0);
glColor3f(0.0f, 0.0f, 0.0f);
glPointSize(4.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1.0,1.0,-1.0, 1.0, -1.0, 1.0);
A = screenWidth/4.0;
B= 0.0;
C=D=screenHeight/2.0;
gluOrtho2D(0.0, 640.0, 0.0, 480.0);
}
void myDisplay(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.4,0.0,0.0);
glRecti(200,150,500,330);
glFlush();
12
}
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(640,480);
glutInitWindowPosition(100,150);
glutDisplayFunc(myDisplay);
myInit();
glutMainLoop();
return 1;
Sample Output:
Discussion:
This program is to implement polygon (rectangle) filling algorithm.
13
Experiment Name: Write a program to implement Cohen-Suntherland line clipping algorithm.
Theory: In this algorithm we divide the line clipping process two phases: (1) identify those lines
which intersect the clipping window & so need to be clipped & (2) perform the clipping. All lines fall
into one of the following clipping categories:
1. Visible- both endpoints of the line lie within the window.
2. Not visible- the line definitely lies outside the window. This will occur if the line from (x1,y1) to
(x2,y2) satisfies any one of the following four inequalities:
x1,x2 > xmax y1,y2 > ymax
x1,x2 < xmin y1,y2 < ymin
3. Clipping candidate- the line is in neither category 1 nor 2.
Source Code:
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<iterator>
#include <windows.h>
#include <gl/glut.h>
using namespace std;
#define REP(i,n) for(i=0; i<(n); i++)
#define FOR(i,a,b) for(i=(a); i<=(b); i++)
#define WIDTH 640
#define HEIGHT 480
#define Wxmin 0
#define Wxmax 200
#define Wymin 0
#define Wymax 200
struct wind {
double x[4],y[4];
}W;
void reshape(int width, int height) {
glViewport(0,0,width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-WIDTH/2, WIDTH/2+1, -HEIGHT/2, HEIGHT/2+1,-1000,1000);
14
}
else {
if((x1>=x0)&&(y1>=y0))
drawLine(y0,x0,y1,x1,1);
else if((x1<x0)&&(y1>=y0))
drawLine(y0,-x0,y1,-x1,2);
else if((x1<x0)&&(y1<y0))
drawLine(-y0,-x0,-y1,-x1,5);
else if((x1>=x0)&&(y1<=y0))
drawLine(-y0,x0,-y1,x1,6); }
}
void CohenSutherlandLineClipAndDraw(double x0,double y0,double x1,double y1,double
xmin,double xmax,double ymin,double ymax)
{
outcode outcode0,outcode1,outcodeOut;
bool accept = false,done = false;
outcode0 = CompOutCode(x0,y0,xmin,xmax,ymin,ymax);
outcode1 = CompOutCode(x1,y1,xmin,xmax,ymin,ymax);
do{
if(!(outcode0|outcode1))
accept = true; done = true;
else if(outcode0&outcode1)
done = true;
else {
double x,y;
outcodeOut = outcode0?outcode0:outcode1;
if(outcodeOut&TOP){
x = x0 + (x1-x0)*(ymax-y0)/(y1-y0);
y = ymax;
}
else if(outcodeOut&BOTTOM){
x = x0 + (x1-x0)*(ymin-y0)/(y1-y0);
y = ymin;
}
else if(outcodeOut&RIGHT){
y = y0 + (y1-y0)*(xmax-x0)/(x1-x0);
x = xmax;
}
else {
y = y0 + (y1-y0)*(xmin-x0)/(x1-x0);
x = xmin;
16
}
if(outcodeOut==outcode0) {
x0 = x; y0= y;
outcode0 = CompOutCode(x0,y0,xmin,xmax,ymin,ymax); }
else {
x1 = x; y1 = y;
outcode1 = CompOutCode(x1,y1,xmin,xmax,ymin,ymax); } }
}while(done==false);
if(accept) {
drawSlop(x0,y0,x1,y1);
CohenSutherlandLineClipAndDraw(P[i].x,P[i].y,P[j].x,P[j].y,W.x[0],W.x[1],W.y[0],W.y[2]);
REP(i,2)
j = (i + 2)%4;
CohenSutherlandLineClipAndDraw(P[i].x,P[i].y,P[j].x,P[j].y,W.x[0],W.x[1],W.y[0],W.y[2]);
void display(void)
glColor4f(1.0,0.0,0.0,1.0);
angle = 0;
getPoint();
getPos();
setW();
while(true)
17
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBegin(GL_POINTS);
resetW();
glColor4f(3.0,5.0,1.0,1.0);
drawW();
glColor4f(0.0,0.0,0.0,0.0);
drawTetra();
glColor4f(1.0,0.0,0.0,1.0);
drawTetraC();
glEnd();
glutSwapBuffers();
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE);
glutInitWindowPosition(-1,-1);
glutInitWindowSize(WIDTH, HEIGHT);
glutCreateWindow("My Window");
glutReshapeFunc(reshape);
glutDisplayFunc(display);
glutMainLoop();
Discussion:
This program is to implement polygon (rectangle) filling algorithm.
18
Experiment Name: Write a program to implement a scaling & shifting polygon by mouse
clipping.
Objective: This objective of the program is to implement a scaling & shifting polygon by mouse
clipping.
Theory: In this algorithm we divide the line clipping process two phases: (1) identify those lines
which intersect the clipping window & so need to be clipped & (2) perform the clipping. All lines fall
into one of the following clipping categories:
1. Visible- both endpoints of the line lie within the window.
2. Not visible- the line definitely lies outside the window. This will occur if the line from (x1,y1) to
(x2,y2) satisfies any one of the following four inequalities:
x1,x2 > xmax y1,y2 > ymax
x1,x2 < xmin y1,y2 < ymin
3. Clipping candidate- the line is in neither category 1 nor 2.
Source Code:
#include<windows.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<GL/gl.h>
#include<GL/glu.h>
#include<GL/glut.h>
const int screenWidth = 640;
const int screenHeight = 480;
GLdouble A,B,C,D;
void myInit(void) {
glClearColor(1.0, 1.0, 1.0, 0.0);
glColor3f(0.0f, 0.0f, 0.0f);
glPointSize(4.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1.0,1.0,-1.0, 1.0, -1.0, 1.0);
A = screenWidth/4.0;
B= 0.0; C=D=screenHeight/2.0;
gluOrtho2D(0.0, 640.0, 0.0, 480.0); }
void myMouse(int button, int state, int x ,int y) {
if(button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) {
glColor4f(0.0,1.0,0.0,1.0);
glRecti(200,150,500,330);
19
}
glutDisplayFunc(myDisplay);
glutMouseFunc(myMouse);
myInit();
20
glutMainLoop();
Sample Output:
Discussion:
This program is to implement a scaling & shifting polygon by mouse clipping.
21
Experiment Name: Write a program to implement a scaling line triangle transformation.
Objective: This objective of the program is to implement a scaling line triangle transformation.
Theory: Scaling is the process of expanding or compressing the dimensions of an object. Positive
scaling constants sx and sy are used to describe changes in length with respect to the x direction and y
direction, respectively. A scaling constant greater than one indicates an expansion of length, and less
than one, compressing of length. The scaling transformation [Link] is given by P’=[Link](P) where
x’=sxx and y’ syy. Notice that, after a scaling transformation is performed, the new object is located at
a different position relative to the origin.
Source Code:
#include <windows.h>
#include <GL/gl.h>
#include <GL/glut.h>
void triangle()
{
glColor3f(0.0, 1.0, 1.0);
glBegin(GL_TRIANGLES);
glVertex2i (80, 150);
glVertex2i (80, 250);
glVertex2i (130,200);
}
void line(int sx,int sy,int dx,int dy)
{
glColor3f(1.0, 1.0, 1.0);
glBegin(GL_LINES);
glVertex2i (sx, sy);
glVertex2i (dx, dy);
}
void display(void)
{
glClear (GL_COLOR_BUFFER_BIT);
line(100,150,100,250);
triangle();
glTranslatef(70.0, 20.0,0.0);
glEnable(GL_LINE_STIPPLE);
glLineStipple(1, 0xF0F0);
line(100,150,100,250);
triangle();
}
22
void init (void)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize (350, 350);
glutInitWindowPosition (100, 100);
glutCreateWindow ("Transformation");
init();
glutDisplayFunc(display);
glutMainLoop();
Sample Output:
Discussion:
This program is to implement a scaling line triangle transformation.
23
Experiment Name: Write a program to implement composite (transition+scaling) transformation.
Theory: More complex geometric and coordinate transformations can be built from the basic
transformations described by using the process of composition of functions. For example, such
operations as rotation about a point other than the origin or reflection about lines other than the axes
can be constructed from the basic transformations. If the xy coordinate system is displaced to a new
position, where the direction and distance of the displacement is given by the vector v=txI+tyJ, the
coordinates of a point systems are related by the translation transformation Tv, where x’=x-tx and
y’=y-ty.
Source Code:
#include <GL/glut.h>
#include <stdlib.h>
void init(void)
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
void draw_triangle(void)
glBegin (GL_LINE_LOOP);
glVertex2f(0.0, 25.0);
glVertex2f(25.0, -25.0);
glVertex2f(-25.0, -25.0);
}
24
void display(void)
glClear (GL_COLOR_BUFFER_BIT);
glLoadIdentity ();
draw_triangle ();
glEnable (GL_LINE_STIPPLE);
glLoadIdentity ();
draw_triangle ();
glLoadIdentity ();
draw_triangle ();
glLoadIdentity ();
draw_triangle ();
glDisable (GL_LINE_STIPPLE);
glutInit(&argc, argv);
25
glutInitWindowSize (500, 500);
glutCreateWindow (argv[0]);
init ();
glutDisplayFunc(display);
glutMainLoop();
Sample Output:
Discussion:
This program is to implement composite (transition+scaling) transformation.
26