0% found this document useful (0 votes)
198 views61 pages

Complete Comp - Graphics File

The document contains code snippets and explanations from an expert on computer graphics (Navneet Sinha) regarding implementing various 2D graphics algorithms in C/C++ using graphics.h. The algorithms discussed include drawing lines, circles, ellipses using midpoint and Bresenham's algorithms, rotating and shearing 2D objects. For each concept, code snippets are provided along with sample inputs/outputs. The expert provides the code, explains what it does and shows example outputs to help learners understand how to implement these basic 2D graphics algorithms.

Uploaded by

api-19769356
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
Download as doc, pdf, or txt
0% found this document useful (0 votes)
198 views61 pages

Complete Comp - Graphics File

The document contains code snippets and explanations from an expert on computer graphics (Navneet Sinha) regarding implementing various 2D graphics algorithms in C/C++ using graphics.h. The algorithms discussed include drawing lines, circles, ellipses using midpoint and Bresenham's algorithms, rotating and shearing 2D objects. For each concept, code snippets are provided along with sample inputs/outputs. The expert provides the code, explains what it does and shows example outputs to help learners understand how to implement these basic 2D graphics algorithms.

Uploaded by

api-19769356
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1/ 61

August 04 ,2008

Write a program to display the maximum value at x-axis and y-axis.


#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\tc\\bgi");
int a,b;
clrscr();
a=getmaxx();
b=getmaxy();
printf("a=%d",a);
printf("\nb=%d",b); b
getch();
}
OUTPUT:

Navneet Sinha
August 11 , 2008

Write a program to divide the screen into four equivalent quadrants by using
graphics.

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
void main()
{
int a,b;
int gd = DETECT, gm;
initgraph(&gd, &gm, “c:\\tc\\bgi”);
a = getmaxx();
b = getmaxy();
line(a/2, 0, a/2, b);
line(0, b/2, a, b/2);
getch();
}

Navneet Sinha
August 11 ,2008

OUTPUT:

Navneet Sinha
August 18 ,2008

Write a program to implement a Digital Differential Analysis algorithm (DDA)


for drawing a line.

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
void main()
{
float x1, x2, y1, y2, steps, dx, dy, x, y;
xin, yin, I;
int gd = DETECT, gm;
initgraph(&gd, &gm, “c:\\tc\\bgi”);
printf(“Enter the value of x1 and y1”);
scanf(“%d%d”, &x1, &y1);
printf(“Enter the value of x2 and y2”);
scanf(“%d%d”, &x2, &y2);
dx = x2-x1;
dy = y2-y1;
if(dx>=dy)
steps = dx;
else
steps = dy;
xin = dx/steps;
yin = dy/steps;
x = x1;
y = y1;
for(I=0; I<steps; I++)
{
putpixel(x, y, 78);
x = x+xin;
y = y+yin;
}
getch();
}

Navneet Sinha
August 18 ,2008

OUTPUT:

August 25 ,2008

Navneet Sinha
Write a program to implement Bresenhem’s algorithm for drawing a line.

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
void main()
{
int x1, x2, y1, y2, dx, dy, x, y, p;
int gd = DETECT, gm;
initgraph(&gd, &gm, “c:\\tc\\bgi”);
printf(“Enter the value of x1 and y1”);
scanf(“%d%d”, &x1, &y1);
printf(“Enter the value of x2 and y2”);
scanf(“%d%d”, &x2, &y2);
dx = x2-x1;
dy = y2-y1;
p = 2*dy-dx;
x = x1;
y = y1;
while(x<=x2)
{
if(p<=0)
p = p + 2*dy;
else
{
y++;
p = p + 2*dy – 2*dx;
}
x++;
putpixel(x, y, 1);
}
getch();
}

August 25 ,2008

Navneet Sinha
OUTPUT:

September 1 ,2008

Navneet Sinha
Write a program to implement Bresenhem’s circle algorithm.

