Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

1) WAP For Generating An Axis: Output

Download as pdf or txt
Download as pdf or txt
You are on page 1of 21

1]WAP for generating an axis.

:-
#include<conio.h>
#include<graphic.h>
#include<iostream.h>
void main()
{
clrscr();
int gd=DETECT, gm;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
line(320,0,320,480);
line(0,240,640,240);
getch();
closegraph();
}

OUTPUT:
2]WAP to print dashed line using vector generation algorithm
:-
#include<stdio.h>
#include<conio.h>
#include<iostream.h>
#include<graphics.h>
#include<math.h>
class Line
{
int x1,x2;
int y1,y2,len;
int dx,dy;
public:
void initgraph1();
void get();
void points();
void cal();
void display();
int sign(int);
};
int Line::sign(int a)
{
if(a<0)
return -1;
else
if(a==0)
return 0;
else
return 1;
}
void Line::initgraph1()
{
int gd = DETECT,gm,errorcode;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
}
void Line::display()
{
setbkcolor(1);
outtextxy(590,240,"x-axis");
outtextxy(200,20,"y-axis");
setcolor(RED);
line(318,20,318,450);//x-axis
line(20,210,620,210);//y-axis
}
void Line::points()
{
int c=350,val=10,cc=44,ccn=36,t=286;
for(int i=1;i<9;i++)
{
line(c,200,c,220);//x-positive axis
c+=30;
gotoxy(cc,12);
cout<<val;
cc+=4;
val+=10;
line(t,200,t,220);//negative x axis
t-=30;
gotoxy(ccn,15);
ccn-=4;
cout<<(val*(-1))+10;
} int r=180;//positive y
axis
int row=12;
val=10;
for(i=1;i<7;i++)
{
line(308,r,328,r);
r-=30;
gotoxy(35,row);
cout<<val;
val+=10;
row-=2;
} r=240;
row=15;
val=10;
for(i=1;i<7;i++)
{
line(308,r,328,r);
r+=28;
gotoxy(42,row);
cout<<val*(-1);
val+=10;
row+=2;
}
circle(318,210,10);
putpixel(318,210,RED);
}
void Line::get()
{
gotoxy(1,1);
cout<<"Enter First point:";
cin>>x1;
cin>>y1;
gotoxy(1,4);
cout<<"Enter second point:";
cin>>x2;
cin>>y2;
}
void Line::cal()
{
x1=(318+(x1*3));
x2=(318+(x2*3));
y1=(210-(y1*3));
y2=(210-(y2*3));
float x,y;
if (abs(x2-x1)>abs(y2-y1))
len=abs(x2-x1);
else
len=abs(y2-y1);
if((y2-y1)<0)
dy=-1;
else
dy=1;
if((x2-x1)<0)
dx-=1;
else
dx=1;
x=x1;
y=y1;
int i=0;
x=x+(.5*sign(dx));
y=y+(.5*sign(dy));
gotoxy(5,10);
int count=20;
while(i<len)
{
if(count>10)
{
putpixel(x,y,RED);
x=x+dx;
y=y+dy;
count--;
i++;
} if(count==10)
{
count=0;
} if(count<=10)
{
i++;
count++;
x=x+dx;
y=y+dy;
if(count==9)
{
count=20;
}
}
}
}
void main()
{
clrscr();
Line obj;
obj.initgraph1();
obj.display();
obj.points();
obj.get();
obj.cal();
getch();
}

