Vous êtes sur la page 1sur 13

Draw a Circle using Bresenham s Circle Algorithm

#include <stdio.h>
#include <dos.h>
#include <graphics.h>
void plotPoints(int cx, int cy, int x, int y)
{
putpixel(cx+x, cy+y, RED); putpixel(cx-x, cy+y, RED);
putpixel(cx+x, cy-y, RED); putpixel(cx-x, cy-y, RED);
putpixel(cx+y, cy+x, RED); putpixel(cx-y, cy+x, RED);
putpixel(cx+y, cy-x, RED);putpixel(cx-y, cy-x, RED);
}
void main()
{
int cx, cy, x = 0, y, r, p;
int gd = DETECT, gm;
clrscr();
printf("Enter the coordinates of centre of the circle: ");
scanf("%d %d", &cx, &cy);
printf("Enter radius of : ");
scanf("%d", &r);
y = r;
p = 3 - 2 * r;
initgraph(&gd, &gm, "");
cleardevice();
while (x < y)
{
plotPoints(cx, cy, x, y);
x++;
if (p < 0)
p = p + 4 * x + 6;
else
{
y--;
p = p + 4 * (x - y) + 10;
}
plotPoints(cx, cy, x, y);
delay(200);
}
getch();
}

2D Transformations: Translations, Scaling, Rotation


#include<graphics.h>
#include<stdio.h>
#include<conio.h>
void translation( int figure[], int edges, int dx, int dy )
{
for(int i=0; i < edges; i++)
{
figure[2*i] += dx;
figure[2*i+1] += dy;
}
}
void main()
{
int figure[20], edges, dx, dy; // A Figure with Max 10 edges.
int gd = DETECT, gm;

clrscr();
printf( "Number of edges: " );
scanf( "%d", &edges );
for(int i=0; i < edges; i++)
{
printf( "Enter edge (x%d,y%d) : ", i , i );
scanf( "%d %d", &figure[2*i], &figure[2*i+1] );
}
figure[2*i] = figure[0];
figure[2*i+1] = figure[1];
edges += 1;
printf( "Enter dx: ");
scanf( "%d", &dx);
printf( "Enter dy: ");
scanf( "%d", &dy);
initgraph( &gd, &gm, "" );
cleardevice();
drawpoly( edges, figure );
getch();
translation(figure,edges,dx,dy);
setcolor(RED);
drawpoly( edges, figure );
getch();
}
void translation( int figure[], int edges, int dx, int dy )
{
for(int i=0; i < edges; i++)
{
figure[2*i] += dx;
figure[2*i+1] += dy;
}
}

#include<graphics.h>
#include<stdio.h>
#include<conio.h>
void scale( int figure[], int edges, int dx, int dy, int cx, int cy )
{
for(int i=0; i < edges; i++)
{
figure[2*i] = (figure[2*i] - cx) * dx + cx;
figure[2*i+1] = (figure[2*i+1] - cy) * dy + cy;
}
}
void main()
{
int figure[20], edges; // A Figure with Max 10 edges.
int dx, dy, cx=0, cy=0;
int gd = DETECT, gm;

clrscr();
printf( "Number of edges: " );
scanf( "%d", &edges );
for(int i=0; i < edges; i++)
{
printf( "Enter edge (x%d,y%d) : ", i , i );
scanf( "%d %d", &figure[2*i], &figure[2*i+1] );
}
figure[2*i] = figure[0];
figure[2*i+1] = figure[1];
edges += 1;
printf( "Enter dx: ");
scanf( "%d", &dx);
printf( "Enter dy: ");
scanf( "%d", &dy);
printf( "Enter the center of scaling: \n");
printf( "cx: ");
scanf( "%d", &cx);
printf( "cy: ");
scanf( "%d", &cy);
initgraph( &gd, &gm, "" );
cleardevice();
setbkcolor(WHITE);
setcolor(GREEN);
setlinestyle(SOLID_LINE, 0, 3);
drawpoly( edges, figure );
getch();
scale(figure,edges,dx,dy,cx,cy);
setcolor(RED);
drawpoly( edges, figure );
getch();
}
void scale( int figure[], int edges, int dx, int dy, int cx, int cy )
{
for(int i=0; i < edges; i++)
{
figure[2*i] = (figure[2*i] - cx) * dx + cx;
figure[2*i+1] = (figure[2*i+1] - cy) * dy + cy;
}
}
#include<graphics.h>
#include<stdio.h>
#include<conio.h>
#include<math.h>
void rotate( int figure[], int edges, double angle, int cx, int cy )
{
double x, y;
angle = -1 * (angle*3.14/180);

double cos_a = cos(angle);


double sin_a = sin(angle);
for(int i=0; i < edges; i++)
{
x = figure[2*i] - cx;
y = figure[2*i+1] - cy;
figure[2*i] = ceil( (x * cos_a) - (y * sin_a) + cx );
figure[2*i+1] = ceil( (x * sin_a)+(y * cos_a) + cy );
}
}
void main()
{
int figure[20], edges; // A Figure with Max 10 edges.
double angle;
int cx=0, cy=0;
int gd = DETECT, gm;
initgraph( &gd, &gm, "" );
int max_y = getmaxy();
clrscr();
cleardevice();
printf( "Number of edges: " );
scanf( "%d", &edges );
for(int i=0; i < edges; i++)
{
printf( "Enter edge (x%d,y%d) : ", i , i );
scanf( "%d %d", &figure[2*i], &figure[2*i+1] );
}
figure[2*i] = figure[0];
figure[2*i+1] = figure[1];
edges += 1;
printf( "Enter angle of rotation in degrees: ");
scanf( "%lf", &angle);
printf( "Enter the center of rotation: \n");
printf( "cx: ");
scanf( "%d", &cx);
printf( "cy: ");
scanf( "%d", &cy);
cy = max_y - cy;
cleardevice();
setbkcolor(WHITE);
setcolor(GREEN);
setlinestyle(SOLID_LINE, 0, 3);
drawpoly( edges, figure );
getch();
for(int i=0; i < edges; i++)
figure[2*i+1] = max_y - figure[2*i+1];
rotate(figure,edges,angle,cx,cy);
for(int i=0; i < edges; i++)
figure[2*i+1] = max_y - figure[2*i+1];
setcolor(RED);

drawpoly( edges, figure );


getch();
}
void rotate( int figure[], int edges, double angle, int cx, int cy )
{
double x, y;
angle = -1 * (angle*3.14/180);
double cos_a = cos(angle);
double sin_a = sin(angle);
for(int i=0; i < edges; i++)
{
x = figure[2*i] - cx;
y = figure[2*i+1] - cy;
figure[2*i] = floor( (x * cos_a) - (y * sin_a) + cx + 0.5 );
figure[2*i+1] = floor( (x * sin_a)+(y * cos_a) + cy + 0.5 );
}
}

