Source code in C
#include<graphics.h>
#include<stdlib.h>
#include<stdio.h>
#include<conio.h>
#include<dos.h>
#include<math.h>
void line_solid(int, int, int, int);
void circle_solid(int, int, int);
void circle_dot(int, int, int);
void circle_dash(int, int, int);
void circle_dashdot(int, int, int);
void circle_solidt(int, int, int);
void main()
{
//Request autodetection
int gdriver = DETECT,gmode,errorcode;
int xmax,ymax,x1,y1,r,l;
//Initialize graphics and local variables
A:
initgraph(&gdriver,&gmode,"C:\\TC\\BGI");
//Read result of initialization
errorcode=graphresult();
if(errorcode!=grOk) //Error occured
{
printf("Graphics error : %s\n",grapherrormsg(errorcode));
printf("Press any key to halt.");
getch();
exit(1);
}
// setcolor(2);
xmax=getmaxx();
ymax=getmaxy();
line_solid((xmax/2)+1,0,(xmax/2)+1,ymax);
line_solid(0,(ymax/2),xmax,(ymax/2));
gotoxy(1,1);
printf("0:Exit 1:Solid 2:Dot 3:Dash 4:DashDot 5:Thick");
gotoxy(1,3);
printf("Enter value for center point (xc,yc) : ");
gotoxy(1,4);
printf("Enter value for radius (r) : ");
//rectangle(0,65,xmax,ymax);
//setviewport(1,57,xmax-1,ymax-1,1);
gotoxy(1,2);
printf("Which circle line you want : ");
gotoxy(30,2);
scanf("%d",&l);
if(l==0)
exit(0);
if(l!=1&&l!=2&&l!=3&&l!=4&&l!=5)
{
gotoxy(45,2);
printf("Enter valid choice");
getch();
clrscr();
goto A;
}
gotoxy(40,3);
printf("(");
gotoxy(45,3);
printf(",");
gotoxy(50,3);
printf(")");
gotoxy(41,3);
scanf("%d",&x1);
gotoxy(46,3);
scanf("%d",&y1);
gotoxy(30,4);
scanf("%d",&r);
if(l==1) circle_solid((xmax/2)+x1,(ymax/2)+y1,r);
else if(l==2) circle_dot((xmax/2)+x1,(ymax/2)+y1,r);
else if(l==3) circle_dash((xmax/2)+x1,(ymax/2)+y1,r);
else if(l==4) circle_dashdot((xmax/2)+x1,(ymax/2)+y1,r);
else if(l==5) circle_solidt((xmax/2)+x1,(ymax/2)+y1,r);
getch();
goto A;
}
void line_solid(int x1, int y1, int x2, int y2)
{
int dx=abs(x1-x2),dy=abs(y1-y2);
float p=2*dy-dx,m=2*dx-dy;
float twoDy=2*dy,twoDyDx=2*(dy-dx),twoDx=2*dx,twoDxDy=2*(dx-dy);
float x,y,xEnd,yEnd;
if(dx>=dy)
{
if(x1>x2)
{
x=x2;
y=y2;
xEnd=x1;
}
else
{
x=x1;
y=y1;
xEnd=x2;
}
putpixel(x,y,6);
while(x<xEnd)
{
x++;
if(p<0)
{
p+=twoDy;
}
else
{
y++;
p+=twoDyDx;
}
putpixel(x,y,6);
}
}
else //if(dx<dy)
{
if(y1>y2)
{
x=x2;
y=y2;
yEnd=y1;
}
else
{
x=x1;
y=y1;
yEnd=y2;
}
putpixel(x,y,6);
while(y<=yEnd)
{
y++;
if(m<0)
{
m+=twoDx;
}
else
{
x++;
m+=twoDxDy;
}
putpixel(x,y,6);
}
}
}
void circle_solid(int x1, int y1, int r)
{
int x=0,y=r,p=1-r;
putpixel(x1+x,y1+y,6);
putpixel(x1-x,y1+y,6);
putpixel(x1+x,y1-y,6);
putpixel(x1-x,y1-y,6);
putpixel(x1+y,y1+x,6);
putpixel(x1-y,y1+x,6);
putpixel(x1+y,y1-x,6);
putpixel(x1-y,y1-x,6);
while(x<y)
{
if(p<0)
{
x++;
p+=(2*x)+1;
}
else
{
x++;
y--;
p+=(2*(x-y))+1;
}
putpixel(x1+x,y1+y,6);
putpixel(x1-x,y1+y,6);
putpixel(x1+x,y1-y,6);
putpixel(x1-x,y1-y,6);
putpixel(x1+y,y1+x,6);
putpixel(x1-y,y1+x,6);
putpixel(x1+y,y1-x,6);
putpixel(x1-y,y1-x,6);
}
}
void circle_dot(int x1, int y1, int r)
{
int x=0,y=r,p=1-r,k=0;
putpixel(x1+x,y1+y,6);
putpixel(x1-x,y1+y,6);
putpixel(x1+x,y1-y,6);
putpixel(x1-x,y1-y,6);
putpixel(x1+y,y1+x,6);
putpixel(x1-y,y1+x,6);
putpixel(x1+y,y1-x,6);
putpixel(x1-y,y1-x,6);
while(x<y)
{
k++;
if(p<0)
{
x++;
p+=(2*x)+1;
}
else
{
x++;
y--;
p+=(2*(x-y))+1;
}
if(k%2==0)
{
putpixel(x1+x,y1+y,6);
putpixel(x1-x,y1+y,6);
putpixel(x1+x,y1-y,6);
putpixel(x1-x,y1-y,6);
putpixel(x1+y,y1+x,6);
putpixel(x1-y,y1+x,6);
putpixel(x1+y,y1-x,6);
putpixel(x1-y,y1-x,6);
}
}
}
void circle_dash(int x1, int y1, int r)
{
int x=0,y=r,p=1-r,k=0,m=0;
putpixel(x1+x,y1+y,6);
putpixel(x1-x,y1+y,6);
putpixel(x1+x,y1-y,6);
putpixel(x1-x,y1-y,6);
putpixel(x1+y,y1+x,6);
putpixel(x1-y,y1+x,6);
putpixel(x1+y,y1-x,6);
putpixel(x1-y,y1-x,6);
while(x<y)
{
k++;
if(p<0)
{
x++;
p+=(2*x)+1;
}
else
{
x++;
y--;
p+=(2*(x-y))+1;
}
if(k%5==0)
m++;
if(k%5!=0)
{
if(m%2==0)
{
putpixel(x1+x,y1+y,6);
putpixel(x1-x,y1+y,6);
putpixel(x1+x,y1-y,6);
putpixel(x1-x,y1-y,6);
putpixel(x1+y,y1+x,6);
putpixel(x1-y,y1+x,6);
putpixel(x1+y,y1-x,6);
putpixel(x1-y,y1-x,6);
}
else
{
putpixel(x1+x,y1+y,2);
putpixel(x1-x,y1+y,2);
putpixel(x1+x,y1-y,2);
putpixel(x1-x,y1-y,2);
putpixel(x1+y,y1+x,2);
putpixel(x1-y,y1+x,2);
putpixel(x1+y,y1-x,2);
putpixel(x1-y,y1-x,2);
}
}
}
}
void circle_dashdot(int x1, int y1, int r)
{
int x=0,y=r,p=1-r,k=0,h=2;
putpixel(x1+x,y1+y,6);
putpixel(x1-x,y1+y,6);
putpixel(x1+x,y1-y,6);
putpixel(x1-x,y1-y,6);
putpixel(x1+y,y1+x,6);
putpixel(x1-y,y1+x,6);
putpixel(x1+y,y1-x,6);
putpixel(x1-y,y1-x,6);
while(x<y)
{
k++; h++;
if(p<0)
{
x++;
p+=(2*x)+1;
}
else
{
x++;
y--;
p+=(2*(x-y))+1;
}
if(k%7!=0&&h%7!=0)
{
if((h-1)%7!=0)
{
putpixel(x1+x,y1+y,6);
putpixel(x1-x,y1+y,6);
putpixel(x1+x,y1-y,6);
putpixel(x1-x,y1-y,6);
putpixel(x1+y,y1+x,6);
putpixel(x1-y,y1+x,6);
putpixel(x1+y,y1-x,6);
putpixel(x1-y,y1-x,6);
}
else
{
putpixel(x1+x,y1+y,2);
putpixel(x1-x,y1+y,2);
putpixel(x1+x,y1-y,2);
putpixel(x1-x,y1-y,2);
putpixel(x1+y,y1+x,2);
putpixel(x1-y,y1+x,2);
putpixel(x1+y,y1-x,2);
putpixel(x1-y,y1-x,2);
}
}
}
}
void circle_solidt(int x1, int y1, int r)
{
int x=0,y=r,p=1-r;
putpixel(x1+x,y1+y,6);
putpixel(x1+x+1,y1+y,6);
putpixel(x1+x-1,y1+y,6);
putpixel(x1+x,y1+y+1,6);
putpixel(x1+x,y1+y-1,6);
putpixel(x1-x,y1+y,6);
putpixel(x1-x+1,y1+y,6);
putpixel(x1-x-1,y1+y,6);
putpixel(x1-x,y1+y+1,6);
putpixel(x1-x,y1+y-1,6);
putpixel(x1+x,y1-y,6);
putpixel(x1+x+1,y1-y,6);
putpixel(x1+x-1,y1-y,6);
putpixel(x1+x,y1-y+1,6);
putpixel(x1+x,y1-y-1,6);
putpixel(x1-x,y1-y,6);
putpixel(x1-x+1,y1-y,6);
putpixel(x1-x-1,y1-y,6);
putpixel(x1-x,y1-y+1,6);
putpixel(x1-x,y1-y-1,6);
putpixel(x1+y,y1+x,6);
putpixel(x1+y+1,y1+x,6);
putpixel(x1+y-1,y1+x,6);
putpixel(x1+y,y1+x+1,6);
putpixel(x1+y,y1+x-1,6);
putpixel(x1-y,y1+x,6);
putpixel(x1-y+1,y1+x,6);
putpixel(x1-y-1,y1+x,6);
putpixel(x1-y,y1+x+1,6);
putpixel(x1-y,y1+x-1,6);
putpixel(x1+y,y1-x,6);
putpixel(x1+y+1,y1-x,6);
putpixel(x1+y-1,y1-x,6);
putpixel(x1+y,y1-x+1,6);
putpixel(x1+y,y1-x-1,6);
putpixel(x1-y,y1-x,6);
putpixel(x1-y+1,y1-x,6);
putpixel(x1-y-1,y1-x,6);
putpixel(x1-y,y1-x+1,6);
putpixel(x1-y,y1-x-1,6);
while(x<y)
{
if(p<0)
{
x++;
p+=(2*x)+1;
}
else
{
x++;
y--;
p+=(2*(x-y))+1;
}
putpixel(x1+x,y1+y,6);
putpixel(x1+x+1,y1+y,6);
putpixel(x1+x-1,y1+y,6);
putpixel(x1+x,y1+y+1,6);
putpixel(x1+x,y1+y-1,6);
putpixel(x1-x,y1+y,6);
putpixel(x1-x+1,y1+y,6);
putpixel(x1-x-1,y1+y,6);
putpixel(x1-x,y1+y+1,6);
putpixel(x1-x,y1+y-1,6);
putpixel(x1+x,y1-y,6);
putpixel(x1+x+1,y1-y,6);
putpixel(x1+x-1,y1-y,6);
putpixel(x1+x,y1-y+1,6);
putpixel(x1+x,y1-y-1,6);
putpixel(x1-x,y1-y,6);
putpixel(x1-x+1,y1-y,6);
putpixel(x1-x-1,y1-y,6);
putpixel(x1-x,y1-y+1,6);
putpixel(x1-x,y1-y-1,6);
putpixel(x1+y,y1+x,6);
putpixel(x1+y+1,y1+x,6);
putpixel(x1+y-1,y1+x,6);
putpixel(x1+y,y1+x+1,6);
putpixel(x1+y,y1+x-1,6);
putpixel(x1-y,y1+x,6);
putpixel(x1-y+1,y1+x,6);
putpixel(x1-y-1,y1+x,6);
putpixel(x1-y,y1+x+1,6);
putpixel(x1-y,y1+x-1,6);
putpixel(x1+y,y1-x,6);
putpixel(x1+y+1,y1-x,6);
putpixel(x1+y-1,y1-x,6);
putpixel(x1+y,y1-x+1,6);
putpixel(x1+y,y1-x-1,6);
putpixel(x1-y,y1-x,6);
putpixel(x1-y+1,y1-x,6);
putpixel(x1-y-1,y1-x,6);
putpixel(x1-y,y1-x+1,6);
putpixel(x1-y,y1-x-1,6);
}
}
No comments:
Post a Comment