Output:-
3]WAP to create Line using Breseham’s algorithm.
:-
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<iostream.h>
#include<graphics.h>
char IncFlag;
void bresenham(int x1,int x2,int y1,int y2);
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
int x1,x2,y1,y2;
void bresenham(int,int,int,int);
cout<<"\nEnter x1:";
cin>>x1;
cout<<"\nEnter x2:";
cin>>x2;
cout<<"\nEnter y1:";
cin>>y1;
cout<<"\nEnter y2:";
cin>>y2;
line(320,0,320,480);
line(0,240,640,240);
bresenham(x1,x2,y1,y2);
getch();
closegraph();
}
void bresenham(int x1,int x2,int y1,int y2)
{
int S,O,End,p;
int dx=abs(x1-x2);
int dy=abs(y1-y2);
float slope;
int PInc,NInc,XInc,YInc;
if(dx==0)
{}
else
{
slope=(float)(y1-y2)/(x1-x2);
if(slope>-1 && slope<1)
{
IncFlag='X';
PInc=2*(dy-dx);
NInc=2*(dy-dx);
if(x1>x2)
{
S=x2;
O=y2;
End=x1;
}
else
{
S=x1;
O=y1;
End=x2;
}
}
else
{
IncFlag='Y';
PInc=2*(dx-dy);
NInc=2*dx;
p=2*(dx-dy);
if(y1>y2)
{
O=x2;
S=y2;
End=y1;
}
else
{
O=x1;
S=y1;
End=y2;
} if(IncFlag=='X')
{
putpixel(320+S,240-O,12);
}
else
{
putpixel(320+O,240-S,12);
}
while(S<=End)
{
S++;
if(slope>0.0)
{
O++;
}
else
{
O--;
}
} if(IncFlag=='X')
{
putpixel(320+S,240-O,12);
}
else
{
putpixel(320+O,240-O,12);
}
}
}
}
OUTPUT:
4]WAP to create line using DDA algorithm.
:-
#include<graphics.h>
#include<math.h>
#include<conio.h>
#include<iostream.h>
void DDALine (int x1, int y1, int x2, int y2, int iColor);
void main()
{ int gDriver=DETECT, gMode;
int x1,x2,y1,y2,iColor;
initgraph(&gDriver,&gMode,"C:\\TURBOC3\\BGI");
cleardevice();
cout<<endl<<"Enter x1:";
cin>>x1;
cout<<"Enter y1:";
cin>>y1;
cout<<endl<<"Enter x2:";
cin>>x2;
cout<<"Enter y2:";
cin>>y2;
cout<<endl<<"Enter COLOR:";
cin>>iColor;
cleardevice();
DDALine(320,1,320,480,12);
DDALine(1,240,640,240,12);
circle(320,240,2);
DDALine(320+x1,240-y1,320+x2,240-y2,iColor%16);
getch();
}
void DDALine(int x1,int y1,int x2,int y2,int iColor)
{ float dX,dY,iSteps;
float xInc,yInc,iCount,x,y;
dX=x1-x2;
dY=y1-y2;
if (fabs(dX)>fabs(dY))
{ iSteps=fabs(dX);
}
else
{ iSteps=fabs(dY);
}
xInc=dX/iSteps;
yInc=dY/iSteps;
x=x1;
y=y1;
circle(x,y,1);
for(iCount=1; iCount<=iSteps; iCount++)
{
putpixel(floor(x),floor(y),iColor);
x-=xInc;
y-=yInc;
}
circle(x,y,1);
return;
}

OUTPUT:
5]WAP to generate an arc.
:-
#include<conio.h>
#include<graphics.h>
int main()
{
int gd=DETECT, gm;
int x = 250;
int y = 250;
int start_angle = 155;
int end_angle = 300;
int radius = 100;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
arc(x,y,start_angle,end_angle,radius);
getch();
closegraph();
return 0;
}