C Program for COHEN-SUTHERLAND 2D LINE CLIPPING ALGORITHM


To identify the portion of line that is inside or outside a specified region
The region against which a line is to be clipped is called clipping window. The
portion of lines inside the clipping window are retain for display.
ALGORITHM:
1.
Start
2.
Read two end points of a line P1,P2 (x1,y1) and (x2,y2)
3.
Read two corners of window(wx1,wy1) and (wx2,wy2)
4.
Assign the region codes for two end points using following steps
a.
Set bit 1 if x < wx1
b.
Set bit 2 if x>wx2
c.
Set bit 3 if y<wy2
d.
Set bit 4 if y> wy1
5.
Check for the visibility of the line
a.
If region codes for both end points are 0 then the line is completely v
isible. Draw the line. Go to 10
b.
If the region codes are not zero and the logical AND ing of them is also
non zero then the line is completely invisible. Go to 10
c.
If the region codes for two end points do not satisfy the conditions in
5 a and 5 b the line is partially visible
6.
Determine the intersecting edge of the clipping window by inspect
ing the region codes of two end points
a.
If the region codes foe both end points are non zero find the intersect
ion point P1 and P2 with boundary edges of clipping window with respect to P1 and
P2 respectively
b.
If the region code for any one end point is non zero then find intersect
ion point P1 or P2 with the boundary edge of the clipping window with respect to i
t.
7.
Divide the line segments considering the intersection points
8.
Reject the line segment if any one end point of it appears outside the c
lipping window
9.
Draw the remaining line segments
10. Stop.
PROGRAM:

