SECTION-A:::::::::::::::::
PROGRAM-1::::::::::::::
Program Title:
Write a program to implement DDA line drawing algorithm.
Source Code:
#include <windows.h>
#include <gl/glut.h>
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 1.0, 1.0);
glBegin(GL_POINTS);
int x1 = 10, y1 = 10, x2 = 100, y2 = 100, x, y, xend, dx, dy;
float m, b;
dx = x2 -x1;
dy = y2 -y1;
m = dy / dx;
b = y1 - m*x1;
if(dx < 0) {
x = x2;
y = y2;
xend = x1;
} else {
x = x1;
y = y1;
xend = x2;
}
while(x < xend){
glVertex2i(x,y);
x = x+1;
y = m * x + b;
}
glEnd();
glFlush();
}
int main(int argc,char *argv[])
{
glutInit(&argc,argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize (500, 500);
glutInitWindowPosition (100, 100);
glutCreateWindow ("points");
glClearColor(0,0,0,0.0);
glMatrixMode (GL_PROJECTION);
gluOrtho2D (0.0, 200.0, 0.0, 150.0);
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
Output:
PROGRAM-2::::::::::::::
Program Title:
Write a program to implement Bresenhams line drawing algorithm.
Source Code:
#include <windows.h>
#include <GL/glut.h>
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 1.0, 1.0);
glBegin(GL_POINTS);
int x1 = 60, y1 = 40, x2 = 120, y2 = 120, x, y, xend, dx, dy, inc1, inc2, d;
dx = x2 -x1;
dy = y2 -y1;
inc1 = 2 * dy;
inc2 = 2 * (dy-dx);
d = inc1 - dx;
if(dx < 0) {
x = x2;
y = y2;
xend = x1;
} else {
x = x1;
y = y1;
xend = x2;
}
glVertex2i(x,y);
while(x <= xend){
if(d<0)
{
d = d + inc1;
}
else {
d = d + inc2;
y = y + 1;
}
x = x + 1;
glVertex2i(x,y);
}
glEnd();
glFlush();
}
int main(int argc,char *argv[])
{
glutInit(&argc,argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize (500, 500);
glutInitWindowPosition (100, 100);
glutCreateWindow ("points");
glClearColor (0,0,0,0.0);
glMatrixMode (GL_PROJECTION);
gluOrtho2D (0.0, 200.0, 0.0, 150.0);
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
Output:
PROGRAM-3::::::::::::::
Program Title:
Write a program to implement Bresenhams circle drawing algorithm.
Source Code:
#include <windows.h>
#include <GL/glut.h>
#include<math.h>
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 1.0, 1.0);
glBegin(GL_POINTS);
int x, y, r= 40, h = 100, k= 80, d;
x = 0;
y = r;
d = 3 - (2*r);
while(x < y){
glVertex2i(x+h, y+k);
glVertex2i(y+h, x+k);
glVertex2i(h-y, x+k);
glVertex2i(h-x, y+k);
glVertex2i(h-x, k-y);
glVertex2i(h-y, k-x);
glVertex2i(y+h, k-x);
glVertex2i(x+h, k-y);
if (d < 0){
d = d + (4 * x) + 6;
x = x + 1;
}
else{
d = d + (4 * (x - y)) + 10;
x = x + 1;
y = y -1;
}
}
glEnd();
glFlush();
}
int main(int argc,char *argv[])
{
glutInit(&argc,argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize (650, 500);
glutInitWindowPosition (100, 100);
glutCreateWindow ("points");
glClearColor(0,0,0,0.0);
glMatrixMode (GL_PROJECTION);
gluOrtho2D (0.0, 200.0, 0.0, 150.0);
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
Output:
PROGRAM-4::::::::::::::
Program Title:
Write a program to implement midpoint ellipse drawing algorithm.
Source Code:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void drawEllipse(float xc, float yc, float x, float y)
{
putpixel(xc+x, yc+y, RED);
putpixel(xc-x, yc+y, RED);
putpixel(xc+x, yc-y, RED);
putpixel(xc-x, yc-y, RED);
}
void ellipseMidpoint(float xc, float yc, float rx, float ry)
{
float rxSq = rx * rx;
float rySq = ry * ry;
float x = 0, y = ry, p;
float px = 0, py = 2 * rxSq * y;
drawEllipse(xc, yc, x, y);
p = rySq - (rxSq * ry) + (0.25 * rxSq);
while (px < py)
{
x++;
px = px + 2 * rySq;
if (p < 0)
p = p + rySq + px;
else
{
y--;
py = py - 2 * rxSq;
p = p + rySq + px - py;
}
drawEllipse(xc, yc, x, y);
}
p = rySq*(x+0.5)*(x+0.5) + rxSq*(y-1)*(y-1) - rxSq*rySq;
while (y > 0)
{
y--;
py = py - 2 * rxSq;
if (p > 0)
p = p + rxSq - py;
else
{
x++;
px = px + 2 * rySq;
p = p + rxSq - py + px;
}
drawEllipse(xc, yc, x, y);
}
}
int main()
{
int xc, yc, rx, ry;
int gd = DETECT, gm;
initgraph(&gd, &gm, "c:tcbgi");
printf("tttMID POINT ELLIPSE DRAWING ALGORITHMnntEnter the center coordinates of ellipse:");
scanf("%d %d",&xc,&yc);
printf("ntEnter x-radius coordinate:");
scanf("%d",&rx);
printf("ntEnter y-radius coordinate:");
scanf("%d",&ry);
ellipseMidpoint(xc, yc, rx, ry);
getch();
closegraph();
return 0;
}
Input:
Output:
PROGRAM-5::::::::::::::
Program Title:
Write a program to draw a house.
Source Code:
#include<GL/glu.h>
#include<GL/glut.h>
#include<windows.h>
void init(void)
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0.0, 200.0, 0.0, 150.0);
}
void buildHouse(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 0.0, 0.0);
glBegin(GL_POLYGON); //start house
glColor3f(1.0, 1.0, 0.0);
glVertex2i(50, 0);
glColor3f(0.0, 1.0, 0.0);
glVertex2i(50, 100);
glColor3f(0.0, 1.0, 1.0);
glVertex2i(150, 100);
glColor3f(1.0, 1.0, 0.0);
glVertex2i(150,0);
glEnd(); //end house
glBegin(GL_POLYGON); //start window
glColor3f(0.3, 0.2, 0.0);
glVertex2i(60, 80);
glVertex2i(80, 80);
glVertex2i(80, 65);
glVertex2i(60, 65);
glEnd(); //end window
glBegin(GL_POLYGON); //start window
glColor3f(0.3, 0.2, 0.0);
glVertex2i(120, 80);
glVertex2i(140, 80);
glVertex2i(140, 65);
glVertex2i(120, 65);
glEnd(); //end window
glBegin(GL_POLYGON); //start ceiling
glColor3f(0.8, 0.0, 0.0);
glVertex2i(40, 100);
glColor3f(0.5, 0.0, 0.3);
glVertex2i(160, 100);
glColor3f(1.0, 0.0, 0.0);
glVertex2i(100, 130);
glEnd(); //end ceiling
glBegin(GL_POLYGON); //start door
glColor3f(0.3, 0.2, 0.0);
glVertex2i(80, 0);
glVertex2i(80, 50);
glVertex2i(120, 50);
glVertex2i(120,0);
glEnd(); //end door
glFlush();
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowPosition(50, 100);
glutInitWindowSize(500, 500);
glutCreateWindow("House Section OpenGL");
init();
glutDisplayFunc(buildHouse);
glutMainLoop();
return 0;
}
Output:
Hay listen carefully, don’t draw this home with your own color, just use pencil.
SECTION-B:::::::::::::::::
PROGRAM-1::::::::::::::
Program Title:
Write a program to implement Cohen Sutherland Line Clipping algorithm.
Source Code:
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<graphics.h>
#include<dos.h>
typedef struct coordinate
{
int x,y;
char code[4];
}PT;
void drawwindow();
void drawline(PT p1,PT p2);
PT setcode(PT p);
int visibility(PT p1,PT p2);
PT resetendpt(PT p1,PT p2);
int main()
{
int gd=DETECT,v,gm;
PT p1,p2,p3,p4,ptemp;
printf("nEnter x1 and y1n");
scanf("%d %d",&p1.x,&p1.y);
printf("nEnter x2 and y2n");
scanf("%d %d",&p2.x,&p2.y);
initgraph(&gd,&gm,"c:turboc3bgi");
drawwindow();
delay(500);
drawline(p1,p2);
delay(500);
cleardevice();
delay(500);
p1=setcode(p1);
p2=setcode(p2);
v=visibility(p1,p2);
delay(500);
switch(v)
{
case 0: drawwindow();
delay(500);
drawline(p1,p2);
break;
case 1: drawwindow();
delay(500);
break;
case 2: p3=resetendpt(p1,p2);
p4=resetendpt(p2,p1);
drawwindow();
delay(500);
drawline(p3,p4);
break;
}
delay(5000);
closegraph();
}
void drawwindow()
{
line(150,100,450,100);
line(450,100,450,350);
line(450,350,150,350);
line(150,350,150,100);
}
void drawline(PT p1,PT p2)
{
line(p1.x,p1.y,p2.x,p2.y);
}
PT setcode(PT p) //for setting the 4 bit code
{
PT ptemp;
if(p.y<100)
ptemp.code[0]='1'; //Top
else
ptemp.code[0]='0';
if(p.y>350)
ptemp.code[1]='1'; //Bottom
else
ptemp.code[1]='0';
if(p.x>450)
ptemp.code[2]='1'; //Right
else
ptemp.code[2]='0';
if(p.x<150)
ptemp.code[3]='1'; //Left
else
ptemp.code[3]='0';
ptemp.x=p.x;
ptemp.y=p.y;
return(ptemp);
}
int visibility(PT p1,PT p2)
{
int i,flag=0;
for(i=0;i<4;i++)
{
if((p1.code[i]!='0') || (p2.code[i]!='0'))
flag=1;
}
if(flag==0)
return(0);
for(i=0;i<4;i++)
{
if((p1.code[i]==p2.code[i]) && (p1.code[i]=='1'))
flag='0';
}
if(flag==0)
return(1);
return(2);
}
PT resetendpt(PT p1,PT p2)
{
PT temp;
int x,y,i;
float m,k;
if(p1.code[3]=='1')
x=150;
if(p1.code[2]=='1')
x=450;
if((p1.code[3]=='1') || (p1.code[2]=='1'))
{
m=(float)(p2.y-p1.y)/(p2.x-p1.x);
k=(p1.y+(m*(x-p1.x)));
temp.y=k;
temp.x=x;
for(i=0;i<4;i++)
temp.code[i]=p1.code[i];
if(temp.y<=350 && temp.y>=100)
return (temp);
}
if(p1.code[0]=='1')
y=100;
if(p1.code[1]=='1')
y=350;
if((p1.code[0]=='1') || (p1.code[1]=='1'))
{
m=(float)(p2.y-p1.y)/(p2.x-p1.x);
k=(float)p1.x+(float)(y-p1.y)/m;
temp.x=k;
temp.y=y;
for(i=0;i<4;i++)
temp.code[i]=p1.code[i];
return(temp);
}
else
return(p1);
}
Input:
Output:
PROGRAM-2::::::::::::::
Program Title:
Write a program to implement Sutherland Hodgeman Polygon Clipping algorithm
Source Code:
#include<iostream>
#include<windows.h>
#include<graphics.h>
#include<conio.h>
#include<stdlib.h>
using namespace std;
#define round(a) ((int)(a+0.5))
int k;
float xmin,ymin,xmax,ymax,arr[20],m;
void clipl(float x1,float y1,float x2,float y2)
{
if(x2-x1)
m=(y2-y1)/(x2-x1);
else
m=100000;
if(x1 >= xmin && x2 >= xmin)
{
arr[k]=x2;
arr[k+1]=y2;
k+=2;
}
if(x1 < xmin && x2 >= xmin)
{
arr[k]=xmin;
arr[k+1]=y1+m*(xmin-x1);
arr[k+2]=x2;
arr[k+3]=y2;
k+=4;
}
if(x1 >= xmin && x2 < xmin)
{
arr[k]=xmin;
arr[k+1]=y1+m*(xmin-x1);
k+=2;
}
}
void clipt(float x1,float y1,float x2,float y2)
{
if(y2-y1)
m=(x2-x1)/(y2-y1);
else
m=100000;
if(y1 <= ymax && y2 <= ymax)
{
arr[k]=x2;
arr[k+1]=y2;
k+=2;
}
if(y1 > ymax && y2 <= ymax)
{
arr[k]=x1+m*(ymax-y1);
arr[k+1]=ymax;
arr[k+2]=x2;
arr[k+3]=y2;
k+=4;
}
if(y1 <= ymax && y2 > ymax)
{
arr[k]=x1+m*(ymax-y1);
arr[k+1]=ymax;
k+=2;
}
}
void clipr(float x1,float y1,float x2,float y2)
{
if(x2-x1)
m=(y2-y1)/(x2-x1);
else
m=100000;
if(x1 <= xmax && x2 <= xmax)
{
arr[k]=x2;
arr[k+1]=y2;
k+=2;
}
if(x1 > xmax && x2 <= xmax)
{
arr[k]=xmax;
arr[k+1]=y1+m*(xmax-x1);
arr[k+2]=x2;
arr[k+3]=y2;
k+=4;
}
if(x1 <= xmax && x2 > xmax)
{
arr[k]=xmax;
arr[k+1]=y1+m*(xmax-x1);
k+=2;
}
}
void clipb(float x1,float y1,float x2,float y2)
{
if(y2-y1)
m=(x2-x1)/(y2-y1);
else
m=100000;
if(y1 >= ymin && y2 >= ymin)
{
arr[k]=x2;
arr[k+1]=y2;
k+=2;
}
if(y1 < ymin && y2 >= ymin)
{
arr[k]=x1+m*(ymin-y1);
arr[k+1]=ymin;
arr[k+2]=x2;
arr[k+3]=y2;
k+=4;
}
if(y1 >= ymin && y2 < ymin)
{
arr[k]=x1+m*(ymin-y1);
arr[k+1]=ymin;
k+=2;
}
}
int main()
{
int gdriver=DETECT,gmode,n,poly[20], i;
float xi,yi,xf,yf,polyy[20];
//clrscr();
system("cls");
cout<<"Coordinates of rectangular clip window :nxmin,ymin :";
cin>>xmin>>ymin;
cout<<"xmax,ymax :";
cin>>xmax>>ymax;
cout<<"nnPolygon to be clipped :nNumber of sides :";
cin>>n;
cout<<"Enter the coordinates :";
for(i=0;i < 2*n;i++)
cin>>polyy[i];
polyy[i]=polyy[0];
polyy[i+1]=polyy[1];
for(i=0;i < 2*n+2;i++)
poly[i]=round(polyy[i]);
initgraph(&gdriver,&gmode,"C:TCBGI");
setcolor(RED);
rectangle(xmin,ymax,xmax,ymin);
cout<<"ttUNCLIPPED POLYGON";
setcolor(WHITE);
fillpoly(n,poly);
getch();
cleardevice();
k=0;
for(i=0;i < 2*n;i+=2)
clipl(polyy[i],polyy[i+1],polyy[i+2],polyy[i+3]);
n=k/2;
for(i=0;i < k;i++)
polyy[i]=arr[i];
polyy[i]=polyy[0];
polyy[i+1]=polyy[1];
k=0;
for(i=0;i < 2*n;i+=2)
clipt(polyy[i],polyy[i+1],polyy[i+2],polyy[i+3]);
n=k/2;
for(i=0;i < k;i++)
polyy[i]=arr[i];
polyy[i]=polyy[0];
polyy[i+1]=polyy[1];
k=0;
for(i=0;i < 2*n;i+=2)
clipr(polyy[i],polyy[i+1],polyy[i+2],polyy[i+3]);
n=k/2;
for(i=0;i < k;i++)
polyy[i]=arr[i];
polyy[i]=polyy[0];
polyy[i+1]=polyy[1];
k=0;
for(i=0;i < 2*n;i+=2)
clipb(polyy[i],polyy[i+1],polyy[i+2],polyy[i+3]);
for(i=0;i < k;i++)
poly[i]=round(arr[i]);
if(k)
fillpoly(k/2,poly);
setcolor(RED);
rectangle(xmin,ymax,xmax,ymin);
cout<<"tCLIPPED POLYGON";
getch();
closegraph();
return 0;
}
Input:
Output:
PROGRAM-3::::::::::::::
Program Title:
Write a program to implement Window to Viewport transformation.
Source Code:
#include<stdio.h>
#include<conio.h>
#include<dos.h>
#include<graphics.h>
#include<math.h>
int main()
{
int xwmin,ywmin,xwmax,ywmax,xv1,yv1;
int xvmin,xvmax,yvmin,yvmax,xw,yw,xv,yv;
int gd=DETECT,gm;
initgraph(&gd,&gm,"");
printf("Enter the window coordinates xwmin,xwmax,ywmin,ywmaxn");
scanf("%dt%dt%dt%d",&xwmin,&xwmax,&ywmin,&ywmax);
line(xwmin-25,xwmin-25,xwmin-25,ywmax+50);
line(xwmin-40,ywmax+25,xwmax+50,ywmax+25);
outtextxy(xwmin+5,ywmax+5,"Window");
line(xwmin,ywmin,xwmin,ywmax);
line(xwmin,ywmax,xwmax,ywmax);
line(xwmax,ywmax,xwmax,ywmin);
line(xwmax,ywmin,xwmin,ywmin);
xvmax=xwmax/2;
xvmin=xwmin/2;
yvmin=ywmin/2;
yvmax=ywmax/2;
line(xvmin+275,xvmin+275,xvmin+275,yvmax+325);
line(xvmin+255,yvmax+315,xvmax+325,yvmax+315);
outtextxy(xvmin+305,yvmax+305,"Viewport");
line(xvmin+300,yvmin+300,xvmin+300,yvmax+300);
line(xvmin+300,yvmax+300,xvmax+300,yvmax+300);
line(xvmax+300,yvmax+300,xvmax+300,yvmin+300);
line(xvmax+300,yvmin+300,xvmin+300,yvmin+300);
xw=xwmin+50;
yw=ywmin+50;
putpixel(xw,yw,4);
xv1=((xvmax-xvmin)/(xwmax-xwmin))*(xw-xwmin);
xv=xv1+xvmin;
yv1=((yvmax-yvmin)/(ywmax-ywmin))*(yw-ywmin);
yv=yv1+yvmin;
putpixel(xv+325,yv+325,2);
getch();
closegraph();
}
Input:
Output:
PROGRAM-4::::::::::::::
Program Title:
Write a program to implement the following 2D transformations:-
a) Translation
b) Scaling
c) Rotation
Source Code:
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include<math.h>
int main()
{
int gm;
int gd=DETECT;
int x1,x2,x3,y1,y2,y3,nx1,nx2,nx3,ny1,ny2,ny3,c;
int sx,sy,xt,yt,r;
float t;
initgraph(&gd,&gm,"c:tcbg:");
printf("t Program for basic transactions");
printf("nt Enter the points of triangle");
setcolor(1);
scanf("%d%d%d%d%d%d",&x1,&y1,&x2,&y2,&x3,&y3);
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
getch();
printf("n 1.Transactionn 2.Rotationn 3.Scallingn 4.exit");
printf("Enter your choice:");
scanf("%d",&c);
switch(c)
{
case 1:
printf("n Enter the translation factor");
scanf("%d%d",&xt,&yt);
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();
}
Input:
Output:
PROGRAM-5::::::::::::::
Program Title:
Write a program to draw a triangle and rotate about the pivot point.
Source Code:
#include<graphics.h>
#include<conio.h>
#include<stdio.h>
#include<math.h>
int main()
{
int gd=DETECT,gm;
int i,xmid,ymid,x1,y1,x2,y2,x3,y3,x,y,dy,dx,p,gap=50,temp,xr,yr;
int x1dash,y1dash,x2dash,y2dash,x3dash,y3dash;
float m;
double theta;
char str[5];
//clrscr();
initgraph(&gd,&gm,"..bgi");
printf("Enter first co-ords of the trianglen");
scanf("%d %d",&x1,&y1);
printf("Enter second co-ords of the trianglen");
scanf("%d %d",&x2,&y2);
printf("Enter third co-ords of the trianglen");
scanf("%d %d",&x3,&y3);
xmid= getmaxx()/2;
ymid= getmaxy()/2;
line(5,ymid,getmaxx()-5,ymid);
line(xmid+3,5,xmid+3,getmaxy()-5);
for( i= xmid+gap;i<getmaxx()-5;i=i+gap)
{
outtextxy(i,ymid-3,"|");
//itoa(i-xmid,str,10);
outtextxy(i,ymid+3,str);
}
for( i= ymid-gap;i>5;i=i-gap)
{
outtextxy(xmid,i,"-");
//itoa(ymid-i,str,10);
outtextxy(xmid+5,i,str);
}
for( i= xmid-gap;i>5;i=i-gap)
{
outtextxy(i,ymid-3,"|");
//itoa(-(xmid-i),str,10);
outtextxy(i-6,ymid+3,str);
}
for( i= ymid+gap;i<getmaxy()-5;i=i+gap)
{
outtextxy(xmid,i,"-");
//itoa(-(i-ymid),str,10);
outtextxy(xmid+8,i,str);
}
line(x1+xmid,ymid-y1,x2+xmid,ymid-y2);
line(x2+xmid,ymid-y2,x3+xmid,ymid-y3);
line(x3+xmid,ymid-y3,x1+xmid,ymid-y1);
printf("Enter the degree to rotaten");
scanf("%lf",&theta);
theta= ((float) theta *3.14f )/(float)180; // converting theta to radian
printf("Enter the arbitrary point to rotaten");
scanf("%d%d",&xr,&yr);
x1dash=xr+(x1-xr)*cos(theta)-(y1-yr)*sin(theta);
x2dash=xr+(x2-xr)*cos(theta)-(y2-yr)*sin(theta);
x3dash=xr+(x3-xr)*cos(theta)-(y3-yr)*sin(theta);
y1dash=yr+(x1-xr)*sin(theta)+(y1-yr)*cos(theta);
y2dash=yr+(x2-xr)*sin(theta)+(y2-yr)*cos(theta);
y3dash=yr+(x3-xr)*sin(theta)+(y3-yr)*cos(theta);
line(x1dash+xmid,ymid-y1dash,x2dash+xmid,ymid-y2dash);
line(x2dash+xmid,ymid-y2dash,x3dash+xmid,ymid-y3dash);
line(x3dash+xmid,ymid-y3dash,x1dash+xmid,ymid-y1dash);
getch();
closegraph();
}
Input:
Output:
Complete and copy them carefully, thanks you all……!!!

Computer Graphics and Multimedia lab report

  • 1.
    SECTION-A::::::::::::::::: PROGRAM-1:::::::::::::: Program Title: Write aprogram to implement DDA line drawing algorithm. Source Code: #include <windows.h> #include <gl/glut.h> void display(void) { glClear(GL_COLOR_BUFFER_BIT); glColor3f(1.0, 1.0, 1.0); glBegin(GL_POINTS); int x1 = 10, y1 = 10, x2 = 100, y2 = 100, x, y, xend, dx, dy; float m, b; dx = x2 -x1; dy = y2 -y1; m = dy / dx; b = y1 - m*x1; if(dx < 0) { x = x2; y = y2; xend = x1; } else { x = x1; y = y1; xend = x2; } while(x < xend){ glVertex2i(x,y); x = x+1; y = m * x + b; } glEnd(); glFlush(); }
  • 2.
    int main(int argc,char*argv[]) { glutInit(&argc,argv); glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); glutInitWindowSize (500, 500); glutInitWindowPosition (100, 100); glutCreateWindow ("points"); glClearColor(0,0,0,0.0); glMatrixMode (GL_PROJECTION); gluOrtho2D (0.0, 200.0, 0.0, 150.0); glutDisplayFunc(display); glutMainLoop(); return 0; } Output: PROGRAM-2:::::::::::::: Program Title: Write a program to implement Bresenhams line drawing algorithm. Source Code: #include <windows.h>
  • 3.
    #include <GL/glut.h> void display(void) { glClear(GL_COLOR_BUFFER_BIT); glColor3f(1.0,1.0, 1.0); glBegin(GL_POINTS); int x1 = 60, y1 = 40, x2 = 120, y2 = 120, x, y, xend, dx, dy, inc1, inc2, d; dx = x2 -x1; dy = y2 -y1; inc1 = 2 * dy; inc2 = 2 * (dy-dx); d = inc1 - dx; if(dx < 0) { x = x2; y = y2; xend = x1; } else { x = x1; y = y1; xend = x2; }
  • 4.
    glVertex2i(x,y); while(x <= xend){ if(d<0) { d= d + inc1; } else { d = d + inc2; y = y + 1; } x = x + 1; glVertex2i(x,y); } glEnd(); glFlush(); } int main(int argc,char *argv[]) { glutInit(&argc,argv); glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); glutInitWindowSize (500, 500); glutInitWindowPosition (100, 100); glutCreateWindow ("points");
  • 5.
    glClearColor (0,0,0,0.0); glMatrixMode (GL_PROJECTION); gluOrtho2D(0.0, 200.0, 0.0, 150.0); glutDisplayFunc(display); glutMainLoop(); return 0; } Output: PROGRAM-3:::::::::::::: Program Title: Write a program to implement Bresenhams circle drawing algorithm. Source Code: #include <windows.h> #include <GL/glut.h>
  • 6.
    #include<math.h> void display(void) { glClear(GL_COLOR_BUFFER_BIT); glColor3f(1.0, 1.0,1.0); glBegin(GL_POINTS); int x, y, r= 40, h = 100, k= 80, d; x = 0; y = r; d = 3 - (2*r); while(x < y){ glVertex2i(x+h, y+k); glVertex2i(y+h, x+k); glVertex2i(h-y, x+k); glVertex2i(h-x, y+k); glVertex2i(h-x, k-y); glVertex2i(h-y, k-x); glVertex2i(y+h, k-x);
  • 7.
    glVertex2i(x+h, k-y); if (d< 0){ d = d + (4 * x) + 6; x = x + 1; } else{ d = d + (4 * (x - y)) + 10; x = x + 1; y = y -1; } } glEnd(); glFlush(); } int main(int argc,char *argv[]) { glutInit(&argc,argv); glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); glutInitWindowSize (650, 500); glutInitWindowPosition (100, 100); glutCreateWindow ("points"); glClearColor(0,0,0,0.0);
  • 8.
    glMatrixMode (GL_PROJECTION); gluOrtho2D (0.0,200.0, 0.0, 150.0); glutDisplayFunc(display); glutMainLoop(); return 0; } Output: PROGRAM-4:::::::::::::: Program Title: Write a program to implement midpoint ellipse drawing algorithm. Source Code: #include<stdio.h> #include<conio.h> #include<graphics.h>
  • 9.
    void drawEllipse(float xc,float yc, float x, float y) { putpixel(xc+x, yc+y, RED); putpixel(xc-x, yc+y, RED); putpixel(xc+x, yc-y, RED); putpixel(xc-x, yc-y, RED); } void ellipseMidpoint(float xc, float yc, float rx, float ry) { float rxSq = rx * rx; float rySq = ry * ry; float x = 0, y = ry, p; float px = 0, py = 2 * rxSq * y; drawEllipse(xc, yc, x, y); p = rySq - (rxSq * ry) + (0.25 * rxSq); while (px < py) { x++; px = px + 2 * rySq; if (p < 0) p = p + rySq + px; else { y--; py = py - 2 * rxSq; p = p + rySq + px - py;
  • 10.
    } drawEllipse(xc, yc, x,y); } p = rySq*(x+0.5)*(x+0.5) + rxSq*(y-1)*(y-1) - rxSq*rySq; while (y > 0) { y--; py = py - 2 * rxSq; if (p > 0) p = p + rxSq - py; else { x++; px = px + 2 * rySq; p = p + rxSq - py + px; } drawEllipse(xc, yc, x, y); } } int main() { int xc, yc, rx, ry; int gd = DETECT, gm; initgraph(&gd, &gm, "c:tcbgi"); printf("tttMID POINT ELLIPSE DRAWING ALGORITHMnntEnter the center coordinates of ellipse:"); scanf("%d %d",&xc,&yc);
  • 11.
    printf("ntEnter x-radius coordinate:"); scanf("%d",&rx); printf("ntEntery-radius coordinate:"); scanf("%d",&ry); ellipseMidpoint(xc, yc, rx, ry); getch(); closegraph(); return 0; } Input:
  • 12.
    Output: PROGRAM-5:::::::::::::: Program Title: Write aprogram to draw a house. Source Code: #include<GL/glu.h> #include<GL/glut.h> #include<windows.h> void init(void) { glClearColor(0.0, 0.0, 0.0, 0.0); glMatrixMode(GL_PROJECTION); gluOrtho2D(0.0, 200.0, 0.0, 150.0); }
  • 13.
    void buildHouse(void) { glClear(GL_COLOR_BUFFER_BIT); glColor3f(1.0, 0.0,0.0); glBegin(GL_POLYGON); //start house glColor3f(1.0, 1.0, 0.0); glVertex2i(50, 0); glColor3f(0.0, 1.0, 0.0); glVertex2i(50, 100); glColor3f(0.0, 1.0, 1.0); glVertex2i(150, 100); glColor3f(1.0, 1.0, 0.0); glVertex2i(150,0); glEnd(); //end house glBegin(GL_POLYGON); //start window glColor3f(0.3, 0.2, 0.0); glVertex2i(60, 80); glVertex2i(80, 80); glVertex2i(80, 65); glVertex2i(60, 65); glEnd(); //end window glBegin(GL_POLYGON); //start window glColor3f(0.3, 0.2, 0.0);
  • 14.
    glVertex2i(120, 80); glVertex2i(140, 80); glVertex2i(140,65); glVertex2i(120, 65); glEnd(); //end window glBegin(GL_POLYGON); //start ceiling glColor3f(0.8, 0.0, 0.0); glVertex2i(40, 100); glColor3f(0.5, 0.0, 0.3); glVertex2i(160, 100); glColor3f(1.0, 0.0, 0.0); glVertex2i(100, 130); glEnd(); //end ceiling glBegin(GL_POLYGON); //start door glColor3f(0.3, 0.2, 0.0); glVertex2i(80, 0); glVertex2i(80, 50); glVertex2i(120, 50); glVertex2i(120,0); glEnd(); //end door glFlush(); } int main(int argc, char** argv)
  • 15.
    { glutInit(&argc, argv); glutInitDisplayMode(GLUT_SINGLE |GLUT_RGB); glutInitWindowPosition(50, 100); glutInitWindowSize(500, 500); glutCreateWindow("House Section OpenGL"); init(); glutDisplayFunc(buildHouse); glutMainLoop(); return 0; } Output: Hay listen carefully, don’t draw this home with your own color, just use pencil.
  • 16.
    SECTION-B::::::::::::::::: PROGRAM-1:::::::::::::: Program Title: Write aprogram to implement Cohen Sutherland Line Clipping algorithm. Source Code: #include<stdio.h> #include<stdlib.h> #include<math.h> #include<graphics.h> #include<dos.h> typedef struct coordinate { int x,y; char code[4]; }PT; void drawwindow(); void drawline(PT p1,PT p2); PT setcode(PT p); int visibility(PT p1,PT p2); PT resetendpt(PT p1,PT p2); int main() { int gd=DETECT,v,gm; PT p1,p2,p3,p4,ptemp;
  • 17.
    printf("nEnter x1 andy1n"); scanf("%d %d",&p1.x,&p1.y); printf("nEnter x2 and y2n"); scanf("%d %d",&p2.x,&p2.y); initgraph(&gd,&gm,"c:turboc3bgi"); drawwindow(); delay(500); drawline(p1,p2); delay(500); cleardevice(); delay(500); p1=setcode(p1); p2=setcode(p2); v=visibility(p1,p2); delay(500); switch(v) { case 0: drawwindow(); delay(500); drawline(p1,p2); break; case 1: drawwindow();
  • 18.
    delay(500); break; case 2: p3=resetendpt(p1,p2); p4=resetendpt(p2,p1); drawwindow(); delay(500); drawline(p3,p4); break; } delay(5000); closegraph(); } voiddrawwindow() { line(150,100,450,100); line(450,100,450,350); line(450,350,150,350); line(150,350,150,100); } void drawline(PT p1,PT p2) { line(p1.x,p1.y,p2.x,p2.y); }
  • 19.
    PT setcode(PT p)//for setting the 4 bit code { PT ptemp; if(p.y<100) ptemp.code[0]='1'; //Top else ptemp.code[0]='0'; if(p.y>350) ptemp.code[1]='1'; //Bottom else ptemp.code[1]='0'; if(p.x>450) ptemp.code[2]='1'; //Right else ptemp.code[2]='0'; if(p.x<150) ptemp.code[3]='1'; //Left else ptemp.code[3]='0'; ptemp.x=p.x; ptemp.y=p.y;
  • 20.
    return(ptemp); } int visibility(PT p1,PTp2) { int i,flag=0; for(i=0;i<4;i++) { if((p1.code[i]!='0') || (p2.code[i]!='0')) flag=1; } if(flag==0) return(0); for(i=0;i<4;i++) { if((p1.code[i]==p2.code[i]) && (p1.code[i]=='1')) flag='0'; } if(flag==0) return(1); return(2); }
  • 21.
    PT resetendpt(PT p1,PTp2) { PT temp; int x,y,i; float m,k; if(p1.code[3]=='1') x=150; if(p1.code[2]=='1') x=450; if((p1.code[3]=='1') || (p1.code[2]=='1')) { m=(float)(p2.y-p1.y)/(p2.x-p1.x); k=(p1.y+(m*(x-p1.x))); temp.y=k; temp.x=x; for(i=0;i<4;i++) temp.code[i]=p1.code[i]; if(temp.y<=350 && temp.y>=100) return (temp); }
  • 22.
  • 23.
  • 24.
    PROGRAM-2:::::::::::::: Program Title: Write aprogram to implement Sutherland Hodgeman Polygon Clipping algorithm Source Code: #include<iostream> #include<windows.h> #include<graphics.h> #include<conio.h> #include<stdlib.h> using namespace std; #define round(a) ((int)(a+0.5)) int k; float xmin,ymin,xmax,ymax,arr[20],m; void clipl(float x1,float y1,float x2,float y2) { if(x2-x1) m=(y2-y1)/(x2-x1); else m=100000; if(x1 >= xmin && x2 >= xmin) { arr[k]=x2; arr[k+1]=y2; k+=2; } if(x1 < xmin && x2 >= xmin) { arr[k]=xmin; arr[k+1]=y1+m*(xmin-x1); arr[k+2]=x2; arr[k+3]=y2; k+=4; } if(x1 >= xmin && x2 < xmin) { arr[k]=xmin; arr[k+1]=y1+m*(xmin-x1);
  • 25.
    k+=2; } } void clipt(float x1,floaty1,float x2,float y2) { if(y2-y1) m=(x2-x1)/(y2-y1); else m=100000; if(y1 <= ymax && y2 <= ymax) { arr[k]=x2; arr[k+1]=y2; k+=2; } if(y1 > ymax && y2 <= ymax) { arr[k]=x1+m*(ymax-y1); arr[k+1]=ymax; arr[k+2]=x2; arr[k+3]=y2; k+=4; } if(y1 <= ymax && y2 > ymax) { arr[k]=x1+m*(ymax-y1); arr[k+1]=ymax; k+=2; } } void clipr(float x1,float y1,float x2,float y2) { if(x2-x1) m=(y2-y1)/(x2-x1); else m=100000; if(x1 <= xmax && x2 <= xmax) { arr[k]=x2; arr[k+1]=y2;
  • 26.
    k+=2; } if(x1 > xmax&& x2 <= xmax) { arr[k]=xmax; arr[k+1]=y1+m*(xmax-x1); arr[k+2]=x2; arr[k+3]=y2; k+=4; } if(x1 <= xmax && x2 > xmax) { arr[k]=xmax; arr[k+1]=y1+m*(xmax-x1); k+=2; } } void clipb(float x1,float y1,float x2,float y2) { if(y2-y1) m=(x2-x1)/(y2-y1); else m=100000; if(y1 >= ymin && y2 >= ymin) { arr[k]=x2; arr[k+1]=y2; k+=2; } if(y1 < ymin && y2 >= ymin) { arr[k]=x1+m*(ymin-y1); arr[k+1]=ymin; arr[k+2]=x2; arr[k+3]=y2; k+=4; } if(y1 >= ymin && y2 < ymin) { arr[k]=x1+m*(ymin-y1); arr[k+1]=ymin;
  • 27.
    k+=2; } } int main() { int gdriver=DETECT,gmode,n,poly[20],i; float xi,yi,xf,yf,polyy[20]; //clrscr(); system("cls"); cout<<"Coordinates of rectangular clip window :nxmin,ymin :"; cin>>xmin>>ymin; cout<<"xmax,ymax :"; cin>>xmax>>ymax; cout<<"nnPolygon to be clipped :nNumber of sides :"; cin>>n; cout<<"Enter the coordinates :"; for(i=0;i < 2*n;i++) cin>>polyy[i]; polyy[i]=polyy[0]; polyy[i+1]=polyy[1]; for(i=0;i < 2*n+2;i++) poly[i]=round(polyy[i]); initgraph(&gdriver,&gmode,"C:TCBGI"); setcolor(RED); rectangle(xmin,ymax,xmax,ymin); cout<<"ttUNCLIPPED POLYGON"; setcolor(WHITE); fillpoly(n,poly); getch(); cleardevice(); k=0; for(i=0;i < 2*n;i+=2) clipl(polyy[i],polyy[i+1],polyy[i+2],polyy[i+3]); n=k/2; for(i=0;i < k;i++) polyy[i]=arr[i]; polyy[i]=polyy[0]; polyy[i+1]=polyy[1]; k=0; for(i=0;i < 2*n;i+=2) clipt(polyy[i],polyy[i+1],polyy[i+2],polyy[i+3]);
  • 28.
    n=k/2; for(i=0;i < k;i++) polyy[i]=arr[i]; polyy[i]=polyy[0]; polyy[i+1]=polyy[1]; k=0; for(i=0;i< 2*n;i+=2) clipr(polyy[i],polyy[i+1],polyy[i+2],polyy[i+3]); n=k/2; for(i=0;i < k;i++) polyy[i]=arr[i]; polyy[i]=polyy[0]; polyy[i+1]=polyy[1]; k=0; for(i=0;i < 2*n;i+=2) clipb(polyy[i],polyy[i+1],polyy[i+2],polyy[i+3]); for(i=0;i < k;i++) poly[i]=round(arr[i]); if(k) fillpoly(k/2,poly); setcolor(RED); rectangle(xmin,ymax,xmax,ymin); cout<<"tCLIPPED POLYGON"; getch(); closegraph(); return 0; } Input:
  • 29.
    Output: PROGRAM-3:::::::::::::: Program Title: Write aprogram to implement Window to Viewport transformation. Source Code: #include<stdio.h> #include<conio.h> #include<dos.h> #include<graphics.h> #include<math.h> int main() { int xwmin,ywmin,xwmax,ywmax,xv1,yv1; int xvmin,xvmax,yvmin,yvmax,xw,yw,xv,yv; int gd=DETECT,gm; initgraph(&gd,&gm,""); printf("Enter the window coordinates xwmin,xwmax,ywmin,ywmaxn"); scanf("%dt%dt%dt%d",&xwmin,&xwmax,&ywmin,&ywmax); line(xwmin-25,xwmin-25,xwmin-25,ywmax+50); line(xwmin-40,ywmax+25,xwmax+50,ywmax+25); outtextxy(xwmin+5,ywmax+5,"Window"); line(xwmin,ywmin,xwmin,ywmax); line(xwmin,ywmax,xwmax,ywmax);
  • 30.
  • 31.
    Output: PROGRAM-4:::::::::::::: Program Title: Write aprogram to implement the following 2D transformations:- a) Translation b) Scaling c) Rotation Source Code: #include <graphics.h> #include <stdlib.h> #include <stdio.h> #include <conio.h> #include<math.h> int main() { int gm; int gd=DETECT; int x1,x2,x3,y1,y2,y3,nx1,nx2,nx3,ny1,ny2,ny3,c; int sx,sy,xt,yt,r;
  • 32.
    float t; initgraph(&gd,&gm,"c:tcbg:"); printf("t Programfor basic transactions"); printf("nt Enter the points of triangle"); setcolor(1); scanf("%d%d%d%d%d%d",&x1,&y1,&x2,&y2,&x3,&y3); line(x1,y1,x2,y2); line(x2,y2,x3,y3); line(x3,y3,x1,y1); getch(); printf("n 1.Transactionn 2.Rotationn 3.Scallingn 4.exit"); printf("Enter your choice:"); scanf("%d",&c); switch(c) { case 1: printf("n Enter the translation factor"); scanf("%d%d",&xt,&yt); 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);
  • 33.
    getch(); case 2: printf("n Enterthe 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);
  • 34.
  • 35.
    Output: PROGRAM-5:::::::::::::: Program Title: Write aprogram to draw a triangle and rotate about the pivot point. Source Code: #include<graphics.h> #include<conio.h> #include<stdio.h> #include<math.h> int main() { int gd=DETECT,gm; int i,xmid,ymid,x1,y1,x2,y2,x3,y3,x,y,dy,dx,p,gap=50,temp,xr,yr; int x1dash,y1dash,x2dash,y2dash,x3dash,y3dash; float m; double theta; char str[5]; //clrscr(); initgraph(&gd,&gm,"..bgi"); printf("Enter first co-ords of the trianglen");
  • 36.
    scanf("%d %d",&x1,&y1); printf("Enter secondco-ords of the trianglen"); scanf("%d %d",&x2,&y2); printf("Enter third co-ords of the trianglen"); scanf("%d %d",&x3,&y3); xmid= getmaxx()/2; ymid= getmaxy()/2; line(5,ymid,getmaxx()-5,ymid); line(xmid+3,5,xmid+3,getmaxy()-5); for( i= xmid+gap;i<getmaxx()-5;i=i+gap) { outtextxy(i,ymid-3,"|"); //itoa(i-xmid,str,10); outtextxy(i,ymid+3,str); } for( i= ymid-gap;i>5;i=i-gap) { outtextxy(xmid,i,"-"); //itoa(ymid-i,str,10); outtextxy(xmid+5,i,str); } for( i= xmid-gap;i>5;i=i-gap) { outtextxy(i,ymid-3,"|"); //itoa(-(xmid-i),str,10); outtextxy(i-6,ymid+3,str); } for( i= ymid+gap;i<getmaxy()-5;i=i+gap) { outtextxy(xmid,i,"-"); //itoa(-(i-ymid),str,10); outtextxy(xmid+8,i,str); } line(x1+xmid,ymid-y1,x2+xmid,ymid-y2); line(x2+xmid,ymid-y2,x3+xmid,ymid-y3); line(x3+xmid,ymid-y3,x1+xmid,ymid-y1); printf("Enter the degree to rotaten");
  • 37.
    scanf("%lf",&theta); theta= ((float) theta*3.14f )/(float)180; // converting theta to radian printf("Enter the arbitrary point to rotaten"); scanf("%d%d",&xr,&yr); x1dash=xr+(x1-xr)*cos(theta)-(y1-yr)*sin(theta); x2dash=xr+(x2-xr)*cos(theta)-(y2-yr)*sin(theta); x3dash=xr+(x3-xr)*cos(theta)-(y3-yr)*sin(theta); y1dash=yr+(x1-xr)*sin(theta)+(y1-yr)*cos(theta); y2dash=yr+(x2-xr)*sin(theta)+(y2-yr)*cos(theta); y3dash=yr+(x3-xr)*sin(theta)+(y3-yr)*cos(theta); line(x1dash+xmid,ymid-y1dash,x2dash+xmid,ymid-y2dash); line(x2dash+xmid,ymid-y2dash,x3dash+xmid,ymid-y3dash); line(x3dash+xmid,ymid-y3dash,x1dash+xmid,ymid-y1dash); getch(); closegraph(); } Input:
  • 38.
    Output: Complete and copythem carefully, thanks you all……!!!