Showing posts with label programming. Show all posts
Showing posts with label programming. Show all posts
Thursday, August 16, 2012
Draw shapes using c graphics
This
c graphics program draws basic shapes such as circle, line, rectangle,
ellipse and display text on screen using c graphics. This can be a first
graphics program for a beginner.
C programming code
#include<graphics.h> #include<conio.h> main() { int gd = DETECT,gm,left=100,top=100,right=200,bottom=200,x= 300,
y=150,radius=50; initgraph(&gd, &gm, "C:\\TC\\BGI"); rectangle(left, top, right, bottom); circle(x, y, radius); bar(left + 300, top, right + 300, bottom); line(left - 10, top + 150, left + 410, top + 150); ellipse(x, y + 200, 0, 360, 100, 50); outtextxy(left + 100, top + 325, "My First C Graphics Program"); getch(); closegraph(); return 0; }
Moving Wheel
#include<stdio.h> #include<conio.h> #include<graphics.h> #include<math.h> #include<dos.h> int l = 1; void ddaline(int x1, int y1, int x2, int y2) { int s, dx, dy, m; float xi, yi, x, y; dx = x2 - x1; dy = y2 - y1; if (abs(dx) > abs(dy)) s = abs(dx); else s = abs(dy); xi = dx / (float) s; yi = dy / (float) s; x = x1; y = y1; putpixel(x1 + 0.5, y1 + 0.5, 15); for (m = 0; m < s; m++) { x += xi; y += yi; putpixel(x + 0.5, y + 0.5, 15); } } void plotpoints1(int x, int y, int cx, int cy) { putpixel(cx + x, cy + y, 15); putpixel(cx - x, cy - y, 15); putpixel(cx - y, cy + x, 15); putpixel(cx + y, cy - x, 15); if (l % 20 == 0) { ddaline(cx - x, cy - y, cx + x, cy + y); ddaline(cx - y, cy + x, cx + y, cy - x); } l++; } void plotpoints2(int x, int y, int cx, int cy) { putpixel(cx - x, cy + y, 15); putpixel(cx + x, cy - y, 15); putpixel(cx + y, cy + x, 15); putpixel(cx - y, cy - x, 15); if (l % 20 == 0) { ddaline(cx + x, cy - y, cx - x, cy + y); ddaline(cx - y, cy - x, cx + y, cy + x); } l++; } void mcircle(int cx, int cy, int r) { int x = 0, y, p; y = r; p = 1 - r; while (x < y) { plotpoints1(x, y, cx, cy); x++; if (p < 0) p += 2 * x + 1; else { y--; p += 2 * (x - y) + 1; } } x = y + 1; while (abs(x) > 0) { plotpoints2(x, y, cx, cy); x--; if (p >= 0) p = p - 2 * x - 1; else { y++; p = p - 2 * (x - y) - 1; } } } void main() { int gd = DETECT, gm = DETECT; int i = 0; initgraph(&gd, &gm, ""); while (!kbhit()) { if (i > 640) i = -200; cleardevice(); mcircle(100 + (i++), 200, 100); delay(90); i++; } getch(); }
C graphics program moving car
#include <graphics.h> #include <dos.h> int main() { int i, j = 0, gd = DETECT, gm; initgraph(&gd,&gm,"C:\\TC\\BGI"); settextstyle(DEFAULT_FONT,HORIZ_DIR,2); outtextxy(25,240,"Press any key to view the moving car"); getch(); for( i = 0 ; i <= 420 ; i = i + 10, j++ ) { rectangle(50+i,275,150+i,400); rectangle(150+i,350,200+i,400); circle(75+i,410,10); circle(175+i,410,10); setcolor(j); delay(100); if( i == 420 ) break; if ( j == 15 ) j = 2; cleardevice(); // clear screen } getch(); closegraph(); return 0; }
Midpoint Circle Algorithm Using C Programming
#include<stdio.h>#include<conio.h>
#include<graphics.h>
void main()
{
#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();
}
3D Animation Computer Graphics Programs
1.3D Translation Program Using C Programming
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<process.h>
#include<graphics.h>
int x1,x2,y1,y2,mx,my,depth;
void draw();
void trans();
void main()
{
int gd=DETECT,gm,c;
initgraph(&gd,&gm,"d:\\tc\\bgi");
printf("\n\t\t3D Transmission\n\n");
printf("\nEnter 1st top value(x1,y1):");
scanf("%d%d",&x1,&y1);
printf("Enter right bottom value(x2,y2):");
scanf("%d%d",&x2,&y2);
depth=(x2-x1)/4;
mx=(x1+x2)/2;
my=(y1+y2)/2;
draw();
getch();
cleardevice();
trans();
getch();
}
void draw()
{
bar3d(x1,y1,x2,y2,depth,1);
}
void trans()
{
int a1,a2,b1,b2,dep,x,y;
printf("\n Enter the Ttransition Co ordinates:");
scanf("%d%d",&x,&y);
a1=x1+x;
a2=x2+x;
b1=y1+y;
b2=y2+y;
dep=(a2-a1)/4;
bar3d(a1,b1,a2,b2,dep,1);
setcolor(5);
draw();
}
2.3D Scaling Program Using C Programming
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<process.h>
#include<graphics.h>
int x1,x2,y1,y2,mx,my,depth;
void draw();
void scale();
void main()
{
int gd=DETECT,gm,c;
initgraph(&gd,&gm,"d:\\tc\\bgi");
printf("\n\t\t3D Transformation Scalling\n\n");
printf("\nEnter 1st top value(x1,y1):");
scanf("%d%d",&x1,&y1);
printf("Enter right bottom value(x2,y2):");
scanf("%d%d",&x2,&y2);
depth=(x2-x1)/4;
mx=(x1+x2)/2;
my=(y1+y2)/2;
draw();
getch();
cleardevice();
scale();
getch();
}
void draw()
{
bar3d(x1,y1,x2,y2,depth,1);
}
void scale()
{
int x,y,a1,a2,b1,b2,dep;
printf("\n\n Enter scalling co-ordinates:");
scanf("%d%d",&x,&y);
a1=mx+(x1-mx)*x;
a2=mx+(x2-mx)*x;
b1=my+(y1-my)*y;
b2=my+(y2-my)*y;
dep=(a2-a1)/4;
bar3d(a1,b1,a2,b2,dep,1);
setcolor(5);
draw();
}
3. 3D Rotation Program Using C Programming
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<process.h>
#include<graphics.h>
int x1,x2,y1,y2,mx,my,depth;
void draw();
void rotate();
void main()
{
int gd=DETECT,gm,c;
initgraph(&gd,&gm,"d:\\tc\\bgi");
printf("\n3D Transformation Rotating\n\n");
printf("\nEnter 1st top value(x1,y1):");
scanf("%d%d",&x1,&y1);
printf("Enter right bottom value(x2,y2):");
scanf("%d%d",&x2,&y2);
depth=(x2-x1)/4;
mx=(x1+x2)/2;
my=(y1+y2)/2;
draw();
getch();
cleardevice();
rotate();
getch();
}
void draw()
{
bar3d(x1,y1,x2,y2,depth,1);
}
void rotate()
{
float t;
int a1,b1,a2,b2,dep;
printf("Enter the angle to rotate=");
scanf("%f",&t);
t=t*(3.14/180);
a1=mx+(x1-mx)*cos(t)-(y1-my)*sin(t);
a2=mx+(x2-mx)*cos(t)-(y2-my)*sin(t);
b1=my+(x1-mx)*sin(t)-(y1-my)*cos(t);
b2=my+(x2-mx)*sin(t)-(y2-my)*cos(t);
if(a2>a1)
dep=(a2-a1)/4;
else
dep=(a1-a2)/4;
bar3d(a1,b1,a2,b2,dep,1);
setcolor(5);
//
draw();
}
2D Animation Computer Graphics Programs using C Language
1. 2D Scaling Program Using C Programming
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<process.h>
#include<math.h>
int x1,y1,x2,y2,x3,y3,mx,my;
void draw();
void scale();
void main()
{
int gd=DETECT,gm;
int c;
initgraph(&gd,&gm," ");
printf("Enter the 1st point for the triangle:");
scanf("%d%d",&x1,&y1);
printf("Enter the 2nd point for the triangle:");
scanf("%d%d",&x2,&y2);
printf("Enter the 3rd point for the triangle:");
scanf("%d%d",&x3,&y3);
draw();
scale();
}
void draw()
{
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
}
void scale()
{
int x,y,a1,a2,a3,b1,b2,b3;
int mx,my;
printf("Enter the scalling coordinates");
scanf("%d%d",&x,&y);
mx=(x1+x2+x3)/3;
my=(y1+y2+y3)/3;
cleardevice();
a1=mx+(x1-mx)*x;
b1=my+(y1-my)*y;
a2=mx+(x2-mx)*x;
b2=my+(y2-my)*y;
a3=mx+(x3-mx)*x;
b3=my+(y3-my)*y;
line(a1,b1,a2,b2);
line(a2,b2,a3,b3);
line(a3,b3,a1,b1);
draw();
getch();
}
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<process.h>
#include<math.h>
int x1,y1,x2,y2,x3,y3,mx,my;
void draw();
void scale();
void main()
{
int gd=DETECT,gm;
int c;
initgraph(&gd,&gm," ");
printf("Enter the 1st point for the triangle:");
scanf("%d%d",&x1,&y1);
printf("Enter the 2nd point for the triangle:");
scanf("%d%d",&x2,&y2);
printf("Enter the 3rd point for the triangle:");
scanf("%d%d",&x3,&y3);
draw();
scale();
}
void draw()
{
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
}
void scale()
{
int x,y,a1,a2,a3,b1,b2,b3;
int mx,my;
printf("Enter the scalling coordinates");
scanf("%d%d",&x,&y);
mx=(x1+x2+x3)/3;
my=(y1+y2+y3)/3;
cleardevice();
a1=mx+(x1-mx)*x;
b1=my+(y1-my)*y;
a2=mx+(x2-mx)*x;
b2=my+(y2-my)*y;
a3=mx+(x3-mx)*x;
b3=my+(y3-my)*y;
line(a1,b1,a2,b2);
line(a2,b2,a3,b3);
line(a3,b3,a1,b1);
draw();
getch();
}
2. 2D Rotation Program Using C Programming
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<process.h>
#include<math.h>
void TriAngle(int x1,int y1,int x2,int y2,int x3,int y3);
void Rotate(int x1,int y1,int x2,int y2,int x3,int y3);
void main()
{
int gd=DETECT,gm;
int x1,y1,x2,y2,x3,y3;
initgraph(&gd,&gm," ");
printf("Enter the 1st point for the triangle:");
scanf("%d%d",&x1,&y1);
printf("Enter the 2nd point for the triangle:");
scanf("%d%d",&x2,&y2);
printf("Enter the 3rd point for the triangle:");
scanf("%d%d",&x3,&y3);
TriAngle(x1,y1,x2,y2,x3,y3);
getch();
cleardevice();
Rotate(x1,y1,x2,y2,x3,y3);
setcolor(1);
TriAngle(x1,y1,x2,y2,x3,y3);
getch();
}
void TriAngle(int x1,int y1,int x2,int y2,int x3,int y3)
{
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
}
void Rotate(int x1,int y1,int x2,int y2,int x3,int y3)
{
int x,y,a1,b1,a2,b2,a3,b3,p=x2,q=y2;
float Angle;
printf("Enter the angle for rotation:");
scanf("%f",&Angle);
cleardevice();
Angle=(Angle*3.14)/180;
a1=p+(x1-p)*cos(Angle)-(y1-q)*sin(Angle);
b1=q+(x1-p)*sin(Angle)+(y1-q)*cos(Angle);
a2=p+(x2-p)*cos(Angle)-(y2-q)*sin(Angle);
b2=q+(x2-p)*sin(Angle)+(y2-q)*cos(Angle);
a3=p+(x3-p)*cos(Angle)-(y3-q)*sin(Angle);
b3=q+(x3-p)*sin(Angle)+(y3-q)*cos(Angle);
printf("Rotate");
TriAngle(a1,b1,a2,b2,a3,b3);
}
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<process.h>
#include<math.h>
void TriAngle(int x1,int y1,int x2,int y2,int x3,int y3);
void Rotate(int x1,int y1,int x2,int y2,int x3,int y3);
void main()
{
int gd=DETECT,gm;
int x1,y1,x2,y2,x3,y3;
initgraph(&gd,&gm," ");
printf("Enter the 1st point for the triangle:");
scanf("%d%d",&x1,&y1);
printf("Enter the 2nd point for the triangle:");
scanf("%d%d",&x2,&y2);
printf("Enter the 3rd point for the triangle:");
scanf("%d%d",&x3,&y3);
TriAngle(x1,y1,x2,y2,x3,y3);
getch();
cleardevice();
Rotate(x1,y1,x2,y2,x3,y3);
setcolor(1);
TriAngle(x1,y1,x2,y2,x3,y3);
getch();
}
void TriAngle(int x1,int y1,int x2,int y2,int x3,int y3)
{
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
}
void Rotate(int x1,int y1,int x2,int y2,int x3,int y3)
{
int x,y,a1,b1,a2,b2,a3,b3,p=x2,q=y2;
float Angle;
printf("Enter the angle for rotation:");
scanf("%f",&Angle);
cleardevice();
Angle=(Angle*3.14)/180;
a1=p+(x1-p)*cos(Angle)-(y1-q)*sin(Angle);
b1=q+(x1-p)*sin(Angle)+(y1-q)*cos(Angle);
a2=p+(x2-p)*cos(Angle)-(y2-q)*sin(Angle);
b2=q+(x2-p)*sin(Angle)+(y2-q)*cos(Angle);
a3=p+(x3-p)*cos(Angle)-(y3-q)*sin(Angle);
b3=q+(x3-p)*sin(Angle)+(y3-q)*cos(Angle);
printf("Rotate");
TriAngle(a1,b1,a2,b2,a3,b3);
}
3. 2D Translation Triangle Program Using C Programming
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<process.h>
#include<math.h>
int x1,y1,x2,y2,x3,y3,mx,my;
void draw();
void tri();
void main()
{
int gd=DETECT,gm;
int c;
initgraph(&gd,&gm,"d:\\tc\\bgi ");
printf("Enter the 1st point for the triangle:");
scanf("%d%d",&x1,&y1);
printf("Enter the 2nd point for the triangle:");
scanf("%d%d",&x2,&y2);
printf("Enter the 3rd point for the triangle:");
scanf("%d%d",&x3,&y3);
cleardevice();
draw();
getch();
tri();
getch();
}
void draw()
{
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
}
void tri()
{
int x,y,a1,a2,a3,b1,b2,b3;
printf("Enter the Transaction coordinates");
scanf("%d%d",&x,&y);
cleardevice();
a1=x1+x;
b1=y1+y;
a2=x2+x;
b2=y2+y;
a3=x3+x;
b3=y3+y;
line(a1,b1,a2,b2);
line(a2,b2,a3,b3);
line(a3,b3,a1,b1);
}
4. 2D Translation Rectangle Program Using C Programming
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<process.h>
#include<math.h>
void RectAngle(int x,int y,int Height,int Width);
void Translate(int x,int y,int Height,int Width);
void main()
{
int gd=DETECT,gm;
int x,y,Height,Width;
initgraph(&gd,&gm," ");
printf("Enter the First point for the Rectangle:");
scanf("%d%d",&x,&y);
printf("Enter the Height&Width for the Rectangle:");
scanf("%d%d",&Height,&Width);
RectAngle(x,y,Height,Width);
getch();
cleardevice();
Translate(x,y,Height,Width);
RectAngle(x,y,Height,Width);
getch();
}
void RectAngle(int x,int y,int Height,int Width)
{
line(x,y,x+Width,y);
line(x,y,x,y+Height);
line(x+Width,y,x+Width,y+Height);
line(x,y+Height,x+Width,y+Height);
}
void Translate(int x,int y,int Height,int Width)
{
int Newx,Newy,a,b;
printf("Enter the Transaction coordinates");
scanf("%d%d",&Newx,&Newy);
cleardevice();
a=x+Newx;
b=y+Newy;
RectAngle(a,b,Height,Width);
}
Text Animation Program Using C Programming
#include<stdio.h>
#include<math.h>
#include<conio.h>
#include<graphics.h>
#define round(val) (int)(val+0.5)
void main()
{ int gd=DETECT,gm,sx,sy,tx,ty;
char text[50];
void move(int,int,int,int,char[]);
printf("Enter the text:");
scanf("%s",text);
printf("Enter the initial points:");
scanf("%d%d",&sx,&sy);
printf("Enter the TARGET points:");
scanf("%d%d",&tx,&ty);
initgraph(&gd,&gm,"");
outtextxy(sx,sy,text);
move(sx,sy,tx,ty,text);
getch();
closegraph();
}
void move(int sx,int sy,int tx,int ty,char text[50])
{
int dx=tx-sx,dy=ty-sy,steps,k;
float xin,yin,x=sx,y=sy;
getch();
if(abs(dx)>abs(dy))
steps=abs(dy);
else
steps=abs(dy);
xin=dx/(float)steps;
yin=dy/(float)steps;
for(k=0;k<steps;k++)
{
cleardevice();
x+=xin;
y+=yin;
setcolor(15);
outtextxy(round(x),round(y),text);
delay(50);
}
}
Line Clipping Program Using C Programming
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
#define Round(val)((int)(val+.5))
int maxx,maxy,miny,minx;
void main()
{
int gd=DETECT,gm;
void clipping(int xa,int ya,int xb,int y);
int xa,xb,ya,yb;
printf("Enter the window coordination");
scanf("%d%d%d%d",&minx,&maxy,&maxx,&miny);
printf("Enter the two and points for the line");
scanf("%d%d%d%d",&xa,&ya,&xb,&yb);
initgraph(&gd,&gm,"");
rectangle(minx,miny,maxx,maxy);
line(xa,ya,xb,yb);
getch();
closegraph();
}
void clipping(int xa,int ya,int xb,int yb)
{
int Dx=xb-xa,Dy=yb-ya,steps,k;
int visible1=0,visible2=0;
float xin,yin,x=xa,y=ya;
if(abs(Dx)>abs(Dy))
steps=abs(Dx);
else
steps=abs(Dy);
xin=Dx/(float)steps;
yin=Dy/(float)steps;
putpixel(Round(x),Round(y),2);
for(k=0;k<steps;k++)
{
x+=xin;
y+=yin;
if((y>miny && y<maxx))
{
visible1=1;
putpixel(Round(x),Round(y),2);
}
else
visible2=1;
}
if(visible1==0)
outtextxy(20,200,"complextely visible");
if(visible1==1 && visible2==1)
outtextxy(20,20,"partialy visible");
if(visible1==1&&visible2==0)
outtextxy(20,20,"completly visible");
}
Bresenham's Circle Drawing Algorithm Using C Programming
#include<stdio.h> #include<conio.h> #include<graphics.h> void main() { int gd=DETECT,gm; int x,y,r; void cir(int,int,int); printf("Enter the Mid points and Radious:"); scanf("%d%d%d",&x,&y,&r); initgraph(&gd,&gm,""); cir(x,y,r); getch(); closegraph(); } void cir(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(); } |
Bresenham's Line Drawing Algorithm
#include<stdio.h> #include<math.h> #include<conio.h> #include<graphics.h> void main() { int x1,x2,y1,y2; int gd=DETECT,gm; void linebres(int,int,int,int); printf("Enter the two end points:"); scanf("%d%d%d%d",&x1,&x2,&y1,&y2); initgraph(&gd,&gm,""); cleardevice(); linebres(x1,y1,x2,y2); getch(); line(x1,y1,x2,y2); getch(); closegraph(); } void linebres(int x1,int y1,int x2,int y2) { int dx=abs(x1-x2),dy=abs(y1-y2); int p,x,y,i,xend,yend; if(dx!=0) { p=2*dy-dx; if(x1>x2) { x=x2; y=y2; xend=x1; } else { x=x1; y=y1; xend=x2; } putpixel(x,y,2); for(i=x;i<xend;i++) { x+=1; if(p<0) p+=2*dy; else p+=2*(dy-dx); } putpixel(x,y,2); } else { p=2*dx-dy; if(y1>y2) { x=x2; y=y2; yend=y2; } putpixel(x,y,2); for(i=y;i<yend;i++) { y+=1; if(p<0) p+=2*dx; else { x+=1; p+=2*(dx-dy); } putpixel(x,y,2); } } } | Tutorials |
Subscribe to:
Posts (Atom)