Computer Graphics and Multimedia Practical Lab Report
Computer Graphics and Multimedia Practical Lab Report
Date of Experiment:
Date of Submission:
SUBMITTED TO:
ANANNA RASHID
Lect. Department of CSE
Mohammadpur Kendriya COLLEGE-6458
Noorjahan Road, Mohammadpur, Dhaka-1207 Signature
SUBMITTED BY:
MD. ISMAIL HASAN TANJER
Roll-45, Reg. No: 16502000971
6th Semester
Department of CSE
Mohammadpur Kendriya COLLEGE-6458
Noorjahan Road, Mohammadpur, Dhaka-1207
=3=
yi+1=yi + m∆x
or xi+1=xi + ∆y/m
Algorithm:
double m = (double) (y2 - y1) / (double) (x2 - x1);
double x = x1;
double y = y1;
for (int i = min(x1, x2); i <= max(x1, x2); i++) {
drawPoint(x, y);
printf("%.1f, %.1f\n", x, y);
if (m == 1) {
x = x + 1;
y = y + 1;
}
else if (m > 1) {
x = x + (double)1.0 / m;
y = y + 1;
}
else if (m < 1) {
x = x + 1;
y = y + m;
}
}
=4=
Source Code:
#include <windows.h>
#include <GL/glut.h>
#include <stdlib.h>
#include <math.h>
#include <stdio.h>
double x = x1;
double y = y1;
for (int i = min(x1, x2); i <= max(x1, x2); i++) {
drawPoint(x, y);
printf("%.1f, %.1f\n", x, y);
if (m == 1) {
x = x + 1;
y = y + 1;
}
else if (m > 1) {
x = x + (double)1.0 / m;
y = y + 1;
}
else if (m < 1) {
=5=
x = x + 1;
y = y + m;
}
}
}
glutSwapBuffers();
}
int main()
{
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(500,500);
glutInitWindowPosition(100,100);
glutCreateWindow("DDA");
glClearColor(0,0,0,0);
//glOrtho(-200, 200, -200, 200, -1, 1);
glutDisplayFunc(display);
glutMainLoop();
return EXIT_SUCCESS;
}
=6=
Sample output:
Discussion:
Date of Experiment:
Date of Submission:
SUBMITTED TO:
ANANNA RASHID
Lect. Department of CSE
Mohammadpur Kendriya COLLEGE-6458
Noorjahan Road, Mohammadpur, Dhaka-1207 Signature
SUBMITTED BY:
MD. ISMAIL HASAN TANJER
Roll-45, Reg. No: 16502000971
6th Semester
Department of CSE
Mohammadpur Kendriya COLLEGE-6458
Noorjahan Road, Mohammadpur, Dhaka-1207
=8=
Algorithm:
while (x1 < x2) {
x1++;
if (p >= 0){
y1++;
p = p + 2 * dy - 2 * dx;
}
else{
p=p+2*dy;
}
drawPoint(x1, y1);
printf("%d, %d\n", x1, y1);
}
Source Code:
#include <windows.h>
#include <GL/glut.h>
#include <stdlib.h>
#include <stdio.h>
void drawPoint(int x, int y)
{
glPointSize(2.0);
glBegin(GL_POINTS);
glColor3f( 1.0 , 1.0 , 0.0 ) ;
glVertex2f(x, y);
glEnd();
}
=9=
int p = 2 * dy - dx;
//int x = x1;
//int y = y1;
x1++;
if (p >= 0){
y1++;
p = p + 2 * dy - 2 * dx;
}
else{
p=p+2*dy;
}
drawPoint(x1, y1);
printf("%d, %d\n", x1, y1);
}
}
glutSwapBuffers();
}
int main()
{
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
=10=
glutInitWindowSize(500,500);
glutInitWindowPosition(100,100);
glutCreateWindow("Bresenham's Algorithm");
glClearColor(0,0,0,0);
glOrtho(-200, 200, -200, 200, -1, 1);
glutDisplayFunc(display);
glutMainLoop();
return EXIT_SUCCESS;
}
Sample Output:
Discussion:
Date of Experiment:
Date of Submission:
SUBMITTED TO:
ANANNA RASHID
Lect. Department of CSE
Mohammadpur Kendriya COLLEGE-6458
Noorjahan Road, Mohammadpur, Dhaka-1207 Signature
SUBMITTED BY:
MD. ISMAIL HASAN TANJER
Roll-45, Reg. No: 16502000971
6th Semester
Department of CSE
Mohammadpur Kendriya COLLEGE-6458
Noorjahan Road, Mohammadpur, Dhaka-1207
=12=
Algorithm:
int x =0;
int y =r;
int d = 3-2*r;
drawCirclePixel(x,y);
while(y>x)
{
x++;
//update d,x,y
if(d>0)
{
y--;
d=d+4*(x-y)+10;
}
else{
d=d+4*x+6;
}
drawCirclePixel(x,y);
printf("%d,%d\n",x,y);
}
Source Code:
#include <windows.h>
#include <GL/glut.h>
#include <stdlib.h>
#include <stdio.h>
void drawPixel(int x, int y)
{
glPointSize(5.0);
glBegin(GL_POINTS);
=13=
void drawCircle(int r)
{
int x =0;
int y =r;
int d = 3-2*r;
drawCirclePixel(x,y);
while(y>x)
{
x++;
//update d,x,y
if(d>0)
{
y--;
d=d+4*(x-y)+10;
}
else{
d=d+4*x+6;
}
drawCirclePixel(x,y);
printf("%d,%d\n",x,y);
=14=
}
}
static void display (void)
{
glPushMatrix();
drawCircle(10);
glPopMatrix();
glutSwapBuffers();
}
int main()
{
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(500,500);
glutInitWindowPosition(100,100);
glutCreateWindow("Bresenham_circle");
glClearColor(0,0,0,0);
glOrtho(-200, 200, -200, 200, -1, 1);
glutDisplayFunc(display);
glutMainLoop();
return EXIT_SUCCESS;
}
Sample Output:
Discussion:
Date of Experiment:
Date of Submission:
SUBMITTED TO:
ANANNA RASHID
Lect. Department of CSE
Mohammadpur Kendriya COLLEGE-6458
Noorjahan Road, Mohammadpur, Dhaka-1207 Signature
SUBMITTED BY:
MD. ISMAIL HASAN TANJER
Roll-45, Reg. No: 16502000971
6th Semester
Department of CSE
Mohammadpur Kendriya COLLEGE-6458
Noorjahan Road, Mohammadpur, Dhaka-1207
=16=
for(x=1;x<=x2;x++) {
y = b * sqrt(1 - (pow(x,2)/pow(a,2)));
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); }
=17=
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();
}
=18=
Sample Output:
Discussion:
Date of Experiment:
Date of Submission:
SUBMITTED TO:
ANANNA RASHID
Lect. Department of CSE
Mohammadpur Kendriya COLLEGE-6458
Noorjahan Road, Mohammadpur, Dhaka-1207 Signature
SUBMITTED BY:
MD. ISMAIL HASAN TANJER
Roll-45, Reg. No: 16502000971
6th Semester
Department of CSE
Mohammadpur Kendriya COLLEGE-6458
Noorjahan Road, Mohammadpur, Dhaka-1207
=20=
Algorithm:
for (int y = bl_y; y <= tl_y; y++){
for (int x = bl_x; x <=br_x; x++) {
drawPoint(x, y);
}
}
Source Code:
#include <windows.h>
#include <GL/glut.h>
#include <stdlib.h>
#include <math.h>
#include <stdio.h>
void drawPoint(int x, int y)
{
glLineWidth(1.0);
glBegin(GL_POINTS);
glColor3f( 1.0 , 1.0 , 0.0 ) ;
glVertex2f(x, y);
glEnd();
}
/*
b-bottom
t--top
l-left
r-right
*/
=21=
{
for (int y = bl_y; y <= tl_y; y++){
for (int x = bl_x; x <=br_x; x++) {
drawPoint(x, y);
}
}
}
glutSwapBuffers();
}
int main()
{
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(600,600);
glutInitWindowPosition(100,100);
glutCreateWindow("Polygon (Rectangle) filling algorithm");
glClearColor(0,0,0,0);
glOrtho(-200, 200, -200, 200, -1, 1);
glutDisplayFunc(display);
glutMainLoop();
return EXIT_SUCCESS;
}
=22=
Sample Output:
Discussion:
Date of Experiment:
Date of Submission:
SUBMITTED TO:
ANANNA RASHID
Lect. Department of CSE
Mohammadpur Kendriya COLLEGE-6458
Noorjahan Road, Mohammadpur, Dhaka-1207 Signature
SUBMITTED BY:
MD. ISMAIL HASAN TANJER
Roll-45, Reg. No: 16502000971
6th Semester
Department of CSE
Mohammadpur Kendriya COLLEGE-6458
Noorjahan Road, Mohammadpur, Dhaka-1207
=24=
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();
=25=
drawLine(x0,y0,x1,y1,0);
else if((x1<x0)&&(y1>=y0))
drawLine(-x0,y0,-x1,y1,3);
else if((x1<x0) && (y1<y0))
drawLine(-x0,-y0,-x1,-y1,4);
15
else
drawLine(x0,-y0,x1,-y1,7); }
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;
}
=27=
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;
}
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)
{
=28=
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();
}
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE);
glutInitWindowPosition(-1,-1);
glutInitWindowSize(WIDTH, HEIGHT);
glutCreateWindow("My Window");
glutReshapeFunc(reshape);
glutDisplayFunc(display);
glutMainLoop();
}
Sample Output:
Discussion:
Date of Experiment:
Date of Submission:
SUBMITTED TO:
ANANNA RASHID
Lect. Department of CSE
Mohammadpur Kendriya COLLEGE-6458
Noorjahan Road, Mohammadpur, Dhaka-1207 Signature
SUBMITTED BY:
MD. ISMAIL HASAN TANJER
Roll-45, Reg. No: 16502000971
6th Semester
Department of CSE
Mohammadpur Kendriya COLLEGE-6458
Noorjahan Road, Mohammadpur, Dhaka-1207
=30=
nx1=x1+xt;
ny1=y1+yt;
nx2=x2+xt;
ny2=y2+yt;
nx3=x3+xt;
ny3=y3+yt;
line(nx1,ny1,nx2,ny2);
line(nx2,ny2,nx3,ny3);
line(nx3,ny3,nx1,ny1);
getch();
case 2:
printf("\n Enter the angle of rotation");
scanf("%d",&r);
t=3.14*r/180;
nx1=abs(x1*cos(t)-y1*sin(t));
ny1=abs(x1*sin(t)+y1*cos(t));
nx2=abs(x2*cos(t)-y2*sin(t));
ny2=abs(x2*sin(t)+y2*cos(t));
nx3=abs(x3*cos(t)-y3*sin(t));
ny3=abs(x3*sin(t)+y3*cos(t));
line(nx1,ny1,nx2,ny2);
line(nx2,ny2,nx3,ny3);
line(nx3,ny3,nx1,ny1);
getch();
case 3:
printf("\n Enter the scalling factor");
scanf("%d%d",&sx,&sy);
nx1=x1*sx;
ny1=y2*sy;
nx2=x2*sx;
ny2=y2*sy;
nx3=x3*sx;
ny3=y3*sy;
line(nx1,ny1,nx2,ny2);
line(nx2,ny2,nx3,ny3);
line(nx3,ny3,nx1,ny1);
getch();
case 4:
break;
default:
printf("Enter the correct choice");
}
closegraph();
}
=32=
Sample Output:
Discussion: