You are on page 1of 19

6.

WAP to translate and scale a triangle


#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include<math.h>
void 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:\tc\bg:");
printf("\t Program for basic transactions");
printf("\n\t 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.Transaction\n 2.Rotation\n 3.Scalling\n 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();
}#include <conio.h>
#include <iostream.h>
#include <graphics.h>
static int LEFT=1,RIGHT=2,BOTTOM=4,TOP=8,xl,yl,xh,yh;
int getcode(int x,int y){
int code = 0;
//Peform Bitwise OR to get outcode
if(y<yh) code |=TOP;
if(y>yl) code |=BOTTOM;
if(x<xl) code |=LEFT;
if(x>xh) code |=RIGHT;
return code;
}
void main(){
int gdriver = DETECT,gmode;
initgraph(&gdriver,&gmode,C:\TC\BGI);
setcolor(BLUE);
cout<<Enter bottom left and top right co-ordinates of window: ;
cin>>xl>>yl>>xh>>yh;
rectangle(xl,yl,xh,yh);
int x1,y1,x2,y2;
cout<<Enter the endpoints of the line: ;
cin>>x1>>y1>>x2>>y2;
line(x1,y1,x2,y2);
getch();
int outcode1=getcode(x1,y1), outcode2=getcode(x2,y2);
int accept = 0; //decides if line
is to be drawn
while(1){
float m =(float)(y2-y1)/(x2-x1);
if(outcode1==0&&outcode2==0){ //Both points inside.
Accept line
accept = 1;
break;
}else if((outcode1 & outcode2)!=0){ //AND of both codes
!= 0.Line is outside. Reject line
break;
}else{
int x,y;
int temp;
if(outcode1==0) temp = outcode2; //Decide if point1 is
inside. if not calculate intersection
else temp = outcode1;
if(temp & TOP){ //Line clips top edge
x = x1+ (yh-y1)/m;
y = yh;
}else if(temp & BOTTOM){ //Line clips bottom edge
x = x1+ (yl-y1)/m;
y = yl;
}else if(temp & LEFT){ //Line clips left edge
x = xl;
y = y1+ m*(xl-x1);
}else if(temp & RIGHT){ //Line clips right edge
x = xh;
y = y1+ m*(xh-x1);
}
if(temp == outcode1){ //Check which point we had selected earlier as temp,
and replace its co-ordinates
x1 = x;
y1 = y;
outcode1 = getcode(x1,y1);
}else{
x2 = x;
y2 = y;
outcode2 = getcode(x2,y2);
}
}
}
setcolor(WHITE);
cout<<After clipping:;
if(accept) line(x1,y1,x2,y2);
getch();
closegraph();
}
7. WAP to rotate a triangle

# include <iostream.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 Angle;
int iCnt;
int Tx,Ty;
cout<<endl;
Angle = 30.0*(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, "C:\\TC\\BGI");
for (iCnt=0; iCnt<4; iCnt++)
{
PolygonPoints[iCnt][0] += 320;
PolygonPoints[iCnt][1] = 240 - PolygonPoints[iCnt][1];
}
PolyLine();
getch();
Rotate();
PolyLine();
getch();
}
8. WAP to reflect a triangle
/*Reflection in x Axis*/

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

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");
for (iCnt=0; iCnt<3; iCnt++)
{
PolygonPoints[iCnt][0] += 320;
PolygonPoints[iCnt][1] = 240 - PolygonPoints[iCnt][1];
}
PolyLine();
getch();
Reflect();
PolyLine();
getch();
}
9. WAP to apply Cohen Sutherland Clipping Algorithm
#include <conio.h>
#include <iostream.h>
#include <graphics.h>
static int LEFT=1,RIGHT=2,BOTTOM=4,TOP=8,xl,yl,xh,yh;
int getcode(int x,int y){
int code = 0;
//Peform Bitwise OR to get outcode
if(y<yh) code |=TOP;
if(y>yl) code |=BOTTOM;
if(x<xl) code |=LEFT;
if(x>xh) code |=RIGHT;
return code;
}
void main(){
int gdriver = DETECT,gmode;
initgraph(&gdriver,&gmode,C:\TC\BGI);
setcolor(BLUE);
cout<<Enter bottom left and top right co-ordinates of window: ;
cin>>xl>>yl>>xh>>yh;
rectangle(xl,yl,xh,yh);
int x1,y1,x2,y2;
cout<<Enter the endpoints of the line: ;
cin>>x1>>y1>>x2>>y2;
line(x1,y1,x2,y2);
getch();

int outcode1=getcode(x1,y1), outcode2=getcode(x2,y2);


int accept = 0; //decides if line
is to be drawn
while(1){
float m =(float)(y2-y1)/(x2-x1);
if(outcode1==0&&outcode2==0){ //Both points inside.
Accept line
accept = 1;
break;
}else if((outcode1 & outcode2)!=0){ //AND of both codes
!= 0.Line is outside. Reject line
break;
}else{
int x,y;
int temp;
if(outcode1==0) temp = outcode2; //Decide if point1 is
inside. if not calculate intersection
else temp = outcode1;

if(temp & TOP){ //Line clips top edge


x = x1+ (yh-y1)/m;
y = yh;
}else if(temp & BOTTOM){ //Line clips bottom edge
x = x1+ (yl-y1)/m;
y = yl;
}else if(temp & LEFT){ //Line clips left edge
x = xl;
y = y1+ m*(xl-x1);
}else if(temp & RIGHT){ //Line clips right edge
x = xh;
y = y1+ m*(xh-x1);
}
if(temp == outcode1){ //Check which point we had selected earlier as temp,
and replace its co-ordinates
x1 = x;
y1 = y;
outcode1 = getcode(x1,y1);
}else{
x2 = x;
y2 = y;
outcode2 = getcode(x2,y2);
}
}
}
setcolor(WHITE);
cout<<After clipping:;
if(accept) line(x1,y1,x2,y2);
getch();
closegraph();
}
10. WAP to draw hyperbola
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>

int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
int midx, midy;
int stangle = 45, endangle = 135;
int radius = 100;

/* initialize graphics and local


variables */
initgraph(&gdriver, &gmode, "");

/* read result of initialization */


errorcode = graphresult();
/* an error occurred */
if (errorcode != grOk)
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();

exit(1); /* terminate with an error code */


}

midx = getmaxx() / 2;
midy = getmaxy() / 2+50;
setcolor(getmaxcolor());

/* draw arc */

arc(midx, midy, stangle, endangle, radius);

arc( midx, midy-220, 225, 315, radius);

/* clean up */
getch();
closegraph();
return 0;
}
11. WAP to clip a polygon using Sutherland Hodgeman Algorithm
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#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;
}
}

void main()
{
int gdriver=DETECT,gmode,n,poly[20];
float xi,yi,xf,yf,polyy[20];
clrscr();
cout<<"Coordinates of rectangular clip window :\nxmin,ymin :";
cin>>xmin>>ymin;
cout<<"xmax,ymax :";
cin>>xmax>>ymax;
cout<<"\n\nPolygon to be clipped :\nNumber of sides :";
cin>>n;
cout<<"Enter the coordinates :";
for(int 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:\\TC\\BGI");
setcolor(RED);
rectangle(xmin,ymax,xmax,ymin);
cout<<"\t\tUNCLIPPED 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();
}

You might also like