#include<stdio.h>
#include<conio.h>
#include<dos.h>
#include<graphics.h>
#include<stdlib.h>
typedef struct coord
{
int x,y;
char code[4];
}PT;
void drawwindow();
void drawline(PT p1,PT p2,int c1);
PT setcode(PT p);
int visibility(PT p1,PT p2);
PT resetendpt(PT p1,PT p2);
void main()
{
int gd=DETECT,gm,v;
PT p1,p2,ptemp;
initgraph(&gd,&gm,"");
cleardevice();
printf("\t\tCOHEN-SUTHERLAND LINE CLIPPING ALGORITHM");
printf("\nEnter the two end points p1(x,y)");
scanf("%d%d",&p1.x,&p1.y);
printf("Enter the two end points p2(x,y):");
scanf("%d%d",&p2.x,&p2.y);
cleardevice();
printf("\t\tCLIPPING WINDOW");
drawwindow();
getch();
printf("\tCOHEN-SUTHERLAND LINE CLIPPING ALGORITHM");
printf("\n\t\tBEFORE CLIPPING");
drawline(p1,p2,4);
getch();
p1=setcode(p1);
p2=setcode(p2);
v=visibility(p1,p2);
switch(v)
{
case 0:
cleardevice();
//printf("\t\tCLIPPING WINDOW");
drawwindow();
drawline(p1,p2,15);
break;
case 1:
cleardevice();
drawwindow();
break;
case 2:
cleardevice();
p1=resetendpt(p1,p2);
p2=resetendpt(p2,p1);
drawwindow();
printf("\tCOHEN-SUTHERLAND LINE CLIPPING ALGORITHM");
printf("\n\t\tAFTER CLIPPING");
drawline(p1,p2,15);
break;
}

getch();
closegraph();
//return(0);
}
void drawwindow()
{
setcolor(RED);
setlinestyle(DOTTED_LINE, 1, 1);
line(150,100,100,100);
line(150,100,150,50);
line(450,100,500,100);
line(450,100,450,50);
line(450,350,500,350);
line(450,350,450,400);
line(150,350,150,400);
line(150,350,100,350) ;
line(150,100,100,100);
line(150,100,150,50);
setlinestyle(SOLID_LINE, 1, 1);
line(150,100,450,100);
line(450,100,450,350);
line(450,350,150,350);
line(150,350,150,100);
outtextxy(450,30,"1001");
outtextxy(470,200,"1000");
outtextxy(470,370,"1010");
outtextxy(300,370,"0010");
outtextxy(120,370,"0110");
outtextxy(120,200,"0100");
outtextxy(120,30,"0101");
outtextxy(300,30,"0001");
outtextxy(300,200,"0000");
}
void drawline(PT p1,PT p2,int c1)
{
setcolor(c1);
line(p1.x,p1.y,p2.x,p2.y);
}
PT setcode(PT p)
{
PT ptemp;
if(p.y<100)
ptemp.code[0]='1';
else
ptemp.code[0]='0';
if(p.y>350)
ptemp.code[1]='1';
else
ptemp.code[1]='0';
if(p.x>200)
ptemp.code[2]='1';
else
ptemp.code[2]='0';
if(p.x<150)
ptemp.code[3]='1';
else
ptemp.code[3]='0';
ptemp.x=p.x;

ptemp.y=p.y;
return(ptemp);
}
int visibility(PT p1,PT p2)
{
int i,flag=0;
for(i=0;i<4;i++)
{
if((p1.code[i]!='0')||(p2.code[i]!='0'))
flag=1;
}
if(flag==0)
return(0);
for(i=0;i<4;i++)
{
if((p1.code[i]==p2.code[i])&&(p1.code[i]=='1'))
flag=0;
}
if(flag==0)
return(1);
return(2);
}
PT resetendpt(PT p1,PT p2)
{
PT temp;
int x,y,i;
float m,k;
if(p1.code[3]=='1')
x=150;
if(p1.code[2]=='1')
x=450;
if((p1.code[3]=='1')||(p1.code[2]=='1'))
{
m=(float)(p2.y-p1.y)/(p2.x-p1.x);
k=(p1.y+(m*(x-p1.x)));
temp.y=k;
temp.x=x;
for(i=0;i<4;i++)
{
temp.code[i]=p1.code[i];
if(temp.y<=350 && temp.y>=100)
return(temp);
}
if(p1.code[0]=='1')
y=100;
if(p1.code[1]=='1')
y=350;
if((p1.code[0]=='1')||(p1.code[1]=='1'))
{
m=(float)(p2.y-p1.y)/(p2.x-p1.x);
k=(float)p1.x+(float)(y-p1.y)/m;
temp.y=y;
temp.x=k;
for(i=0;i<4;i++)
temp.code[i]=p1.code[i];
return(temp);
}
}
else
return(p1);

return(p2);
}

Cohen Sutherland line clipping algorithm in c


