SlideShare a Scribd company logo
1 of 38
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……!!!

More Related Content

What's hot

Computer graphics lab assignment
Computer graphics lab assignmentComputer graphics lab assignment
Computer graphics lab assignmentAbdullah Al Shiam
 
12 Elliptic Curves
12 Elliptic Curves12 Elliptic Curves
12 Elliptic CurvesSam Bowne
 
OpenGL Shading Language
OpenGL Shading LanguageOpenGL Shading Language
OpenGL Shading LanguageJungsoo Nam
 
Transformations computer graphics
Transformations computer graphics Transformations computer graphics
Transformations computer graphics Vikram Halder
 
Steganography and watermarking
Steganography and watermarkingSteganography and watermarking
Steganography and watermarkingsudip nandi
 
Graphics practical lab manual
Graphics practical lab manualGraphics practical lab manual
Graphics practical lab manualVivek Kumar Sinha
 
Elliptic Curve Cryptography
Elliptic Curve CryptographyElliptic Curve Cryptography
Elliptic Curve CryptographyJorgeVillamarin5
 
Steganography
SteganographySteganography
SteganographyPREMKUMAR
 
Threat Hunting Workshop
Threat Hunting WorkshopThreat Hunting Workshop
Threat Hunting WorkshopSplunk
 
Homomorphic Encryption
Homomorphic EncryptionHomomorphic Encryption
Homomorphic EncryptionVictor Pereira
 
Game object models - Game Engine Architecture
Game object models - Game Engine ArchitectureGame object models - Game Engine Architecture
Game object models - Game Engine ArchitectureShawn Presser
 
ECMAScript 6 from an Attacker's Perspective - Breaking Frameworks, Sandboxes,...
ECMAScript 6 from an Attacker's Perspective - Breaking Frameworks, Sandboxes,...ECMAScript 6 from an Attacker's Perspective - Breaking Frameworks, Sandboxes,...
ECMAScript 6 from an Attacker's Perspective - Breaking Frameworks, Sandboxes,...Mario Heiderich
 
Secure Socket Layer (SSL)
Secure Socket Layer (SSL)Secure Socket Layer (SSL)
Secure Socket Layer (SSL)amanchaurasia
 
Computer graphics practical(jainam)
Computer graphics practical(jainam)Computer graphics practical(jainam)
Computer graphics practical(jainam)JAINAM KAPADIYA
 
Data Encryption and Decryption using Hill Cipher
Data Encryption and Decryption using Hill CipherData Encryption and Decryption using Hill Cipher
Data Encryption and Decryption using Hill CipherAashirwad Kashyap
 
Mathematics Towards Elliptic Curve Cryptography-by Dr. R.Srinivasan
Mathematics Towards Elliptic Curve Cryptography-by Dr. R.SrinivasanMathematics Towards Elliptic Curve Cryptography-by Dr. R.Srinivasan
Mathematics Towards Elliptic Curve Cryptography-by Dr. R.Srinivasanmunicsaa
 

What's hot (20)

Computer graphics lab assignment
Computer graphics lab assignmentComputer graphics lab assignment
Computer graphics lab assignment
 
12 Elliptic Curves
12 Elliptic Curves12 Elliptic Curves
12 Elliptic Curves
 
OpenGL Shading Language
OpenGL Shading LanguageOpenGL Shading Language
OpenGL Shading Language
 
Transformations computer graphics
Transformations computer graphics Transformations computer graphics
Transformations computer graphics
 
Steganography and watermarking
Steganography and watermarkingSteganography and watermarking
Steganography and watermarking
 
Homomorphic encryption
Homomorphic encryptionHomomorphic encryption
Homomorphic encryption
 
Graphics practical lab manual
Graphics practical lab manualGraphics practical lab manual
Graphics practical lab manual
 
Elliptic Curve Cryptography
Elliptic Curve CryptographyElliptic Curve Cryptography
Elliptic Curve Cryptography
 
Introduction to graphics programming in c
Introduction to graphics programming in cIntroduction to graphics programming in c
Introduction to graphics programming in c
 
Steganography
SteganographySteganography
Steganography
 
Computer graphics
Computer graphics   Computer graphics
Computer graphics
 
Threat Hunting Workshop
Threat Hunting WorkshopThreat Hunting Workshop
Threat Hunting Workshop
 
Homomorphic Encryption
Homomorphic EncryptionHomomorphic Encryption
Homomorphic Encryption
 
Game object models - Game Engine Architecture
Game object models - Game Engine ArchitectureGame object models - Game Engine Architecture
Game object models - Game Engine Architecture
 
ECMAScript 6 from an Attacker's Perspective - Breaking Frameworks, Sandboxes,...
ECMAScript 6 from an Attacker's Perspective - Breaking Frameworks, Sandboxes,...ECMAScript 6 from an Attacker's Perspective - Breaking Frameworks, Sandboxes,...
ECMAScript 6 from an Attacker's Perspective - Breaking Frameworks, Sandboxes,...
 
