Skip to main content

Posts

Showing posts from August, 2012

2D Scaling

#include<stdio.h> #include <stdlib.h> #include<graphics.h> #include<conio.h> #include<math.h> void draw2d(int,int [],int [],int,int); void main() { int gd=DETECT,gm; int x[20],y[20],tx=0,ty=0,i,fs; int mx,my; initgraph(&gd,&gm,""); printf("No of sides : "); scanf("%d",&fs); printf("Co-ordinates : "); for(i=0;i<fs;i++) { printf("(x%d,y%d)",i,i); scanf("%d%d",&x[i],&y[i]); } draw2d(fs,x,y,tx,ty); printf("magnification (x,y) : "); scanf("%d%d",&mx,&my); for(i=0;i<fs;i++) { x[i]=x[i]*mx; y[i]=y[i]*my; } draw2d(fs,x,y,tx,ty); getch(); } void draw2d(int fs,int x[20],int y[20],int tx,int ty) { int i; for(i=0;i<fs;i++) { if(i!=(fs-1)) line(x[i]+tx,y[i]+ty,x[i+1]+tx,y[i+1]+ty); else line(x[i]+tx,y[i]+ty,x[0]+tx,y[0]+ty); } }

2D Translation

#include <stdio.h> #include <stdlib.h> #include<graphics.h> #include<conio.h> #include<math.h> void draw2d(int,int [],int [],int,int); void main() { int gd=DETECT,gm; int x[20],y[20],tx=0,ty=0,i,fs; initgraph(&gd,&gm,""); printf("No of sides : "); scanf("%d",&fs); printf("Co-ordinates : "); for(i=0;i<fs;i++) { printf("(x%d,y%d)",i,i); scanf("%d%d",&x[i],&y[i]); } draw2d(fs,x,y,tx,ty); printf("translation (x,y) : "); scanf("%d%d",&tx,&ty); draw2d(fs,x,y,tx,ty); getch(); } void draw2d(int fs,int x[20],int y[20],int tx,int ty) { int i; for(i=0;i<fs;i++) { if(i!=(fs-1)) line(x[i]+tx,y[i]+ty,x[i+1]+tx,y[i+1]+ty); else line(x[i]+tx,y[i]+ty,x[0]+tx,y[0]+ty); } }

Simple Animation Using C

Here i have used fillpoly function to draw the object body and used fillellipse function to draw tier. animation is done by looping through the objects x & y position until user hits a key. Keypress event is achived  by using kbhit function. Smoothness of animation is controlled by delay function. Change the delay values to change the animation speed Source: #include<stdio.h> #include<conio.h> #include<graphics.h> #include<dos.h> void main() { int gd=DETECT,gm,i=-300,j; int poly[16]={100,100,250,100,250,50,300,50,325,90,325,140,100,140,100,100}; int tpoly[16]={100,100,250,100,250,50,300,50,325,90,325,140,100,140,100,100}; initgraph(&gd,&gm,""); getch(); while(!kbhit()) { for(j=0;j<16;j+=2) { poly[j]=tpoly[j]+i; } fillpoly(8,poly); setfillstyle(5,7); bar(275+i,60,295+i,85); setfillstyle(5,8); fillellipse(140+i,140,20,20); fillellipse(280+i,140,20,20); setfillstyle(1,0); fillellipse(140+i,140,10,10); fillellipse(280+i,140,10...

View Port

#include<stdio.h> #include <stdlib.h> #include<graphics.h> #include<conio.h> #include<math.h> void draw2d(int,int [],int [],int,int); void main() { int gd=DETECT,gm; int x[20],y[20],tx=0,ty=0,i,fs; int xv1,xv2,yv1,yv2; float sx,sy; initgraph(&gd,&gm,""); printf("No of sides : "); scanf("%d",&fs); printf("Co-ordinates : "); for(i=0;i<fs;i++) { printf("(x%d,y%d)",i,i); scanf("%d%d",&x[i],&y[i]); } printf("Enter view port values : "); scanf("%d%d%d%d",&xv1,&yv1,&xv2,&yv2); cleardevice(); rectangle(0,0,639,479); draw2d(fs,x,y,tx,ty); sx=(xv2-xv1)/(639.0); sy=(yv2-yv1)/(479.0); for(i=0;i<fs;i++) { x[i]=xv1+(x[i])*sx; y[i]=yv1+(y[i])*sy; } getch(); cleardevice(); rectangle(xv1,yv1,xv2,yv2); draw2d(fs,x,y,tx,ty); getch(); } void draw2d(int fs,int x[20],int y[20],int tx,int ty) { int i; for(i=0;i<fs;i++) { if(i!=(fs-1)) line(x[i]+tx,y[i]+ty...

Polygon Clipping

