Graphics Programming Using GCC
Color Filling Recursive
#include<stdio.h>
#include<graphics.h>
#define ROUND(a) ((int)(a+0.5))
int main()
{ int gd=DETECT,gm=VGAMAX;
initgraph(&gd,&gm,NULL);
void circleMidpoint(int,int,int);
void ellipseMid(int,int,int,int);
void lineDDA(int,int,int,int);
void boundaryFill(int,int,int,int);
int xc,yc,r,i,opt1,opt2,xf,yf,n;
int x1,y1,x2,y2;
char c,f;
do
{
printf("Menu\n1.Polygon\n2.Circle\n3.Ellipse\n");
scanf("%d",&opt1);
switch(opt1)
{
case 1:
printf("Enter no of sides of polygon:");
scanf("%d",&n);
printf("Enter line cordinates(x,y):");
scanf("%d%d",&x1,&y1);
for(i=0;i<n;i++)
{
printf("Enter line cordinates(x,y):");
scanf("%d%d",&x2,&y2);
lineDDA(x1,y1,x2,y2);
x1=x2,y1=y2;
}
printf("Menu\n1.Boundary Fill\n2.Flood Fill\n");
scanf("%d",&opt2);
switch(opt2)
{
case 1:
printf("Enter inside point(x,y):");
scanf("%d%d",&x2,&y2);
boundaryFill(x2,y2,8,10);
break;
case 2:
break;
}
break;
case 2:
circleMidpoint(300,200,100);
//printf("Enter (x,y) in circle:");
//scanf("%d%d",&xf,&yf);
boundaryFill(300,200,3,10);
break;
case 3:
ellipseMid(400,300,200,100);
boundaryFill(400,250,3,10);
break;
default:
printf("Invalid");
}
printf("Do u want to continue(Y/N):");
scanf("%c",&c);
}while(c=='y'||c=='Y');
return 0;
}
void circleMidpoint(int xcenter,int ycenter,int radius)
{
int x=0,y=radius,p=1-radius;
void circlePlotPoints(int,int,int,int);
circlePlotPoints(xcenter,ycenter,x,y);
while(x<y)
{
x++;
if(p<0)
p+=(2*x)+1;
else
{
y--;
p+=2*(x-y)+1;
}
circlePlotPoints(xcenter,ycenter,x,y);
}
}
void circlePlotPoints(int xcenter,int ycenter,int x,int y)
{
putpixel(xcenter+x,ycenter+y,10);
putpixel(xcenter-x,ycenter+y,10);
putpixel(xcenter+x,ycenter-y,10);
putpixel(xcenter-x,ycenter-y,10);
putpixel(xcenter+y,ycenter+x,10);
putpixel(xcenter-y,ycenter+x,10);
putpixel(xcenter+y,ycenter-x,10);
putpixel(xcenter-y,ycenter-x,10);
}
void ellipseMid(int xcenter,int ycenter,int Rx,int Ry)
{
int Rx2=Rx*Rx;
int Ry2=Ry*Ry;
int twoRx2=2*Rx2;
int twoRy2=2*Ry2;
int p;
int x=0;
int y=Ry;
int px=0;
int py=twoRx2*y;
void ellipsePlotPoints(int,int,int,int);
ellipsePlotPoints(xcenter,ycenter,x,y);
p=ROUND(Ry2-(Rx2*Ry)+(0.25*Rx2));
while(px<py)
{
x++;
px+=twoRy2;
if(p<0)
p+=Ry2+px;
else
{
y--;
py-=twoRx2;
p+=Ry2+px-py;
}
ellipsePlotPoints(xcenter,ycenter,x,y);
}
p=ROUND(Ry2*(x+0.5)*(x+0.5)+Rx2*(y-1)*(y-1)-Rx2*Ry2);
while(y>0)
{
y--;
py-=twoRx2;
if(p>0)
p+=Rx2-py;
else
{
x++;
px+=twoRy2;
p+=Rx2-py+px;
}
ellipsePlotPoints(xcenter,ycenter,x,y);
}
}
void ellipsePlotPoints(int xcenter,int ycenter,int x,int y)
{
putpixel(xcenter+x,ycenter+y,10);
putpixel(xcenter-x,ycenter+y,10);
putpixel(xcenter+x,ycenter-y,10);
putpixel(xcenter-x,ycenter-y,10);
}
void lineDDA(int xa,int ya,int xb,int yb)
{
int dx=xb-xa,dy=yb-ya,steps,k;
float xincrement,yincrement,x=xa,y=ya;
if(abs(dx)>abs(dy))
steps=abs(dx);
else
steps=abs(dy);
xincrement=dx/(float)steps;
yincrement=dy/(float)steps;
putpixel(ROUND(x),ROUND(y),10);
for(k=0;k<steps;k++)
{
x+=xincrement;
y+=yincrement;
putpixel(ROUND(x),ROUND(y),10);
}
}
void boundaryFill(int x,int y,int fill,int boundary)
{
int current;
current=getpixel(x,y);
if((current!=boundary)&&(current!=fill))
{
setcolor(fill);
putpixel(x,y,fill);
boundaryFill(x+1,y,fill,boundary);
boundaryFill(x-1,y,fill,boundary);
boundaryFill(x,y+1,fill,boundary);
boundaryFill(x,y-1,fill,boundary);
}
}
Circle Drawing
#include<stdio.h>
#include<graphics.h>
int main()
{ int gd=DETECT,gm=VGAMAX;
initgraph(&gd,&gm,NULL);
void circleMidpoint(int,int,int);
void circlePara(int,int,int);
int xc,yc,r,i,opt;
char c,f;
do
{
printf("Enter xc,yc,r:");
scanf("%d%d%d",&xc,&yc,&r);
printf("Enter choice:");
scanf("%d",&opt);
switch(opt)
{
case 1:
circleMidpoint(xc,yc,r);
break;
case 2:
circlePara(xc,yc,r);
break;
default:
printf("Invalid");
}
printf("Do u want to continue(Y/N):");
scanf("%c",&c);
}while(c=='y'||c=='Y');
return 0;
}
void circleMidpoint(int xcenter,int ycenter,int radius)
{
int x=0,y=radius,p=1-radius;
void circlePlotPoints(int,int,int,int);
circlePlotPoints(xcenter,ycenter,x,y);
while(x<y)
{
x++;
if(p<0)
p+=(2*x)+1;
else
{
y--;
p+=2*(x-y)+1;
}
circlePlotPoints(xcenter,ycenter,x,y);
}
}
void circlePlotPoints(int xcenter,int ycenter,int x,int y)
{
putpixel(xcenter+x,ycenter+y,10);
putpixel(xcenter-x,ycenter+y,10);
putpixel(xcenter+x,ycenter-y,10);
putpixel(xcenter-x,ycenter-y,10);
putpixel(xcenter+y,ycenter+x,10);
putpixel(xcenter-y,ycenter+x,10);
putpixel(xcenter+y,ycenter-x,10);
putpixel(xcenter-y,ycenter-x,10);
}
void circlePara(int xc,int yc,int r)
{
int x,y,i=0;
while(i<=90)
{
x=xc+r*cos(i);
y=yc+r*sin(i);
putpixel(x,y,10);
i++;
}
}
Line Drawing
#include<stdio.h>
#include<graphics.h>
#define ROUND(a) ((int)(a+0.5))
int main()
{
int gd=DETECT,gm=VGAMAX;
initgraph(&gd,&gm,NULL);
int x1,y1,x2,y2,opt;
char c;
void lineDDA(int,int,int,int);
void lineBres(int,int,int,int);
do
{
printf("Enter 1st cordinate(x1,y1):");
scanf("%d%d",&x1,&y1);
printf("Enter 2nd cordinate(x2,y2):");
scanf("%d%d",&x2,&y2);
printf("Enter choice:");
scanf("%d",&opt);
switch(opt)
{
case 1:
lineDDA(x1,y1,x2,y2);
break;
case 2:
lineBres(x1,y1,x2,y2);
break;
}
printf("Do u want to continue:(Y/N):");
scanf("%c",&c);
}while(c=='y'||c=='Y');
return 0;
}
void lineDDA(int xa,int ya,int xb,int yb)
{
int dx=xb-xa,dy=yb-ya,steps,k;
float xincrement,yincrement,x=xa,y=ya;
if(abs(dx)>abs(dy))
steps=abs(dx);
else
steps=abs(dy);
xincrement=dx/(float)steps;
yincrement=dy/(float)steps;
putpixel(ROUND(x),ROUND(y),10);
for(k=0;k<steps;k++)
{
x+=xincrement;
y+=yincrement;
putpixel(ROUND(x),ROUND(y),10);
}
}
void lineBres(int xa,int ya,int xb,int yb)
{
int dx=abs(xa-xb),dy=abs(ya-yb);
int p=2*dy-dx;
int twoDy=2*dy,twoDyDx=2*(dy-dx);
int x,y,xEnd;
if(xa>xb)
{
x=xb;
y=yb;
xEnd=xa;
}
else
{
x=xa;
y=ya;
xEnd=xb;
}
putpixel(x,y,10);
while(x<xEnd)
{
x++;
if(p<0)
p+=twoDy;
else
{
y++;
p+=twoDyDx;
}
putpixel(x,y,10);
}
}
No comments:
Post a Comment