Secure Socket Layer (SSL)
Secure Socket Layer (SSL)Secure Socket Layer (SSL)
Secure Socket Layer (SSL)
 
Computer graphics practical(jainam)
Computer graphics practical(jainam)Computer graphics practical(jainam)
Computer graphics practical(jainam)
 
Rsa
RsaRsa
Rsa
 
Data Encryption and Decryption using Hill Cipher
Data Encryption and Decryption using Hill CipherData Encryption and Decryption using Hill Cipher
Data Encryption and Decryption using Hill Cipher
 
Mathematics Towards Elliptic Curve Cryptography-by Dr. R.Srinivasan
Mathematics Towards Elliptic Curve Cryptography-by Dr. R.SrinivasanMathematics Towards Elliptic Curve Cryptography-by Dr. R.Srinivasan
Mathematics Towards Elliptic Curve Cryptography-by Dr. R.Srinivasan
 

Similar to Here are the steps to implement Sutherland-Hodgman polygon clipping algorithm:1. Take the subject polygon vertices as input and store them in an array. 2. Also take the clipping window coordinates (xmin, ymin, xmax, ymax) as input.3. Initialize an output array to store the clipped polygon vertices. 4. For each edge of the subject polygon: 4.1. Call the clipping functions clipl(), clipt(), clipr(), clipb() to clip the edge against left, top, right and bottom clipping lines respectively. 4.2. These functions will calculate the intersection points of the edge with the clipping lines and add them to the output array if the

Tutorial Open GL (Listing Code)
Tutorial Open GL (Listing Code)Tutorial Open GL (Listing Code)
Tutorial Open GL (Listing Code)Aila Gema Safitri
 
computer graphics at openGL
computer graphics at openGLcomputer graphics at openGL
computer graphics at openGLYasir Khan
 
01.Opengl_intro-2.ppt
01.Opengl_intro-2.ppt01.Opengl_intro-2.ppt
01.Opengl_intro-2.pptEngrZamaan
 
Intro to Computer Graphics.ppt
Intro to Computer Graphics.pptIntro to Computer Graphics.ppt
Intro to Computer Graphics.pptadil104135
 
Programa de objetos 3 d wire
Programa de objetos 3 d wirePrograma de objetos 3 d wire
Programa de objetos 3 d wireRené Domínguez
 
FLTK Summer Course - Part VIII - Eighth Impact - Exercises
FLTK Summer Course - Part VIII - Eighth Impact - ExercisesFLTK Summer Course - Part VIII - Eighth Impact - Exercises
FLTK Summer Course - Part VIII - Eighth Impact - ExercisesMichel Alves
 
square.cpp Open
 square.cpp Open square.cpp Open
square.cpp OpenMikeEly930
 
bfd23fd7-0d89-45c0-8b82-c991b30ed375.pdf
bfd23fd7-0d89-45c0-8b82-c991b30ed375.pdfbfd23fd7-0d89-45c0-8b82-c991b30ed375.pdf
bfd23fd7-0d89-45c0-8b82-c991b30ed375.pdfshehabhamad_90
 
Ass day2 1_checkerboard...copy in cpp
Ass day2 1_checkerboard...copy in cppAss day2 1_checkerboard...copy in cpp
Ass day2 1_checkerboard...copy in cppRobi Parvez
 
Robot In OpenGL Using Line Function
Robot In OpenGL Using Line Function Robot In OpenGL Using Line Function
Robot In OpenGL Using Line Function Jannat Jamshed
 
Robot In OpenGL Using Line Function
Robot In OpenGL Using Line Function Robot In OpenGL Using Line Function
Robot In OpenGL Using Line Function Jannat Jamshed
 
The Ring programming language version 1.5.3 book - Part 65 of 184
The Ring programming language version 1.5.3 book - Part 65 of 184The Ring programming language version 1.5.3 book - Part 65 of 184
The Ring programming language version 1.5.3 book - Part 65 of 184Mahmoud Samir Fayed
 
computer graphics at openGL (2)
computer graphics at openGL (2)computer graphics at openGL (2)
computer graphics at openGL (2)Yasir Khan
 
The Ring programming language version 1.9 book - Part 65 of 210
The Ring programming language version 1.9 book - Part 65 of 210The Ring programming language version 1.9 book - Part 65 of 210
The Ring programming language version 1.9 book - Part 65 of 210Mahmoud Samir Fayed
 
The Ring programming language version 1.7 book - Part 63 of 196
The Ring programming language version 1.7 book - Part 63 of 196The Ring programming language version 1.7 book - Part 63 of 196
The Ring programming language version 1.7 book - Part 63 of 196Mahmoud Samir Fayed
 

Similar to Here are the steps to implement Sutherland-Hodgman polygon clipping algorithm:1. Take the subject polygon vertices as input and store them in an array. 2. Also take the clipping window coordinates (xmin, ymin, xmax, ymax) as input.3. Initialize an output array to store the clipped polygon vertices. 4. For each edge of the subject polygon: 4.1. Call the clipping functions clipl(), clipt(), clipr(), clipb() to clip the edge against left, top, right and bottom clipping lines respectively. 4.2. These functions will calculate the intersection points of the edge with the clipping lines and add them to the output array if the (20)