OUTPUT:
6]WAP to draw 2D object and perform transformation.
:-
#include<math.h>
#include<conio.h>
#include<graphics.h>
#include<iostream.h>
class transform
{
public:
int m,a[20][20],c[20][20];
int i,j,k;
public:
void object();
void accept();
void operator *(float b[20][20])
{
for(int i=0;i<m;i++)
{
for(int j=0;j<m;j++)
{
c[i][j]=0;
for(int k=0;k<m;k++)
{
c[i][j]=c[i][j]+(a[i][k]*b[k][j]);
}
}
}
}
};
void transform::object()
{
int gd,gm;
gd=DETECT;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
line(300,0,300,600);
line(0,300,600,300);
for(i=0;i<m-1;i++)
{
line(300+a[i][0],300-a[i][1],300+a[i+1][0],300-a[i+1][1]);
}
line(300+a[0][0],300-a[0][1],300+a[i][0],300-a[i][1]);
for(i=0;i<m-1;i++)
{
line(300+c[i][0],300-c[i][1],300+c[i+1][0],300-c[i+1][1]);
}
line(300+c[0][0],300-c[0][1],300+c[i][0],300-c[i][1]);
int temp;
cout<<"Press 1 to continue:";
cin>>temp;
closegraph();
}
void transform::accept()
{
cout<<"\n";
cout<<"Enter the number of edges:";
cin>>m;
cout<<"Enter the coordinates:";
for(int i=0;i<m;i++)
{
for(int j=0;j<3;j++)
{
if(j>=2)
a[i][j]=1;
else
cin>>a[i][j];
}
}
}
int main()
{
int ch,tx,ty,sx,sy;
float deg,theta,b[20][20];
transform t;
t.accept();
cout<<"\nEnter your choice:";
cout<<"\n1.Translation"
"\n2.Scaling"
"\n3.Rotation";
cin>>ch;
switch(ch)
{
case 1: cout<<"\nTRANSLATION OPERATION\n";
cout<<"Enter value for tx and ty:";
cin>>tx>>ty;
b[0][0]=b[2][2]=b[1][1]=1;
b[0][1]=b[0][2]=b[1][0]=b[1][2]=0;
b[2][0]=tx;
b[2][1]=ty;
t*b;
t.object();
break;
case 2: cout<<"\nSCALING OPERATION\n";
cout<<"Enter value for sx and sy:";
cin>>sx>>sy;
b[0][0]=sx;
b[0][1]=sy;
b[0][1]=b[0][2]=b[1][0]=b[1][2]=0;
b[2][0]=b[2][1]=0;
b[2][2]=1;
t*b;
t.object();
break;
case 3: cout<<"\nROTATION OPERATION\n";
cout<<"Enter value for angle:";
cin>>deg;
theta=deg*(3.14/100);
b[0][1]=b[1][1]=cos(theta);
b[0][1]=sin(theta);
b[1][0]=sin(-theta);
b[0][2]=b[1][2]=b[2][2]=b[2][1]=0;
b[2][2]=1;
t*b;
t.object();
break;
default:
cout<<"\nInvalid choice";
}
getch();
return 0;
}

Output:-
7]WAP to construct a polygon.
:-
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
class POLYGON
{
float x1,x2,y1,y2,x,y;
int i,n;
public:
void Getdata();
void Poly();
};
void POLYGON::Getdata()
{
int gd = DETECT,gm;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
rectangle(158,0,637,479);
cout<<"Enter x1,y1,"<<endl;
cin>>x>>y;
x1=x;y1=y;
cout<<"Enter number of edges"<<endl;
cin>>n;
for(i=0;i<n;i++)
{
if(i==(n-1))
{
x2=x;
y2=y;
}
else
{
cout<<"Enter x2,y2"<<endl;
cin>>x2>>y2;
}
Poly();
x1=x2;
y1=y2;
}
getch();
closegraph();
}
void POLYGON::Poly()
{
setcolor(2);
line(x1+158,y1,x2+158,y2);
}
void main()
{
clrscr();
POLYGON P;
P.Getdata();
getch();
}

OUTPUT:
9]WAP to fill a closed figure.
:-
#include<dos.h>
#include<conio.h>
#include<graphics.h>
void main()
{
clrscr();
int gd=DETECT, gm, i, x, y;
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
x = getmaxx()/3;
y = getmaxx()/3;
setcolor(BLUE);
for(i=1;i<=8;i++)
{
setfillstyle(i,i);
delay(20);
circle(x, y, i*20);
floodfill(x-2+i*20,y,BLUE);
}
getch();
closegraph();
}

Output:-
10]WAP to draw Bezier’s curve.
:-
#include <conio.h>
#include <stdlib.h>
#include <graphics.h>
#include <math.h>
#include<iostream.h>
void bezier (int x[4], int y[4])
{
int gd = DETECT, gm;
int i;
double t;
initgraph (&gd, &gm, "C:\\TURBOC3\\BGI");
for (t = 0.0; t < 1.0; t += 0.0005)
{
double xt = pow (1-t, 3) * x[0] + 3 * t * pow (1-t, 2) * x[1] +
3 * pow (t, 2) * (1-t) * x[2] + pow (t, 3) * x[3];
double yt = pow (1-t, 3) * y[0] + 3 * t * pow (1-t, 2) * y[1] +
3 * pow (t, 2) * (1-t) * y[2] + pow (t, 3) * y[3];
putpixel (xt, yt, WHITE);
}
for (i=0; i<4; i++)
putpixel (x[i], y[i], YELLOW);
getch();
closegraph();
return;
}
void main()
{
clrscr();
int x[4], y[4];
int i;
cout<<"Enter the x- and y-coordinates of the four control points\n";
for (i=0; i<4; i++)
cin>>x[i]>>y[i];
bezier (x, y);
getch();
}

OUTPUT:

You might also like