#include<conio.h>
#include<stdio.h>
#include<graphics.h>
void circlepoint(int,int,int,int);
void main()
{
int x=0,r,y,x1,y1,p,;
int gdrive=DETECT,gm;
initgraph(&gdrive,&gm,"c:\\tc\\bgi");
clrscr();
printf("enter the value of x1 and y1”);
scanf("%d%d",&x1,&y1);
printf("enter the radius");
scanf("%d",&r);
y=r;
d = 2-3r;
while(x<=y)
{
circlepoint(x,y,x1,y1);
if(d<0)
{
d=d+4*x+6;
}
else
{
d=d+4 *(x-y)+10;
y=y-1;
}
x++;
}
getch();
}

September 1 ,2008

Navneet Sinha
void circlepoint(int x ,int y,int x1,int y1 )
{
putpixel(x+x1,y+y1,1);
putpixel(y+x1,x+y1,1 );
putpixel(-y+x1,x+y1,1);
putpixel(-x+x1,y+y1,1);
putpixel(-x+x1,-y+y1,1);
putpixel(-y+x1,-x+y1,1);
putpixel(y+x1,-x+y1,1);
putpixel(x+x1,-y+y1,1);
}

OUTPUT:

September 8 ,2008

Navneet Sinha
Write a program to implement Mid Point circle algorithm.

#include<conio.h>
#include<stdio.h>
#include<graphics.h>
void circlepoint(int,int,int,int);
void main()
{
int x=0,r,y,x1,y1,p;
int gdrive=DETECT,gm;
initgraph(&gdrive,&gm,"c:\\tc\\bgi");
clrscr();
printf("enter the value of x and y");
scanf("%d%d",&x1,&y1);
printf("enter the radius");
scanf("%d",&r);
y=r;
p=1-r;
while(x<=y)
{
circlepoint(x,y,x1,y1);
if(p<=0)
p=p+(2*x)+3;
else
{
p=p+2*(x-y)+5;
y=y-1;
}
x=x+1;
}
getch();
}

September 8 ,2008

void circlepoint(int x ,int y,int x1,int y1 )


Navneet Sinha
{
putpixel(x+x1,y+y1,1);
putpixel(y+x1,x+y1,1 );
putpixel(-y+x1,x+y1,1);
putpixel(-x+x1,y+y1,1);
putpixel(-x+x1,-y+y1,1);
putpixel(-y+x1,-x+y1,1);
putpixel(y+x1,-x+y1,1);
putpixel(x+x1,-y+y1,1);
}

September 8 ,2008

Navneet Sinha
OUTPUT:

Navneet Sinha
September 15, 2008

Write a program to implement the Mid Point ellipse algorithm.

#include<stdio.h>
#include<graphics.h>
#include<conio.h>
void pp(int xx,int yy);
void epoints(float xc,float yc,float x,float y)
{
pp((int)(xc+x+getmaxx()/2),(int)(-yc-y+getmaxy()/2));
pp((int)(xc-x+getmaxx()/2),(int)(-yc+y+getmaxy()/2));
pp((int)(xc+x+getmaxx()/2),(int)(-yc+y+getmaxy()/2));
pp((int)(xc-x+getmaxx()/2),(int)(-yc-y+getmaxy()/2));
}
void pp(int xx, int yy)
{
putpixel(xx,yy,7300);
}
void mdpt(float xc,float yc,float rx,float ry)
{
float x=0,y=ry,p1,p2,t1,t2;
int k;
p1=ry*ry-rx*rx*ry+0.25*rx*rx;
epoints(xc,yc,x,y);
for(k=0;(2*ry*ry*x)<=(2*rx*rx*y);k++)
{
t1=2*ry*ry*x+2*ry*ry;
t2=2*rx*rx*y-2*rx*rx;
if(p1<0)
p1=p1+t1+ry*ry;
else
{
p1=p1+t1-t2+ry*ry;
y--;
}
x++;
epoints(xc,yc,x,y);
}

September 15, 2008

Navneet Sinha
p2=ry*ry*(x+.5)*(x+.5)+rx*rx*(y-1)*(y-1)-rx*rx*ry*ry;
epoints(xc,yc,x,y);
for(k=0;y>=0;k++)
{
t1=2*ry*ry*x+2*ry*ry;
t2=2*rx*rx*y-2*rx*rx;
if(p2>0)
p2=p2-t2+rx*rx;
else
{
p2=p2+t1-t2+rx*rx;
x++;
}
y--;
epoints(xc,yc,x,y);

}
}
void main()
{
int gd=DETECT,gm;
float xc,yc,rx,ry;
initgraph(&gd,&gm, "c:\\tc\\bgi");
printf("Enter center coordinate xc : ");
scanf("%f",&xc);
printf("Enter center coordinate yc : ");
scanf("%f",&yc);
printf("Enter Radius rx : ");
scanf("%f",&rx);
printf("Enter Radius ry : ");
scanf("%f",&ry);
mdpt(xc,yc,rx,ry);
line(getmaxx()/2,0,getmaxx()/2,getmaxy());
line(0,getmaxy()/2,getmaxx(),getmaxy()/2);
getch();

closegraph();
}

September 15, 2008


Navneet Sinha
OUTPUT:

September 22, 2008

Navneet Sinha
Write a program to implement the Rotation on an object about the origin.

# include <iostream.h>
#include<stdio.h>
# include <conio.h>
# include <graphics.h>
# include <math.h>

char IncFlag;
int PolygonPoints[4][2] =
{{10,100},{110,100},{110,200},{10,200}};

void PolyLine()
{
int iCnt;
cleardevice();
line(0,240,640,240);
line(320,0,320,480);
for (iCnt=0; iCnt<4; iCnt++)
{
line(PolygonPoints[iCnt][0],PolygonPoints[iCnt][1],
PolygonPoints[(iCnt+1)%4][0],PolygonPoints[(iCnt+1)%4][1]);
}
}
void Rotate()
{
float r,Angle;
int iCnt;
int Tx,Ty;
cout<<endl;
printf("enter the angle of rotation");
scanf("%f",&r);
Angle = r*(22.0/7.0)/180.0;
for (iCnt=0; iCnt<4; iCnt++)
{
Tx = PolygonPoints[iCnt][0];
Ty = PolygonPoints[iCnt][1];

September 22, 2008

Navneet Sinha
PolygonPoints[iCnt][0] = (Tx - 320)*cos(Angle) -
(Ty - 240)*sin(Angle) + 320;
PolygonPoints[iCnt][1] = (Tx - 320)*sin(Angle) +
(Ty - 240)*cos(Angle) + 240;
}
}

void main()
{
int gDriver = DETECT, gMode;
int iCnt;
initgraph(&gDriver, &gMode, "D:\\TC\\BGI");
for (iCnt=0; iCnt<4; iCnt++)
{
PolygonPoints[iCnt][0] += 320;
PolygonPoints[iCnt][1] = 240 - PolygonPoints[iCnt][1];
}
PolyLine();
getch();
Rotate();
PolyLine();
getch();
}

September 22, 2008

Navneet Sinha
OUTPUT:

September 22, 2008

Navneet Sinha
September 22, 2008

Write a program to implement the Shearing on an object.

# include <iostream.h>
Navneet Sinha
#include<stdio.h>
# include <conio.h>
# include <graphics.h>
# include <math.h>
char IncFlag;
int PolygonPoints[4][2] =
{{10,100},{110,100},{110,200},{10,200}};

void PolyLine()
{
int iCnt;
cleardevice();
line(0,240,640,240);
line(320,0,320,480);
for (iCnt=0; iCnt<4; iCnt++)
{
line(PolygonPoints[iCnt][0],PolygonPoints[iCnt][1],
PolygonPoints[(iCnt+1)%4][0],PolygonPoints[(iCnt+1)%4][1]);
}
}
void SpolyLine()
{
int iCnt;
float sx,sy;
cleardevice();
line(0,240,640,240);
line(320,0,320,480);
printf("enterthe value of sx & sy");
scanf("%f %f",&sx,&sy);
for (iCnt=0; iCnt<4; iCnt++)
{
line(PolygonPoints[iCnt][0]*sx,PolygonPoints[iCnt][1]*sy,
PolygonPoints[(iCnt+1)%4][0]*sx,PolygonPoints[(iCnt+1)%4][1]*sy);
}
}
September 22, 2008

void Rotate()
{
float r,Angle;
int iCnt;

Navneet Sinha
int Tx,Ty;
cout<<endl;
printf("enter the angle of rotation");
scanf("%f",&r);
Angle = r*(22.0/7.0)/180.0;
for (iCnt=0; iCnt<4; iCnt++)
{
Tx = PolygonPoints[iCnt][0];
Ty = PolygonPoints[iCnt][1];
PolygonPoints[iCnt][0] = (Tx - 320)*cos(Angle) -
(Ty - 240)*sin(Angle) + 320;
PolygonPoints[iCnt][1] = (Tx - 320)*sin(Angle) +
(Ty - 240)*cos(Angle) + 240;
}
}

void main()
{
int gDriver = DETECT, gMode;
int iCnt;
initgraph(&gDriver, &gMode, "D:\\TC\\BGI");
for (iCnt=0; iCnt<4; iCnt++)
{
PolygonPoints[iCnt][0] += 320;
PolygonPoints[iCnt][1] = 240 - PolygonPoints[iCnt][1];
}
PolyLine();
getch();
SpolyLine();
getch();
}

September 22, 2008

OUTPUT:

Navneet Sinha
September 22, 2008

Navneet Sinha
September 29, 2008

Write a program to implement Scanfill algorithm.

# include <dos.h>
# include <stdio.h>
Navneet Sinha
# include <graphics.h>
# include <iostream.h>
# include <conio.h>
# include <math.h>

struct Polygon
{
int x;
int y;
};

struct edRecord
{
int YUpper;
float XIntersect;
float dxperscan;
struct edRecord *Next;
};

typedef struct edRecord Edge;


typedef struct Polygon Nodes;
Edge *Active;
Edge **Edges;
int Cntr=6;
Nodes NodesObj[6]={{100,100},{150,150},{150,200},{100,250},{50,200},{50,150}};

void Scanfill();
void InsertEdge(Edge*,Edge*);
void BuildEdgeList(int,Nodes*);
int YNext(int);
void MakeEdgeRec(Nodes,Nodes,int,Edge*);
void BulidActiveList(int);
void UpdateActiveList(int);
void DeleteAfter(Edge*);
September 29, 2008

void ResortActiveList(void);
void ResortActiveList(int);
void main()
{
int gDriver = DETECT, gMode;
int n,xArray[12];
Navneet Sinha
int iCntr,jCntr;

initgraph(&gDriver,&gMode,"c:\\tc\\bgi");

for(iCntr=0,jCntr=0;iCntr<12;iCntr+=2,jCntr++)
xArray[iCntr] = NodesObj[jCntr].x;
for(iCntr=1,jCntr=0;iCntr<12;iCntr+=2,jCntr++)
xArray[iCntr] = NodesObj[jCntr].y;

drawpoly(6,xArray);
line(NodesObj[5].x,NodesObj[5].y,NodesObj[0].x,NodesObj[0].y); //TO
CLOSE POLYGON
getch();
Scanfill();
getch();
closegraph();
restorecrtmode();
return;
}

void Scanfill()
{
int iCntr,Scan;
//initialize edgelists with dummy nodes
Edges=new Edge*[getmaxy()];
for(iCntr=0;iCntr<getmaxy();iCntr++)
{
Edges[iCntr] = new Edge;
Edges[iCntr]->Next = NULL;
}
//initialize Active edgeList with dummy nodes
Active = new Edge;
Active->Next = NULL;

September 29, 2008

BuildEdgeList(Cntr,NodesObj);
for(Scan=0;Scan<getmaxy();Scan++)
{
BulidActiveList(Scan);
if (Active->Next != NULL)
{
Navneet Sinha
ResortActiveList(Scan);
UpdateActiveList(Scan);
ResortActiveList();
}
}
}

void InsertEdge(Edge* edgeList,Edge* InitialEdge)


{
Edge *PEdge,*QEdges;

QEdges=edgeList;
PEdge=QEdges->Next;
while(PEdge!=NULL)
if(InitialEdge->XIntersect < PEdge->XIntersect)
PEdge=NULL;
else
{
QEdges=PEdge;
PEdge=PEdge->Next;
}
InitialEdge->Next = QEdges->Next;
QEdges->Next = InitialEdge;
}

void BuildEdgeList(int Cntr,Nodes* Vertices)


{
Edge* NewEdge;
Nodes Vertex1,Vertex2;
int YPrev,iCntr;
Vertex1.x=Vertices[Cntr-1].x;
Vertex1.y=Vertices[Cntr-1].y;

September 29, 2008

YPrev = Vertices[Cntr-2].y;
for(iCntr=0;iCntr<Cntr;iCntr++)
{
Vertex2.x = Vertices[iCntr].x;
Vertex2.y = Vertices[iCntr].y;
if(Vertex1.y != Vertex2.y)
{
Navneet Sinha
NewEdge = new Edge;
if(Vertex1.y < Vertex2.y)
MakeEdgeRec(Vertex1,Vertex2,YNext(iCntr),NewEdge);
else
MakeEdgeRec(Vertex2,Vertex1,YPrev,NewEdge);
}
YPrev = Vertex1.y;
Vertex1.x = Vertex2.x;
Vertex1.y = Vertex2.y;
}
}

void BulidActiveList(int Scan)


{
Edge *PEdge,*QEdges;
PEdge=Edges[Scan]->Next;
while(PEdge!=NULL)
{
QEdges=PEdge->Next;
InsertEdge(Active,PEdge);
PEdge=QEdges;
}
}

void ResortActiveList(int Scan)


{
Edge *iEdge,*jEdge;
int iCntr;
iEdge = Active->Next;
while(iEdge!=NULL)
{
jEdge = iEdge->Next;
September 29, 2008

for(iCntr=iEdge->XIntersect;iCntr<=jEdge->XIntersect-1;iCntr++)
putpixel(iCntr,Scan,RED);
iEdge=jEdge->Next;
}
}

void UpdateActiveList(int Scan)


{
Navneet Sinha
Edge *PEdge,*QEdges;
QEdges=Active;
PEdge=Active->Next;
while(PEdge!=NULL)
{
if(Scan>=PEdge->YUpper)
{
PEdge=PEdge->Next;
DeleteAfter(QEdges);
}
else
{
PEdge->XIntersect+=PEdge->dxperscan;
QEdges=PEdge;
PEdge=PEdge->Next;
}
}
}

void ResortActiveList()
{
Edge *PEdge,*QEdges;
PEdge=Active->Next;
Active->Next=NULL;
while(PEdge!=NULL)
{
QEdges=PEdge->Next;
InsertEdge(Active,PEdge);
PEdge=QEdges;
}
}
September 29, 2008

int YNext(int YCurr)


{
int jCntr;
if(YCurr+1>Cntr)
jCntr=1;
else
jCntr=YCurr+1;
while(NodesObj[YCurr].y == NodesObj[jCntr].y)
{
Navneet Sinha
if(jCntr+1>Cntr)
jCntr=1;
else
jCntr++;
}
return(NodesObj[jCntr].y);
}

void DeleteAfter(Edge* QEdges)


{
Edge* PEdge;

PEdge = QEdges->Next;
QEdges->Next = PEdge->Next;
delete PEdge;
}

void MakeEdgeRec(Nodes Lower,Nodes Upper,int YComp,Edge* CurrEdge)


{
CurrEdge->dxperscan = (Upper.x - Lower.x) / (Upper.y - Lower.y);
CurrEdge->XIntersect = Lower.x;
if(Upper.y < YComp)
CurrEdge->YUpper = Upper.y - 1;
else
CurrEdge->YUpper = Upper.y;
InsertEdge(Edges[Lower.y],CurrEdge);
}

September 29, 2008

OUTPUT:

Navneet Sinha
September 29, 2008

Navneet Sinha
September 29, 2008

Write a program to implement Boundaryfill algorithm.

#include<iostream.h>
#include<stdlib.h>
#include<conio.h>
#include<graphics.h>
Navneet Sinha
#include<dos.h>

class Circle
{
int XCoord;
int YCoord;
int Radius;
void PlotPoint(int,int,int);
public:
Circle()
{
XCoord=0;
YCoord=0;
Radius=0;
}

Circle(int x, int y, int z)


{
XCoord=x;
YCoord=y;
Radius=z;
}

void CircleMidpoint();
void InitGraphs();
void BoundryFill(int,int,int,int);
void BoundryFillRound();
};

void Circle :: InitGraphs()


{
int gMode, gDriver=DETECT;
int errorcode;
September 29, 2008

initgraph(&gDriver,&gMode,"c:\\tc\\bgi");
errorcode = graphresult();

if (errorcode != grOk) /* an error occurred */


{
cout<<"Graphics error: "<< grapherrormsg(errorcode);
cout<<"Press any key to halt:";
Navneet Sinha
getch();
exit(1); /* return with error code */
}
setbkcolor(3);
}

void Circle :: PlotPoint(int x,int y,int z)


{
putpixel(XCoord+x,YCoord+y,z);
putpixel(XCoord-x,YCoord+y,z);
putpixel(XCoord+x,YCoord-y,z);
putpixel(XCoord-x,YCoord-y,z);
putpixel(XCoord+y,YCoord+x,z);
putpixel(XCoord-y,YCoord+x,z);
putpixel(XCoord+y,YCoord-x,z);
putpixel(XCoord-y,YCoord-x,z);
}

void Circle :: CircleMidpoint()


{
int x,y;
int p;
x=0;
y=Radius;
PlotPoint(x,y,15);

p=1-Radius;
while(x<y)
{
if(p<0)
x+=1;

September 29, 2008

else
{
x+=1;
y-=1;
}

if(p<0)
p=p+2*x+1;
Navneet Sinha
else
{
p=p + 2 *(x-y) + 1;
}
PlotPoint(x,y,15);
}
}

void Circle :: BoundryFillRound()


{
int x,y;
int p;
for(int iCntr=1;iCntr<Radius;iCntr++)
{
x=0;
y=iCntr;
PlotPoint(x,y,5);

p=1-iCntr;
while(x<y)
{
if(p<0)
x+=1;
else
{
x+=1;
y-=1;
}

if(p<0)
p=p+2*x+1;
September 29, 2008

else
{
p=p + 2 *(x-y) + 1;
}
PlotPoint(x,y,5);
}
}
}

Navneet Sinha
void Circle :: BoundryFill(int x, int y,int fill,int bound)
{
int cur;
cur=getpixel(x,y);
if(cur!=bound && cur!= fill)
{
putpixel(x,y,fill);
Circle :: BoundryFill(x+1, y,fill,bound);
Circle :: BoundryFill(x-1, y,fill,bound);
Circle :: BoundryFill(x, y-1,fill,bound);
Circle :: BoundryFill(x, y+1,fill,bound);
Circle :: BoundryFill(x+1, y+1,fill,bound);
Circle :: BoundryFill(x+1, y+1,fill,bound);
Circle :: BoundryFill(x-1, y-1,fill,bound);
Circle :: BoundryFill(x-1, y+1,fill,bound);
}
}

void main()
{
Circle cObj(250,250,42);
cObj.InitGraphs();
cObj.CircleMidpoint();
cObj.BoundryFillRound();
//cObj.BoundryFill(250,250,11,15);
getch();
closegraph();
}

September 29, 2008

OUTPUT:

Navneet Sinha
September 29, 2008

Write a program to implement the reflection of an object along the x-axis.

# include <iostream.h>
# include <conio.h>
# include <graphics.h>
# include <math.h>

Navneet Sinha
char IncFlag;
int PolygonPoints[3][2] =
{{10,100},{110,100},{110,200}};

void PolyLine()
{
int iCnt;
cleardevice();
line(0,240,640,240);
line(320,0,320,480);
for (iCnt=0; iCnt<3; iCnt++)
{
line(PolygonPoints[iCnt][0],PolygonPoints[iCnt][1],
PolygonPoints[(iCnt+1)%3][0],PolygonPoints[(iCnt+1)%3][1]);
}
}
void Reflect()
{
float Angle;
int iCnt;
int Tx,Ty;
cout<<endl;
for (iCnt=0; iCnt<3; iCnt++)
PolygonPoints[iCnt][1] = (480 - PolygonPoints[iCnt][1]);
}

void main()
{
int gDriver = DETECT, gMode;
int iCnt;
initgraph(&gDriver, &gMode, "C:\\TC\\BGI");

September 29, 2008

for (iCnt=0; iCnt<3; iCnt++)


{
PolygonPoints[iCnt][0] += 320;
PolygonPoints[iCnt][1] = 240 - PolygonPoints[iCnt][1];
}
PolyLine();
getch();
Reflect();
Navneet Sinha
PolyLine();
getch();
}

OUTPUT:

September 29, 2008

Navneet Sinha
September 29, 2008

Write a program to implement the reflection of an object along the y-axis.

# include <iostream.h>
# include <conio.h>
# include <graphics.h>
# include <math.h>

Navneet Sinha
char IncFlag;
int PolygonPoints[3][2] =
{{10,100},{110,100},{110,200}};

void PolyLine()
{
int iCnt;
cleardevice();
line(0,240,640,240);
line(320,0,320,480);
for (iCnt=0; iCnt<3; iCnt++)
{
line(PolygonPoints[iCnt][0],PolygonPoints[iCnt][1],
PolygonPoints[(iCnt+1)%3][0],PolygonPoints[(iCnt+1)%3][1]);
}
}
void Reflect()
{
float Angle;
int iCnt;
int Tx,Ty;
cout<<endl;
for (iCnt=0; iCnt<3; iCnt++)
PolygonPoints[iCnt][0] = (640 - PolygonPoints[iCnt][0]);
}

void main()
{
int gDriver = DETECT, gMode;
int iCnt;
initgraph(&gDriver, &gMode, "C:\\TC\\BGI");

September 29, 2008

for (iCnt=0; iCnt<3; iCnt++)


{
PolygonPoints[iCnt][0] += 320;
PolygonPoints[iCnt][1] = 240 - PolygonPoints[iCnt][1];
}
PolyLine();
getch();
Reflect();
Navneet Sinha
PolyLine();
getch();
}

OUTPUT:

September 29, 2008

Navneet Sinha
October 1, 2008

Write a program to implement the Line Clipping.

#include<iostream.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>
#include<dos.h>
Navneet Sinha
#include<process.h>

int pixels[2][4];
float xn1,xn2,yn1,yn2,x3,y3,m;

void show_quadrant()
{
cleardevice();
rectangle(120,40,320,240);
rectangle(320,40,520,240);
rectangle(120,240,320,440);
rectangle(320,240,520,440);
for(int i=130;i<=510;i+=10)
{
if(i==320)
continue;
outtextxy(i,237,"+");
}
for(i=50;i<=430;i+=10)
{
if(i==240)
continue;
outtextxy(317,i,"-");
}
outtextxy(310,230,"O");
outtextxy(530,240,"X");
outtextxy(320,450,"-Y");
outtextxy(100,240,"-X");
outtextxy(320,30,"Y");
}

October 1, 2008

void su_co(int x1,int y1,int x2,int y2,int xmin,int ymin,int xmax,int ymax)
{
int i,j,fl;
for(i=0;i<2;i++)
for(j=0;j<4;j++)
pixels[i][j]=0;
if(y1>ymax)
pixels[0][0]=1;
Navneet Sinha
if(y1<ymin)
pixels[0][1]=1;
if(x1>xmax)
pixels[0][2]=1;
if(x1<xmin)
pixels[0][3]=1;
if(y2>ymax)
pixels[1][0]=1;
if(y2<ymin)
pixels[1][1]=1;
if(x2>xmax)
pixels[1][2]=1;
if(x2<xmin)
pixels[1][3]=1;

for(j=0;j<4;j++)
{
if(pixels[0][j]==0&&pixels[1][j]==0)
continue;
if(pixels[0][j]==1&&pixels[1][j]==1)
{
fl=3;
break;
}
fl=2;
}
switch(fl)
{
case 1:
line(320+x1,240-y1,320+x2,240-y2);
break;
October 1, 2008

case 3:
cout<<"\n\n\a\" Line Is Not Visible...:-(";
break;

case 2:
m=(y2-y1)/(x2-x1);
xn1=x1;
yn1=y1;
xn2=x2;
Navneet Sinha
yn2=y2;
if(pixels[0][0]==1)
{
xn1=x1+(ymax-y1)/m;
yn1=ymax;
}
if(pixels[0][1]==1)
{
xn1=x1+(ymin-y1)/m;
yn1=ymin;
}
if(pixels[0][2]==1)
{
yn1=y1+(xmax-x1)*m;
xn1=xmax;
}
if(pixels[0][3]==1)
{
yn1=y1+(xmin-x1)*m;
xn1=xmin;
}
if(pixels[1][0]==1)
{
xn2=x2+(ymax-y2)/m;
yn2=ymax;
}
if(pixels[1][1]==1)
{
xn2=x2+(ymin-y2)/m;

October 1, 2008

yn2=ymin;
}
if(pixels[1][2]==1)
{
yn2=y2+(xmax-x2)*m;
xn2=xmax;
}
if(pixels[1][3]==1)
{
yn2=y2+(xmin-x2)*m;
Navneet Sinha
xn2=xmin;
}
line(320+xn1,240-yn1,320+xn2,240-yn2);
break;
}
}

void midpt(int x1,int y1,int x2,int y2,int xmin,int ymin,int xmax,int ymax)
{
int fl=1;
int i,j;
int ox1=x1,ox2=x2,oy1=y1,oy2=y2;
for(i=0;i<2;i++)
for(j=0;j<4;j++)
pixels[i][j]=0;

if(y1>ymax)
pixels[0][0]=1;
if(y1<ymin)
pixels[0][1]=1;
if(x1>xmax)
pixels[0][2]=1;
if(x1<xmin)
pixels[0][3]=1;
if(y2>ymax)
pixels[1][0]=1;
if(y2<ymin)
pixels[1][1]=1;

October 1, 2008

if(x2>xmax)
pixels[1][2]=1;
if(x2<xmin)
pixels[1][3]=1;

for(j=0;j<4;j++)
{
if(pixels[0][j]==0&&pixels[1][j]==0)
continue;

if(pixels[0][j]==1&&pixels[1][j]==1)
Navneet Sinha
{
fl=3;
break;
}
fl=2;
}
switch(fl)
{
case 1:
line(320+x1,240-y1,320+x2,240-y2);
break;

case 3:
cout<<"\n\n\a\" Line Is Not Visible...:-(";
break;

case 2:
xn1=x1;
yn1=y1;
xn2=x2;
yn2=y2;
fl=0;
x3=x1;
y3=y1;
while(1)
{
if(!(y1>ymax || y1<ymin || x1>xmax || x1<xmin) && (x3 || y3)!=0.1)
break;
October 1, 2008

x3=(x1+x2)/2;
y3=(y1+y2)/2;
if(!(y3>ymax || y3<ymin || x3>xmax || x3<xmin))
fl=1;
else
fl=0;
if(fl)
{
x2=x3;
y2=y3;
}
else
Navneet Sinha
{
x1=x3;
y1=y3;
}
}
xn1=x3;
yn1=y3;
fl=0;
x1=ox1;
x2=ox2;
y1=oy1;
y2=oy2;
x3=x2;
y3=y2;
while(1)
{
if(!(y2>ymax || y2<ymin || x2>xmax || x2<xmin) && (x3 || y3)!=0.1)
break;
x3=(x1+x2)/2;
y3=(y1+y2)/2;

if(!(y3>ymax || y3<ymin || x3>xmax || x3<xmin))


fl=1;
else
fl=0;

if(fl)
October 1, 2008

{
x1=x3;
y1=y3;
}
else
{
x2=x3;
y2=y3;
}
}
xn2=x3;
yn2=y3;
line(320+xn1,240-yn1,320+xn2,240-yn2);
Navneet Sinha
break;
}
}

void show_message()
{
char *mess[]={"-","=","["," ","L","i","n","e"," ","C","l","i",
"p","p","i","n","g"," ","]","=","-",};
int xx=29,xxx=50,i,j;
_setcursortype(_NOCURSOR);
for(i=0,j=21;i<13,j>=11;i++,j--)
{
gotoxy(xx,1);
cout<<mess[i];
xx++;
gotoxy(xxx,1);
cout<<mess[j];
xxx--;
delay(50);
}
_setcursortype(_NORMALCURSOR);
}

October 1, 2008

void main()
{
clrscr();
int gd=DETECT,gm,i,j;
int xmin,ymin,xmax,ymax,x1,y1,x2,y2;
int choice,ed[20],num;
show_message();
cout<<"\n\n\t\t\" Enter The Co-Ordinates Of The Clipping Window.\"";
cout<<"\n\n\t\t\" Enter X(min) & Y(min) \":=";
cin>>xmin>>ymin;
cout<<"\n\t\t\" Enter X(max) & Y(max) \":=";
cin>>xmax>>ymax;

cout<<"\n\t\t\" Enter The Co-Ordinates Of The Line.\"";


Navneet Sinha
cout<<"\n\n\t\t\" Enter X(1) & Y(1) \":=";
cin>>x1>>y1;
cout<<"\n\t\t\" Enter X(2) & Y(2) \":=";
cin>>x2>>y2;
clrscr();
show_message();
cout<<"\n\n\n\t\t1:==>\" Sutherland-Cohen \"";
cout<<"\n\n\t\t2:==>\" Mid-Point Method \"";
cout<<"\n\n\t\t3:==>\" Exit \"";
cout<<"\n\n\t\t\" Enter Your Choice \":=";
cin>>choice;
switch(choice)
{
case 1:
initgraph(&gd,&gm,"..\\bgi");
clearviewport();
show_quadrant();
line(320+xmin,240-ymin,320+xmin,240-ymax);
line(320+xmin,240-ymax,320+xmax,240-ymax);
line(320+xmax,240-ymax,320+xmax,240-ymin);
line(320+xmax,240-ymin,320+xmin,240-ymin);
line (320+x1,240-y1,320+x2,240-y2);
getch();
cleardevice();

October 1, 2008

show_quadrant();
line(320+xmin,240-ymin,320+xmin,240-ymax);
line(320+xmin,240-ymax,320+xmax,240-ymax);
line(320+xmax,240-ymax,320+xmax,240-ymin);
line(320+xmax,240-ymin,320+xmin,240-ymin);
su_co(x1,y1,x2,y2,xmin,ymin,xmax,ymax);
getch();
break;

case 2:
initgraph(&gd,&gm,"..\\bgi");
clearviewport();
show_quadrant();
line(320+xmin,240-ymin,320+xmin,240-ymax);
line(320+xmin,240-ymax,320+xmax,240-ymax);
Navneet Sinha
line(320+xmax,240-ymax,320+xmax,240-ymin);
line(320+xmax,240-ymin,320+xmin,240-ymin);
line (320+x1,240-y1,320+x2,240-y2);
getch();
cleardevice();
show_quadrant();
line(320+xmin,240-ymin,320+xmin,240-ymax);
line(320+xmin,240-ymax,320+xmax,240-ymax);
line(320+xmax,240-ymax,320+xmax,240-ymin);
line(320+xmax,240-ymin,320+xmin,240-ymin);
midpt(x1,y1,x2,y2,xmin,ymin,xmax,ymax);
getch();
break;

case 3:
exit(0);
default:
cout<<"\n\t\a\" Press A Valid Key...!!! \"";
getch();
main();
break;
}
closegraph();
}
October 1, 2008

OUTPUT:

Navneet Sinha
October 1, 2008

Navneet Sinha
October 1, 2008

Navneet Sinha
October 1, 2008

Navneet Sinha
October 8, 2008

Write a program to implement polygon clipping.

#include<stdio.h>
#include<conio.h>
#include<iostream.h>
#include<graphics.h>
#define ROUND(a) ((int)(a+0.5))
Navneet Sinha
#define n 4

#define LEFT_EDGE 0x1


#define RIGHT_EDGE 0x2
#define BOTTOM_EDGE 0x4
#define TOP_EDGE 0x8

#define INSIDE(a) (!a)


#define REJECT(a,b) (a&b)
#define ACCEPT(a,b) (!(a|b))

typedef struct wcpt2


{
int x,y;
}wcpt2;

typedef struct dcpt


{
int x,y;
}dcpt;

void main()
{
int gd=DETECT,gm;
int left,top,right,bottom;
int x1,x2,y1,y2;
int maxx, maxy;
/* our polygon array */
int poly[10];
void clipline(dcpt,dcpt,wcpt2,wcpt2);
clrscr();

October 8, 2008

initgraph(&gd,&gm,"c:\\tc30\\bgi");
maxx = getmaxx()/4;
maxy = getmaxy()/4;

poly[0] = 20; /* 1st vertex */


poly[1] = maxy / 2;

Navneet Sinha
poly[2] = maxx - 10; /* 2nd */
poly[3] = 10;

poly[4] = maxx - 50; /* 3rd */


poly[5] = maxy - 20;

poly[6] = maxx / 2; /* 4th */


poly[7] = maxy / 2;

/* drawpoly doesn't automatically close


the polygon, so we close it.
*/
poly[8] = poly[0];
poly[9] = poly[1];

/* draw the polygon */


drawpoly(5, poly);

rectangle(20,25,80,125);
wcpt2 pt1,pt2;
dcpt winmin,winmax;

winmin.x=20;
winmin.y=25;
winmax.x=80;
winmax.y=125;

pt1.x=20;
pt1.y=maxy/2;
pt2.x=maxx-10;
pt2.y=10;
October 8, 2008

// clipline(winmin,winmax,pt1,pt2);

int i=0;
for(int index=0;index<n;index++)
{
if(index==n-1)
{
pt1.x=poly[i];
pt1.y=poly[i+1];
Navneet Sinha
i=0;
pt2.x=poly[i];
pt2.y=poly[i+1];
clipline(winmin,winmax,pt1,pt2);
}
else
{
pt1.x=poly[i];
pt1.y=poly[i+1];
pt2.x=poly[i+2];
pt2.y=poly[i+3];
clipline(winmin,winmax,pt1,pt2);
}
i+=2;
}
pt1.x=poly[i];
pt1.y=poly[i+1];
clipline(winmin,winmax,pt1,pt2);
getch();
}

unsigned char encode(wcpt2 pt,dcpt winmin,dcpt winmax)


{
unsigned char code=0x00;
if(pt.x < winmin.x)
code=code | LEFT_EDGE;
if(pt.x > winmax.x)
code=code | RIGHT_EDGE;

October 8, 2008

if(pt.y < winmin.y)


code=code | TOP_EDGE;
if(pt.y > winmax.y)
code=code | BOTTOM_EDGE;
return code;
}

void swappts(wcpt2 *p1,wcpt2 *p2)


{
wcpt2 tmp;
Navneet Sinha
tmp = *p1;
*p1 = *p2;
*p2 = tmp;
}

void swapcode(unsigned char *c1,unsigned char *c2)


{
unsigned char tmp;
tmp = *c1;
*c1 = *c2;
*c2 = tmp;
}

void clipline(dcpt winmin,dcpt winmax,wcpt2 p1,wcpt2 p2)


{
unsigned char encode(wcpt2,dcpt,dcpt);
unsigned char code1,code2;
int done = 0 , draw = 0;
float m;
void swapcode(unsigned char *c1,unsigned char *c2);
void swappts(wcpt2 *p1,wcpt2 *p2);

while(!done)
{
code1 = encode(p1,winmin,winmax);
code2 = encode(p2,winmin,winmax);
if(ACCEPT(code1,code2))
{
draw = 1;
October 8, 2008

done = 1;
}
else if(REJECT(code1,code2))
done = 1;
else if(INSIDE(code1))
{
swappts(&p1,&p2);
swapcode(&code1,&code2);
}
if(code1 & LEFT_EDGE)
{
Navneet Sinha
p1.y += (winmin.x - p1.x) * (p2.y - p1.y) / (p2.x - p1.x);
p1.x = winmin.x;
}
else if(code1 & RIGHT_EDGE)
{
p1.y += (winmax.x - p1.x) * (p2.y - p1.y) / (p2.x - p1.x);
p1.x = winmax.x;
}
else if(code1 & TOP_EDGE)
{
if(p2.x != p1.x)
p1.x += (winmin.y - p1.y) * (p2.x - p1.x) / (p2.y - p1.y);
p1.y = winmin.y;
}
else if(code1 & BOTTOM_EDGE)
{
if(p2.x != p1.x)
p1.x += (winmax.y - p1.y) * (p2.x - p1.x) / (p2.y - p1.y);
p1.y = winmax.y;
}
}
if(draw)
{
setcolor(5);
line(p1.x,p1.y,p2.x,p2.y);
}
}

October 8, 2008

OUTPUT:

Navneet Sinha
Navneet Sinha

You might also like