Tutorial Open GL (Listing Code)
Tutorial Open GL (Listing Code)Tutorial Open GL (Listing Code)
Tutorial Open GL (Listing Code)
 
computer graphics at openGL
computer graphics at openGLcomputer graphics at openGL
computer graphics at openGL
 
01.Opengl_intro-2.ppt
01.Opengl_intro-2.ppt01.Opengl_intro-2.ppt
01.Opengl_intro-2.ppt
 
opengl.ppt
opengl.pptopengl.ppt
opengl.ppt
 
Intro to Computer Graphics.ppt
Intro to Computer Graphics.pptIntro to Computer Graphics.ppt
Intro to Computer Graphics.ppt
 
Programa de objetos 3 d wire
Programa de objetos 3 d wirePrograma de objetos 3 d wire
Programa de objetos 3 d wire
 
FLTK Summer Course - Part VIII - Eighth Impact - Exercises
FLTK Summer Course - Part VIII - Eighth Impact - ExercisesFLTK Summer Course - Part VIII - Eighth Impact - Exercises
FLTK Summer Course - Part VIII - Eighth Impact - Exercises
 
Open gl polygon code review
Open gl polygon code reviewOpen gl polygon code review
Open gl polygon code review
 
square.cpp Open
 square.cpp Open square.cpp Open
square.cpp Open
 
bfd23fd7-0d89-45c0-8b82-c991b30ed375.pdf
bfd23fd7-0d89-45c0-8b82-c991b30ed375.pdfbfd23fd7-0d89-45c0-8b82-c991b30ed375.pdf
bfd23fd7-0d89-45c0-8b82-c991b30ed375.pdf
 
Ass day2 1_checkerboard...copy in cpp
Ass day2 1_checkerboard...copy in cppAss day2 1_checkerboard...copy in cpp
Ass day2 1_checkerboard...copy in cpp
 
Robot In OpenGL Using Line Function
Robot In OpenGL Using Line Function Robot In OpenGL Using Line Function
Robot In OpenGL Using Line Function
 
Robot In OpenGL Using Line Function
Robot In OpenGL Using Line Function Robot In OpenGL Using Line Function
Robot In OpenGL Using Line Function
 
Robot by gulnaz
Robot by gulnazRobot by gulnaz
Robot by gulnaz
 
Robot by gulnaz
Robot by gulnazRobot by gulnaz
Robot by gulnaz
 
The Ring programming language version 1.5.3 book - Part 65 of 184
The Ring programming language version 1.5.3 book - Part 65 of 184The Ring programming language version 1.5.3 book - Part 65 of 184
The Ring programming language version 1.5.3 book - Part 65 of 184
 
computer graphics at openGL (2)
computer graphics at openGL (2)computer graphics at openGL (2)
computer graphics at openGL (2)
 
Development with OpenGL and Qt
Development with OpenGL and QtDevelopment with OpenGL and Qt
Development with OpenGL and Qt
 
The Ring programming language version 1.9 book - Part 65 of 210
The Ring programming language version 1.9 book - Part 65 of 210The Ring programming language version 1.9 book - Part 65 of 210
The Ring programming language version 1.9 book - Part 65 of 210
 
The Ring programming language version 1.7 book - Part 63 of 196
The Ring programming language version 1.7 book - Part 63 of 196The Ring programming language version 1.7 book - Part 63 of 196
The Ring programming language version 1.7 book - Part 63 of 196
 

Recently uploaded

Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppCeline George
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfUmakantAnnand
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsKarinaGenton
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 

Recently uploaded (20)

TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website App
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.Compdf
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its Characteristics
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 

Here are the steps to implement Sutherland-Hodgman polygon clipping algorithm:1. Take the subject polygon vertices as input and store them in an array. 2. Also take the clipping window coordinates (xmin, ymin, xmax, ymax) as input.3. Initialize an output array to store the clipped polygon vertices. 4. For each edge of the subject polygon: 4.1. Call the clipping functions clipl(), clipt(), clipr(), clipb() to clip the edge against left, top, right and bottom clipping lines respectively. 4.2. These functions will calculate the intersection points of the edge with the clipping lines and add them to the output array if the

  • 1. 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(); }
  • 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("ntEnter y-radius coordinate:"); scanf("%d",&ry); ellipseMidpoint(xc, yc, rx, ry); getch(); closegraph(); return 0; } Input:
  • 12. 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); }
  • 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 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;
  • 17. 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();
  • 18. 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); }
  • 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,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); }
  • 21. 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); }
  • 24. 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);
  • 25. 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;
  • 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 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);
  • 31. 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;
  • 32. 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);
  • 33. 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);
  • 35. 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");
  • 36. 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");
  • 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 copy them carefully, thanks you all……!!!