Algo Graphics
Algo Graphics
5)) void lineDDA(int xa,int xb,int ya,int yb) { int dx=xb-xa,dy=yb-ya; int steps,k; float xincr,yincr,x,y; x=xa; y=ya; if(abs(dx)>abs(dy)) steps=abs(dx); else steps=abs(dy); xincr=dx/float(steps); yincr=dy/float(steps); putpixel(ROUND(x), ROUND(y),7); for(k=0;k<steps;k++) { x+=xincr; y+=yincr; putpixel(ROUND(x),ROUND(y),7); } }
void main() { int gdriver = DETECT, gmode, errorcode; initgraph(&gdriver, &gmode, ""); printf("Enter the cordinates"); int x1,y1,x2,y2; scanf("%d%d%d%d",&x1,&y1,&x2,&y2); lineDDA(x1,x2,y1,y2); getch(); }
.a
l g
s a
m e
.c
m o
.a
l g
s a
m e
.c
m o
PROGRAM TO IMPLEMENT BRESENHAMS LINE ALGORITHM #include<conio.h> #include<stdio.h> #include<graphics.h> #include<math.h> #include<iostream.h> void main() { void draw_line(int x1,int y1,int x2,int y2); int gdriver = DETECT, gmode, errorcode; int x1,x2,y1,y2; /* initialize graphics mode */ initgraph(&gdriver, &gmode, ""); cout<<"enter x1,y1\n"; cin>>x1>>y1; cout<<"enter x2,y2\n"; cin>>x2>>y2; draw_line(x1,y1,x2,y2); getch(); } void draw_line(int x1,int y1,int x2,int y2) { int dx,dy,xa,xb,ya,yb,p; dx=abs(x2-x1); dy=abs(y2-y1); if(x1<x2) { xa=x1;ya=y1;xb=x2;yb=y2; } else { xa=x2;ya=y2;xb=x1;yb=y1; } p=(2*dy)-dx; while(xa<=xb) { if(p<0) { putpixel(++xa,490-ya,8); p=p+(2*dy); }
.a
l g
s a
m e
.c
m o
.a
l g
s a
m e
.c
m o
PROGRAM TO IMPLEMENT MID POINT CIRCLE DRAWING ALGORITHM #include<iostream.h> #include<stdio.h> #include<math.h> #include<dos.h> #include<process.h> #include<graphics.h> #include<conio.h> #define round(x) int(x+0.5) void circlepoint(int cx, int cy, int x, int y) { putpixel(x+cx,y+cy,1); putpixel(x+cx,-y+cy,1); putpixel(-x+cx,y+cy,1); putpixel(-x+cx,-y+cy,1); putpixel(y+cx,x+cy,1); putpixel(y+cx,-x+cy,1); putpixel(-y+cx,x+cy,1); putpixel(-y+cx,-x+cy,1); } void circle1(int cx,int cy,int r) { int x,y; x=0; y=r; circlepoint(cx, cy, x,y); int p=1-r; int i; while((x+cx)<(y+cy)) { if(p>0) { x++; y--; p=p+2*(x-y)+1; } else { x++; p=p+2*x+1; } circlepoint(cx, cy, x,y); } getch();
.a
l g
s a
m e
.c
m o
} void main() { int gdriver = DETECT, gmode, errorcode; initgraph(&gdriver, &gmode, ""); clrscr(); int r,cx,cy,current; cout<<" Enter the radius: "; cin>>r; cout<<" Enter the (x,y) coordinates of the centre: "; cin>>cx>>cy; circle1(cx,cy,r); closegraph(); }
.a
l g
s a
m e
.c
m o
PROGRAM TO DRAW A FILLED CIRCLE #include<iostream> #include<stdio.h> #include<math.h> #include<dos.h> #include<process.h> #include<graphics.h> #include<conio.h> using namespace std; #define round(x) int(x+0.5) void lineDDA(int xa,int xb,int ya,int yb) { int dx=xb-xa,dy=yb-ya; int steps,k; float xincr,yincr,x,y; x=xa; y=ya; if(abs(dx)>abs(dy)) steps=abs(dx); else steps=abs(dy); xincr=dx/float(steps); yincr=dy/float(steps); putpixel(round(x), round(y),7);
void circlepoint(int cx, int cy, int x, int y) { putpixel(x+cx,y+cy,7); putpixel(x+cx,-y+cy,7); putpixel(-x+cx,y+cy,7); putpixel(-x+cx,-y+cy,7); putpixel(y+cx,x+cy,7); putpixel(y+cx,-x+cy,7); putpixel(-y+cx,x+cy,7); putpixel(-y+cx,-x+cy,7); lineDDA(cx-x,cx+x,cy+y,y+cx); lineDDA(cx-x,cx+x,cy-y,cy-y); lineDDA(cy-y,cy+y,cx+x,cx+x); lineDDA(cy-y,cy+y,cx-x,cx-x);
.a
l g
s a
m e
.c
m o
} void circle1(int cx,int cy,int r) { int x,y; x=0; y=r; circlepoint(cx, cy, x,y); int p=1-r; int i; while((x+cx)<(y+cy)) { if(p>0) { x++; y--; p=p+2*(x-y)+1; } else { x++; p=p+2*x+1; } circlepoint(cx, cy, x,y); } getch(); }
int main() { initwindow(800,600); int r,cx,cy,current; cout<<" Enter the radius: "; cin>>r; cout<<" Enter the (x,y) coordinates of the centre: "; cin>>cx>>cy; circle1(cx,cy,r);
.a
l g
s a
m e
.c
m o
.a
l g
s a
m e
.c
m o
PROGRAM TO IMPLEMENT MIDPOINT ELLIPSE ALGORITHM #include<stdio.h> #include<iostream> #include<math.h> #include<graphics.h> #include<conio.h> using namespace std; void display(float x1,float y1, float x, float y); void midpofloatellipse(float xc, float yc, float rx, float ry) { float x,y; float p, m; x=(float)0; y=(float)ry; p=ry*ry-rx*rx*ry-rx/4; m=-(x*ry*ry)/(y*rx*rx); while(abs((int)(m))<1) { display(xc,yc,x,y); if(p<0) { p+=ry*ry*(2*x+3); } else { p+=ry*ry*(2*x+3)+rx*rx*(-2*y+2); y--;; } x++; m=-(x*ry*ry)/(y*rx*rx); }
.a
l g
s a
m e
.c
m o
y--; } } void display(float xc,float yc, float x, float y) { putpixel(xc+x,yc+y,11); putpixel(xc+x,yc-y,11); putpixel(xc-x,yc+y,11); putpixel(xc-x,yc-y,11); } int main() { initwindow(400,400); int xc,yc,rx,ry; cout<<"Enter co ordinates of the centre\n"; cin>>xc>>yc; cout<<"Enter the major and minor axes of the ellipse\n"; cin>>rx>>ry; midpofloatellipse(xc,yc,rx,ry); while(!kbhit()); closegraph(); return 0; getch(); closegraph(); }
.a
l g
s a
m e
.c
m o
PROGRAM TO DRAW FILLED ELLIPSE #include<stdio.h> #include<iostream> #include<math.h> #include<graphics.h> #include<conio.h> #define round(x)((int)(x+0.5)) using namespace std; void display(float x1,float y1, float x, float y); void lineDDA(int xa,int xb,int ya,int yb) { int dx=xb-xa,dy=yb-ya; int steps,k; float xincr,yincr,x,y; x=xa; y=ya; if(abs(dx)>abs(dy)) steps=abs(dx); else steps=abs(dy); xincr=dx/float(steps); yincr=dy/float(steps); putpixel(round(x), round(y),7);
void midpofloatellipse(float xc, float yc, float rx, float ry) { float x,y; float p, m; x=(float)0; y=(float)ry; p=ry*ry-rx*rx*ry-rx/4; m=-(x*ry*ry)/(y*rx*rx); while(abs((int)(m))<1) { display(xc,yc,x,y);
.a
l g
s a
m e
.c
m o
if(p<0) { p+=ry*ry*(2*x+3); } else { p+=ry*ry*(2*x+3)+rx*rx*(-2*y+2); y--;; } x++; m=-(x*ry*ry)/(y*rx*rx); } p=(x+1/2)*(x+1/2)*ry*ry+(y-1)*(y-1)*rx*rx-rx*rx*ry*ry; while(y>=0) { display(xc,yc,x,y); if(p<0) { x++; p+=ry*ry*(2*x+2)+rx*rx*(-2*y+3); } else { p+=rx*rx*(-2*y+3); } y--; } } void display(float xc,float yc, float x, float y) { putpixel(xc+x,yc+y,11); putpixel(xc+x,yc-y,11); putpixel(xc-x,yc+y,11); putpixel(xc-x,yc-y,11); lineDDA(xc-x,xc+x,yc+y,yc+y); lineDDA(xc-x,xc+x,yc-y,yc-y); }
.a
l g
s a
m e
.c
m o
int main() { initwindow(300,300); int xc,yc,rx,ry; cout<<"Enter co ordinates of the centre\n"; cin>>xc>>yc; cout<<"Enter the major and minor axes of the ellipse\n"; cin>>rx>>ry; midpofloatellipse(xc,yc,rx,ry);
.a
l g
s a
m e
.c
m o
#include<iostream> #include<stdio.h> #include<math.h> #include<dos.h> #include<process.h> #include<graphics.h> #include<conio.h> using namespace std; #define round(x) int(x+0.5) void circlepoint(int cx, int cy, int x, int y) { putpixel(x+cx,y+cy,7); putpixel(x+cx,-y+cy,7); putpixel(-x+cx,y+cy,7); putpixel(-x+cx,-y+cy,7); putpixel(y+cx,x+cy,7); putpixel(y+cx,-x+cy,7); putpixel(-y+cx,x+cy,7); putpixel(-y+cx,-x+cy,7); } void floodfill(int x,int y,int fill,int old) { if(getpixel(x,y)==old) { putpixel(x,y,fill); floodfill(x+1,y,fill,old); floodfill(x-1,y,fill,old); floodfill(x,y+1,fill,old); floodfill(x,y-1,fill,old); } }
.a
l g
s a
m e
.c
m o
void circle1(int cx,int cy,int r) { int x,y; x=0; y=r; circlepoint(cx, cy, x,y); int p=1-r; int i; while((x+cx)<(y+cy)) {
if(p>0) { x++; y--; p=p+2*(x-y)+1; } else { x++; p=p+2*x+1; } circlepoint(cx, cy, x,y); } floodfill(cx,cy,11,0); getch(); } int main() { int r,cx,cy,current; initwindow(200,200); cout<<" Enter the radius: "; cin>>r; cout<<" Enter the (x,y) coordinates of the centre: "; cin>>cx>>cy; circle1(cx,cy,r); while(!kbhit()); closegraph(); return 0; getch(); }
.a
l g
s a
m e
.c
m o
PROGRAM TO IMPLEMENT BOUNDARY FILL ALGO #include<iostream> #include<stdio.h> #include<math.h> #include<dos.h> #include<process.h> #include<graphics.h> #include<conio.h> using namespace std; #define round(x) int(x+0.5) void circlepoint(int cx, int cy, int x, int y) { putpixel(x+cx,y+cy,7); putpixel(x+cx,-y+cy,7); putpixel(-x+cx,y+cy,7); putpixel(-x+cx,-y+cy,7); putpixel(y+cx,x+cy,7); putpixel(y+cx,-x+cy,7); putpixel(-y+cx,x+cy,7); putpixel(-y+cx,-x+cy,7); } void boundaryfill(int x,int y,int fill,int boundary) { int current = getpixel(x,y); if((current!=boundary)&&(current!=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); } }
.a
l g
s a
m e
.c
m o
void circle1(int cx,int cy,int r) { int x,y; x=0; y=r; circlepoint(cx, cy, x,y); int p=1-r; int i; while((x+cx)<(y+cy))
{ if(p>0) { x++; y--; p=p+2*(x-y)+1; } else { x++; p=p+2*x+1; } circlepoint(cx, cy, x,y); } boundaryfill(cx,cy,2,7); getch(); } int main() { int r,cx,cy,current; initwindow(200,200); cout<<" Enter the radius: "; cin>>r; cout<<" Enter the (x,y) coordinates of the centre: "; cin>>cx>>cy; circle1(cx,cy,r); while(!kbhit()); closegraph(); return 0; getch(); }
.a
l g
s a
m e
.c
m o
PROGRAM TO DRAW A THICK LINE #include<conio.h> #include<stdio.h> #include<graphics.h> #include<math.h> #include<iostream.h> #define ROUND(x) ((int)(x+0.5)) void lineDDA(int xa,int xb,int ya,int yb,int th) { int dx=xb-xa,dy=yb-ya; int steps,k; float xincr,yincr,x,y; x=xa; y=ya; if(abs(dx)>abs(dy)) steps=abs(dx); else steps=abs(dy); xincr=dx/float(steps); yincr=dy/float(steps); putpixel(ROUND(x), ROUND(y),7);
for(k=0;k<steps;k++) { x+=xincr; y+=yincr; for(int i=1;i<=th/2;i++) { putpixel(ROUND(x+i),ROUND(y),7); putpixel(ROUND(x),ROUND(y),7); putpixel(ROUND(x-i),ROUND(y),7); } } } int main() { initwindow(400,400); printf("Enter the cordinates"); int x1,y1,x2,y2,th; scanf("%d%d%d%d",&x1,&y1,&x2,&y2); printf("Enter the thickness of line"); scanf("%d",&th);
.a
l g
s a
m e
.c
m o
.a
l g
s a
m e
.c
m o
#include<conio.h> #include<stdio.h> #include<graphics.h> #include<math.h> #include<iostream> #define ROUND(x) ((int)(x+0.5)) using namespace std; int lineDDA(int xa,int xb,int ya,int yb) { int i=0; int dx=xb-xa,dy=yb-ya; int steps,k; float xincr,yincr,x,y; x=xa; y=ya; if(abs(dx)>abs(dy)) steps=abs(dx); else steps=abs(dy); xincr=dx/float(steps); yincr=dy/float(steps); putpixel(ROUND(x), ROUND(y),7);
.a
l g
s a
m e
.c
m o
} int main() { initwindow(400,400); printf("Enter the cordinates :"); int x1,y1,x2,y2,tx,ty; scanf("%d%d%d%d",&x1,&y1,&x2,&y2); printf("Enter the value by which you want to translate the line :");
.a
l g
s a
m e
.c
m o
PROGRAM TO IMPLEMENT THE SCALING TRANSLATION #include<iostream> #include<stdio.h> #include<math.h> #include<dos.h> #include<process.h> #include<graphics.h> #include<conio.h> using namespace std; #define round(x) int(x+0.5) void circlepoint(int cx, int cy, int x, int y, int sx, int sy) { putpixel(x+cx,y+cy,1); putpixel(x+cx,-y+cy,1); putpixel(-x+cx,y+cy,1); putpixel(-x+cx,-y+cy,1); putpixel(y+cx,x+cy,1); putpixel(y+cx,-x+cy,1); putpixel(-y+cx,x+cy,1); putpixel(-y+cx,-x+cy,1); putpixel((x*sx)+cx,(y*sy)+cy,11); putpixel((x*sx)+cx,-(y*sy)+cy,11); putpixel(-(x*sx)+cx,(y*sy)+cy,11); putpixel(-(x*sx)+cx,-(y*sy)+cy,11); putpixel((y*sx)+cx,(x*sy)+cy,11); putpixel((y*sx)+cx,-(x*sy)+cy,11); putpixel(-(y*sx)+cx,(x*sy)+cy,11); putpixel(-(y*sx)+cx,-(x*sy)+cy,11); }
void circle1(int cx,int cy,int r,int sx,int sy) { int x,y; x=0; y=r; circlepoint(cx, cy, x,y,sx,sy); int p=1-r; int i; while((x+cx)<(y+cy)) { if(p>0) {
.a
l g
s a
m e
.c
m o
x++; y--; p=p+2*(x-y)+1; } else { x++; p=p+2*x+1; } circlepoint(cx, cy, x,y,sx,sy); } getch(); } int main() { initwindow(400,400); int r,cx,cy,current,sx,sy; cout<<" Enter the radius: "; cin>>r; cout<<" Enter the (x,y) coordinates of the centre: "; cin>>cx>>cy; cout<<"Enter the scaling factor (sx,sy) :"; cin>>sx>>sy; circle1(cx,cy,r,sx,sy); while(!kbhit()); closegraph(); return 0; getch(); }
.a
l g
s a
m e
.c
m o
.a
l g
s a
m e
.c
m o
PROGRAM TO IMPLEMENT THE ROTATION TRANSFORMATION #include<conio.h> #include<stdio.h> #include<graphics.h> #include<math.h> #include<iostream> #define ROUND(x) ((int)(x+0.5)) using namespace std; void lineDDA(int xa,int xb,int ya,int yb) { int dx=xb-xa,dy=yb-ya; int steps,k; float xincr,yincr,x,y; x=xa; y=ya; if(abs(dx)>abs(dy)) steps=abs(dx); else steps=abs(dy); xincr=dx/float(steps); yincr=dy/float(steps); putpixel(ROUND(x), ROUND(y),7); for(k=0;k<steps;k++) { x+=xincr; y+=yincr; putpixel(ROUND(x),ROUND(y),7); } }
int main() { initwindow(400,400); printf("Enter the cordinates"); int x1,y1,x2,y2; float angle; scanf("%d%d%d%d",&x1,&y1,&x2,&y2); printf("Enter the rotation angle :"); scanf("%f",&angle); angle=angle/60; int newx,newy; newx=x1+(x2-x1)*cos(angle)-(y2-y1)*sin(angle); newy=y1+(x2-x1)*sin(angle)+(y2-y1)*cos(angle); lineDDA(x1,x2,y1,y2); lineDDA(x1,newx,y1,newy); while(!kbhit());
.a
l g
s a
m e
.c
m o
.a
l g
s a
m e
.c
m o
PROGRAM TO DRAW A ANIMATION (MOVIG CAR) #include<iostream> #include<stdio.h> #include<math.h> #include<dos.h> #include<process.h> #include<graphics.h> #include<conio.h> #define ROUND(x) ((int)(x+0.5)) using namespace std; void lineDDA(int xa,int ya,int xb,int yb) { int dx=xb-xa,dy=yb-ya; int steps,k; float xincr,yincr,x,y; x=xa; y=ya; if(abs(dx)>abs(dy)) steps=abs(dx); else steps=abs(dy); xincr=dx/float(steps); yincr=dy/float(steps); putpixel(ROUND(x), ROUND(y),1);
void circlepoint(int cx, int cy, int x, int y) { putpixel(x+cx,y+cy,1); } void circle1(int cx,int cy,int r) { int x,y; x=0; y=r; circlepoint(cx, cy, x,y); int p=1-r; int i;
.a
l g
s a
m e
.c
m o
while((x+cx)<(y+cy)) { if(p>0) { x++; y--; p=p+2*(x-y)+1; } else { x++; p=p+2*x+1; } circlepoint(cx, cy, x,y); } }
void draw_line(int x1,int y1,int x2,int y2) { int dx,dy,xa,xb,ya,yb,p; dx=abs(x2-x1); dy=abs(y2-y1); if(x1<x2) { xa=x1;ya=y1;xb=x2;yb=y2; } else { xa=x2;ya=y2;xb=x1;yb=y1; } p=(2*dy)-dx; while(xa<=xb) { if(p<0) { putpixel(++xa,490-ya,8); p=p+(2*dy); }
.a
l g
s a
m e
.c
m o
} } } int main() { initwindow(800,600); //clrscr(); //int gdriver = DETECT, gmode, errorcode; //initgraph(&gdriver, &gmode, ""); //printf("Enter the cordinates"); setbkcolor(5); int i=0; while(i<300) { lineDDA(50+i,200,100+i,150); lineDDA(100+i,150,200+i,150); lineDDA(50+i,200,250+i,200); lineDDA(250+i,200,200+i,150); circle(100+i,220,20); circle(200+i,220,20); delay(150); cleardevice(); i+=20; }
.a
l g
s a
m e
.c
m o
.a
l g
s a
m e
.c
m o
PROGRAM TO DRAW POLYGON #include<stdlib.h> #include<conio.h> #include<stdio.h> #include<graphics.h> #include<math.h> #include<iostream.h> #define ROUND(x) ((int)(x+0.5)) void lineDDA(int xa,int ya,int xb,int yb) { int dx=xb-xa,dy=yb-ya; int steps,k; float xincr,yincr,x,y; x=xa; y=ya; if(abs(dx)>abs(dy)) steps=abs(dx); else steps=abs(dy); xincr=dx/float(steps); yincr=dy/float(steps); putpixel(ROUND(x), ROUND(y),7); for(k=0;k<steps;k++) { x+=xincr; y+=yincr; putpixel(ROUND(x),ROUND(y),7); } } void main() { int gdriver = DETECT, gmode, errorcode; initgraph(&gdriver, &gmode, "H:\\umesh\\tc\\bgi"); int cord[100]={0},i,ver; printf("\nEnter the no of vertices of the polygon\n"); scanf("%d",&ver); printf("Enter the cordinates of polygon in the (x1,y1)&(x2,y2)format\n"); for(i=0;i<((2*ver)+2);i++) { scanf("%d",&cord[i]); } for(i=0;i<((2*ver)+2);i=i+2) { lineDDA(cord[i],cord[i+1],cord[i+2],cord[i+3]); } getch(); closegraph(); }
.a
l g
s a
m e
.c
m o
.a
l g
s a
m e
.c
m o