#include<stdio.h> #include<conio.h> #include<graphics.h> #include<math.h> void clip(float,float,float); int i,j=0,n; int rx1,rx2,ry1,ry2; float x1[8],y1[8]; void main() { int gd=DETECT,gm; int i,n; float x[8],y[8],m; clrscr(); initgraph(&gd,&gm,""); printf("coordinates for rectangle : "); scanf("%d%d%d%d",&rx1,&ry1,&rx2,&ry2); printf("no. of sides for polygon : "); scanf("%d",&n); printf("coordinates : "); for(i=0;i<n;i++) { scanf("%f%f",&x[i],&y[i]); } cleardevice(); outtextxy(10,10,"Before clipping"); outtextxy(10,470,"Press any key...."); rectangle(rx1,ry1,rx2,ry2); for(i=0;i<n-1;i++) line(x[i],y[i],x[i+1],y[i+1]); line(x[i],y[i],x[0],y[0]); getch(); cleardevice(); for(i=0;i<n-1;i++) { m=(y[i+1]-y[i])/(x[i+1]-x[i]); clip(x[i],y[i],m); clip(x[i+1],y[i+1],m); } m=(y[i]-y[0])/(x[i]-x[0]); clip(x[i],y[i],m); clip(x[0],y[0],m); ou...

Line Clipping

#include<stdio.h> #include<conio.h> #include<graphics.h> #include<math.h> void clip(float,float,float); int i,j=0,n; int rx1,rx2,ry1,ry2; float x1[8],y1[8]; void main() { int gd=DETECT,gm; int i,n; float x[8],y[8],m; clrscr(); initgraph(&gd,&gm,""); printf("coordinates for rectangle : "); scanf("%d%d%d%d",&rx1,&ry1,&rx2,&ry2); printf("no. of sides for polygon : "); scanf("%d",&n); printf("coordinates : "); for(i=0;i<n;i++) { scanf("%f%f",&x[i],&y[i]); } cleardevice(); outtextxy(10,10,"Before clipping"); outtextxy(10,470,"Press any key...."); rectangle(rx1,ry1,rx2,ry2); for(i=0;i<n-1;i++) line(x[i],y[i],x[i+1],y[i+1]); line(x[i],y[i],x[0],y[0]); getch(); cleardevice(); for(i=0;i<n-1;i++) { m=(y[i+1]-y[i])/(x[i+1]-x[i]); clip(x[i],y[i],m); } clip(x[0],y[0],m); outtextxy(10,10,"After clipping"); outtextxy(10,470,"Pre...

Fill Styles

#include <graphics.h> #include <stdlib.h> #include <string.h> #include <stdio.h> #include <conio.h> void main() { int gd=DETECT,gm; int s; char *fname[] = { "EMPTY FILL","SOLID FILL","LINE FILL", "LTSLASH FILL","SLASH FILL","BKSLASH FILL", "LTBKSLASH FILL","HATCH FILL", "XHATCH FILL","INTERLEAVE FILL", "WIDE DOT FILL","CLOSE DOT FILL","USER FILL" }; initgraph(&gd,&gm," "); clrscr(); cleardevice(); for (s=0;s<13;s++) { setfillstyle(s,2); setcolor(14); rectangle(149,9+s*30,251,31+s*30); bar(150,10+s*30,250,30+s*30); outtextxy(255,20+s*30,fname[s]); } getch(); closegraph(); }

Line Styles

#include <graphics.h> #include <stdlib.h> #include <string.h> #include <stdio.h> #include <conio.h> void main() { int gd=DETECT,gm; int s; char *lname[]={"SOLID LINE","DOTTED LINE","CENTER LINE", "DASHED LINE","USERBIT LINE"}; initgraph(&gd,&gm," "); clrscr(); cleardevice(); printf("Line styles:"); for (s=0;s<5;s++) {   setlinestyle(s,1,3);   line(100,30+s*50,250,250+s*50);   outtextxy(255,250+s*50,lname[s]); } getch(); closegraph(); }

Midpoint Ellipse Drawing Algorithm

#include<stdio.h> #include<conio.h> #include<graphics.h> #include<math.h> void disp(); float x,y; int xc,yc; void main() { int gd=DETECT,gm; int a,b; float p1,p2; clrscr(); initgraph(&gd,&gm,""); scanf("%d%d",&xc,&yc); scanf("%d%d",&a,&b); x=0;y=b; disp(); p1=(b*b)-(a*a*b)+(a*a)/4; while((2.0*b*b*x)<=(2.0*a*a*y)) { x++; if(p1<=0) p1=p1+(2.0*b*b*x)+(b*b); else { y--; p1=p1+(2.0*b*b*x)+(b*b)-(2.0*a*a*y); } disp(); x=-x; disp(); x=-x; } x=a; y=0; disp(); p2=(a*a)+2.0*(b*b*a)+(b*b)/4; while((2.0*b*b*x)>(2.0*a*a*y)) { y++; if(p2>0) p2=p2+(a*a)-(2.0*a*a*y); else { x--; p2=p2+(2.0*b*b*x)-(2.0*a*a*y)+(a*a); } disp(); y=-y; disp(); y=-y; } getch(); closegraph(); } void disp() { putpixel(xc+x,yc+y,10); putpixel(xc-x,yc+y,10); putpixel(xc+x,yc-y,10); putpixel(xc+x,yc-y,10); }