The Cohen-Sutherland algorithm is a computer graphics algorithm used for line cl
ipping. The algorithm divides a two-dimensional space into 9 regions (or a three
-dimensional space into 27 regions), and then efficiently determines the lines a
nd portions of lines that are visible in the center region of interest (the view
port).The Cohen Sutherland algorithm can be used only on a rectangular clipping ar
ea.
IMPLEMENTATION IN C
#include<stdio.h>
#include<graphics.h>
#include<conio.h>
typedef unsigned int outcode;
enum { TOP=0x1, BOTTOM=0x2, RIGHT=0x4, LEFT=0x8 };
void lineclip(x0,y0,x1,y1,xwmin,ywmin,xwmax,ywmax )
float x0,y0,x1,y1,xwmin,ywmin,xwmax,ywmax;
{
int gd,gm;
outcode code0,code1,codeout;
int accept = 0, done=0;
code0 = calcode(x0,y0,xwmin,ywmin,xwmax,ywmax);
code1 = calcode(x1,y1,xwmin,ywmin,xwmax,ywmax);
do{
if(!(code0 | code1))
{ accept =1 ; done =1; }
else
if(code0 & code1) done = 1;
else
{
float x,y;
codeout = code0 ? code0 : code1;
if(codeout & TOP)
{
x = x0 + (x1-x0)*(ywmax-y0)/(y1-y0);
y = ywmax;
}
else
if( codeout & BOTTOM)

{
x = x0 + (x1-x0)*(ywmin-y0)/(y1-y0);
y = ywmin;
}
else
if ( codeout & RIGHT)
{
y = y0+(y1-y0)*(xwmax-x0)/(x1-x0);
x = xwmax;
}
else
{
y = y0 + (y1-y0)*(xwmin-x0)/(x1-x0);
x = xwmin;
}
if( codeout == code0)
{
x0 = x; y0 = y;
code0=calcode(x0,y0,xwmin,ywmin,xwmax,ywmax);
}
else
{
x1 = x; y1 = y;
code1 = calcode(x1,y1,xwmin,ywmin,xwmax,ywmax);
}
}
} while( done == 0);
if(accept) line(x0,y0,x1,y1);
rectangle(xwmin,ywmin,xwmax,ywmax);
getch();
}
int calcode (x,y,xwmin,ywmin,xwmax,ywmax)
float x,y,xwmin,ywmin,xwmax,ywmax;
{
int code =0;
if(y> ywmax)
code |=TOP;
else if( y<ywmin)
code |= BOTTOM;
else if(x > xwmax)
code |= RIGHT;
else if ( x< xwmin)
code |= LEFT;
return(code);
}
main()
{
float x2,y2,x1,y1,xwmin,ywmin,xwmax,ywmax;
int gd=DETECT,gm;

clrscr();
initgraph(&gd,&gm,"e:\\tc\\bgi");
printf("\n\n\tEnter the co-ordinates of Line :");
printf("\n\n\tX1 Y1 : ");
scanf("%f %f",&x1,&y1);
printf("\n\n\tX2 Y2 : ");
scanf("%f %f",&x2,&y2);
printf("\n\tEnter the co_ordinates of window :\n ");
printf("\n\txwmin , ywmin : ");
scanf("%f %f",&xwmin,&ywmin);
printf("\n\txwmax , ywmax : ");
scanf("%f %f",&xwmax,&ywmax);
clrscr();
line(x1,y1,x2,y2);
rectangle(xwmin,ywmin,xwmax,ywmax);
getch();
clrscr();
lineclip(x1,y1,x2,y2,xwmin,ywmin,xwmax,ywmax );
getch();
closegraph();
}

Midpoint Circle Algorithm


#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm;
int x,y,r;
void Drawcircle(int,int,int);
printf("Enter the Mid points and Radious:");
scanf("%d%d%d",&x,&y,&r);
initgraph(&gd,&gm,"");
Drawcircle(x,y,r);
getch();
closegraph();
}
void Drawcircle(int x1,int y1,int r)
{
int x=0,y=r,p=1-r;
void cliplot(int,int,int,int);
cliplot(x1,y1,x,y);
while(x<y)
{
x++;

if(p<0)
p+=2*x+1;
else
{
y--;
p+=2*(x-y)+1;
}
cliplot(x1,y1,x,y);
}
}
void cliplot(int xctr,int yctr,int x,int y)
{
putpixel(xctr +x,yctr +y,1);
putpixel(xctr -x,yctr +y,1);
putpixel(xctr +x,yctr -y,1);
putpixel(xctr -x,yctr -y,1);
putpixel(xctr +y,yctr +x,1);
putpixel(xctr -y,yctr +x,1);
putpixel(xctr +y,yctr -x,1);
putpixel(xctr -y,yctr -x,1);
getch
---------------#include<graphics.h>
#include<conio.h>
#include<stdio.h>
void main()
{
int gd=DETECT,gm;
int i,r,x,y,xc,yc;
float d;
clrscr();
initgraph(&gd,&gm,"c:\\tc\\");
printf("Enter Radius\n");
scanf("%d",&r);
printf("Enter Center of circle\n");
scanf("%d",&xc);
scanf("%d",&yc);
d=1.25-r;
x=0;
y=r;
do
{
if(d<0)
{
x=x+1;
d=d+2*x+1;
}
else
{
x=x+1;
y=y-1;
d=d+2*x-2*y+10;
}
putpixel(xc+x,yc+y,5);

putpixel(xc-y,yc-x,5);
putpixel(xc+y,yc-x,5);
putpixel(xc-y,yc+x,5);
putpixel(xc+y,yc+x,5);
putpixel(xc-x,yc-y,5);
putpixel(xc+x,yc-y,5);
putpixel(xc-x,yc+y,5);
}
while(x<y);
getch();
}

Vous aimerez peut-être aussi