Midpoint Circle Drawing Algorithm

#include<stdio.h> #include<conio.h> #include<graphics.h> #include<math.h> void cplot(int,int,int,int); void main() { int gd=DETECT,gm; int x,y,p,xc,yc,r; initgraph(&gd,&gm,""); cleardevice(); printf("x,y,r : "); scanf("%d%d%d",&xc,&yc,&r); x=0;y=r; p=1-r; cplot(xc,yc,x,y); while(x<y) { x++; if(p<0) p+=2*x+1; else { y--; p+=2*(x-y)+1; } cplot(xc,yc,x,y); } getch(); } void cplot(int xc,int yc,int x,int y) { putpixel(xc+x,yc+y,15); putpixel(xc-x,yc+y,15); putpixel(xc+x,yc-y,15); putpixel(xc-x,yc-y,15); putpixel(xc+y,yc+x,15); putpixel(xc-y,yc+x,15); putpixel(xc+y,yc-x,15); putpixel(xc-y,yc-x,15); }

Bresenham's Line Drawing Algorithm

#include<stdio.h> #include<conio.h> #include<graphics.h> void Bresenhams(int x1,int y1,int x2,int y2,int col,int del); void main() { int gd=DETECT,gm,x1,x2,y1,y2; initgraph(&gd,&gm,""); printf("Enter (x1,y1) and (x2,y2) : "); scanf("%d%d%d%d",&x1,&y1,&x2,&y2); Bresenhams(x1,y1,x2,y2,14,20); getch(); closegraph(); } void Bresenhams(int x1,int y1,int x2,int y2,int col,int del) { int dx,dy,s,x,y; int p,tdy,tdydx; dx=x2-x1; dy=y2-y1; p=2*dy-dx; tdy=2*dy; tdydx=2*(dy-dx); if(x1>x2) { x=x2; y=y2; s=x1; } else { x=x1; y=y1; s=x2; } putpixel(x,y,col); while(x<s) { x++; if(p<0) p+=tdy; else { y++; p+=tdydx; } putpixel(x,y,col); delay(del); } }

DDA Line Drawing Algorithm

#include<stdio.h> #include<conio.h> #include<graphics.h> void DDA(int x1,int y1,int x2,int y2,int col,int del); void main() { int gd=DETECT,gm,x1,x2,y1,y2; initgraph(&gd,&gm,""); printf("Enter (x1,y1) and (x2,y2) : "); scanf("%d%d%d%d",&x1,&y1,&x2,&y2); DDA(x1,y1,x2,y2,14,20); getch(); closegraph(); } void DDA(int x1,int y1,int x2,int y2,int col,int del) { int dx,dy,s,i,xi,yi,x,y; x=x1,y=y1; dx=x2-x1; dy=y2-y1; if(dx>dy) s=dx; else s=dy; xi=dx/s; yi=dy/s; putpixel(x,y,col); for(i=0;i<s;i++) { x+=xi; y+=yi; putpixel(x,y,col); delay(del); } }

Simple Graphics Program

#include<stdio.h> #include<conio.h> #include<graphics.h> #include<stdlib.h> void main() { int x1,y1,x2,y2,r,xr,yr,sa,ea; int gd=DETECT,gm; initgraph(&gd,&gm," "); while(1) { clrscr(); cleardevice(); printf("Basic drawings using graphics :\n\n"); printf("1.Line\n2.Rectangle\n3.Circle\n4.Ellipse\n5.Arc\n6.Exit"); printf("\n\nEnter your choice"); switch(getche()) { case '1': //Line clrscr(); cleardevice(); printf("Enter (x1,y1) and (x2,y2) : "); scanf("%d%d%d%d",&x1,&y1,&x2,&y2); cleardevice(); line(x1,y1,x2,y2); break; case '2': //Rectangle clrscr(); cleardevice(); printf("Enter (x1,y1) and (x2,y2) : "); scanf("%d%d%d%d",&x1,&y1,&x2,&y2); cleardevice(); rectangle(x1,y1,x2,y2); break; case '3': //Circle clrscr(); cleardevice(); printf("Enter (x,y) and (radius): "); scanf("%d%...