Posts

Showing posts from October, 2008

Pendulum

Image
# include <graphics.h> # include <conio.h> # include <dos.h> # include <process.h> int cx= 300 ,cy= 100 ,r= 300 ; int x= 0 ,y,p; int path( int dtn) { int x1,y1; setcolor( 15 ); if (x>=y) { return 0 ; } cleardevice(); if (dtn== 1 ) { circle(cx+x,cy+y, 20 ); line(cx,cy,cx+x,cy+y); } else { circle(cx-x,cy+y, 20 ); line(cx,cy,cx-x,cy+y); } delay( 10 ); if (kbhit()) exit( 0 ); x++; if (p< 0 ) p+= 2 *x+ 1 ; else { y--; p+= 2 *(x-y)+ 1 ; } x1=x; y1=y; path(dtn); cleardevice(); if (dtn== 1 ) { circle(cx+x1,cy+y1, 20 ); line(cx,cy,cx+x1,cy+y1); } else { circle(cx-x1,cy+y1, 20 ); line(cx,cy,cx-x1,cy+y1); } delay( 10 ); if (kbhit()) exit( 0 ); return ( 0 ); } void

Moving Wheel

Image
#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 +

Fill a bucket with water from a pipe.

# include <conio.h> # include <dos.h> # include <graphics.h> void main() { int gd=DETECT,gm=DETECT,i,j; initgraph(&gd,&gm, "" ); ellipse( 300 , 200 , 0 , 360 , 50 , 25 ); ellipse( 300 , 300 , 0 , 360 , 50 , 25 ); line( 250 , 200 , 250 , 300 ); line( 350 , 200 , 350 , 300 ); ellipse( 300 , 100 , 180 , 360 , 5 , 2 ); line( 295 , 100 , 295 , 80 ); line( 305 , 100 , 305 , 86 ); arc( 300 , 80 , 90 , 180 , 5 ); putpixel( 306 , 85 , 15 ); putpixel( 307 , 84 , 15 ); line( 308 , 84 , 630 , 84 ); line( 300 , 75 , 303 , 75 ); line( 314 , 75 , 630 , 75 ); putpixel( 304 , 74 , 15 ); putpixel( 305 , 73 , 15 ); line( 306 , 72 , 306 , 65 ); line( 311 , 72 , 311 , 65 ); putpixel( 312 , 73 , 15 ); putpixel( 313 , 74 , 15 ); pieslice( 309 , 62 , 0 , 360 , 5 ); setfillstyle(SOLID_FILL,BLUE); setcolor(BLUE); for (i= 0 ;i< 7 ;i++) {

To start with graphics programming

Image
Turbo C has a good collection of graphics libraries. If you know the basics of C, you can easily learn graphics programming. To start programming, let us write a small program that displays a circle on the screen. /* simple.c example 1.0 */ #include<graphics.h> #include<conio.h> void main() { int gd=DETECT, gm; initgraph(&gd, &gm, "c:\\tc\\bgi " ); circle(200,100,150); getch(); closegraph(); } To run this program, you need graphics.h header file, graphics.lib library file and Graphics driver (BGI file) in the program folder. These files are part of Turbo C package. In all our programs we used 640x480 VGA monitor. So all the programs are according to that specification. You need to make necessary changes to your programs according to your screen resolution. For VGA monitor, graphics driver used is EGAVGA.BGI. Here, initgraph() function initializes the graphics mode and clears the screen. We will study the difference betwee

A man walking in the rain.

Image
# include <stdio.h> # include <dos.h> # include <conio.h> # include <graphics.h> # include <stdlib.h> void rain( int x1, int y1, int x2, int y2) { int s,dx,dy,m,c= 0 ,t= 1 ; 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 , 9 ); for (m= 0 ;m<s;m++) { c++; x+=xi; y+=yi; if (getpixel(x,y)== 4 ) break ; if (c% 10 == 0 ) t++; putpixel(x+ 0 .5 ,y+ 0 .5 , 0 ); if (t% 2 == 0 ) putpixel(x+ 0 .5 ,y+ 0 .5 , 9 ); } } void main() { int gd=DETECT,gm=DETECT,c=- 200 ,i= 0 ,x= 40 ,l= 15 ,h= 15 ,ht= 0 ; initgraph(&gd,&gm, "" ); cleardevice(); setcolor(BROWN); line( 0 , 201 , 600 , 201 ); cont:

Car Movement

#include<stdio.h> #include<conio.h> #include<graphics.h> #include<dos.h> int scan,ascii; void getkey() { union REGS ii,oo; ii.h.ah= 0 ; int86( 22 ,&ii,&oo); scan=oo.h.ah; ascii=oo.h.al; } void car( int x, int c) { setcolor(c); line(x+ 150 , 100 ,x+ 242 , 100 ); ellipse(x+ 242 , 105 , 0 , 90 , 10 , 5 ); line(x+ 150 , 100 ,x+ 120 , 150 ); line(x+ 252 , 105 ,x+ 280 , 150 ); line(x+ 100 , 150 ,x+ 320 , 150 ); line(x+ 100 , 150 ,x+ 100 , 200 ); line(x+ 320 , 150 ,x+ 320 , 200 ); line(x+ 100 , 200 ,x+ 110 , 200 ); line(x+ 320 , 200 ,x+ 310 , 200 ); arc(x+ 130 , 200 , 0 , 180 , 20 ); arc(x+ 290 , 200 , 0 , 180 , 20 ); line(x+ 270 , 200 ,x+ 150 , 200 ); circle(x+ 130 , 200 , 17 ); circle(x+ 290 , 200 , 17 ); } void main() { int gd=DETECT,gm=DETECT,i= 0 ,c= 1 ,prev= 80 ; initgraph(&gd,&gm, "" ); cleardevice

Car movement during Traffic Signal

#include<conio.h> #include<dos.h> #include<graphics.h> void car( int x, int c) { setcolor(c); line(x+ 150 , 100 ,x+ 242 , 100 ); ellipse(x+ 242 , 105 , 0 , 90 , 10 , 5 ); line(x+ 150 , 100 ,x+ 120 , 150 ); line(x+ 252 , 105 ,x+ 280 , 150 ); line(x+ 100 , 150 ,x+ 320 , 150 ); line(x+ 100 , 150 ,x+ 100 , 200 ); line(x+ 320 , 150 ,x+ 320 , 200 ); line(x+ 100 , 200 ,x+ 110 , 200 ); line(x+ 320 , 200 ,x+ 310 , 200 ); arc(x+ 130 , 200 , 0 , 180 , 20 ); arc(x+ 290 , 200 , 0 , 180 , 20 ); line(x+ 270 , 200 ,x+ 150 , 200 ); circle(x+ 130 , 200 , 17 ); circle(x+ 290 , 200 , 17 ); } void main() { int gd=DETECT,gm=DETECT,i=- 200 ; int ch= 'g' ; initgraph(&gd,&gm, "" ); line( 0 , 70 , 300 , 70 ); line( 450 , 70 , 630 , 70 ); line( 300 , 70 , 300 , 0 ); line( 450 , 70 , 450 , 0 ); line( 0 , 230

Bouncing Ball

# include <stdio.h> # include <conio.h> # include <graphics.h> # include <dos.h> void main() { int gd=DETECT,gm=DETECT; int x,y= 0 ,j,t= 400 ,c= 1 ; initgraph(&gd,&gm, "" ); setcolor(RED); setfillstyle(SOLID_FILL,RED); for (x= 40 ;x< 602 ;x++) { cleardevice(); circle(x,y, 30 ); floodfill(x,y,RED); delay( 40 ); if (y>= 400 ) { c= 0 ; t-= 20 ; } if (y<=( 400 -t)) c= 1 ; y=y+(c? 15 :- 15 ); } getch(); }

Boundary Fill

The boundary fill algorithm fills a region with the given fill color until the given boundary color is found. Here i have used the 8-point technique to color a region. The program is using a recurssive function. The function return when the pixel to be colored is the boundary color or is already the fill color. # include <stdio.h> # include <conio.h> # include <graphics.h> void boundary_fill( int x, int y, int fcolor, int bcolor) { if ((getpixel(x,y) != fcolor) && (getpixel(x,y) != bcolor)) { putpixel(x,y,fcolor); boundary_fill(x+ 1 ,y,fcolor,bcolor); boundary_fill(x- 1 ,y,fcolor,bcolor); boundary_fill(x,y- 1 ,fcolor,bcolor); boundary_fill(x,y+ 1 ,fcolor,bcolor); boundary_fill(x+ 1 ,y- 1 ,fcolor,bcolor); boundary_fill(x+ 1 ,y+ 1 ,fcolor,bcolor); boundary_fill(x- 1 ,y- 1 ,fcolor,bcolor); boundary_fill(x- 1 ,y+ 1 ,fcolor,bcolor); } } void main()

Flood Fill

The flood fill algorithm replaces a current color with a new color.Here i have used the 8-point technique to color a region. The program is using a recurssive function. The function return when the pixel to be replaced is not the given old color. #include<stdio.h> #include<conio.h> #include<graphics.h> void flood_fill( int x, int y, int ncolor, int ocolor) { if (getpixel(x,y) == ocolor) { putpixel(x,y,ncolor); flood_fill(x+1,y,ncolor,ocolor); flood_fill(x+1,y-1,ncolor,ocolor); flood_fill(x+1,y+1,ncolor,ocolor); flood_fill(x,y-1,ncolor,ocolor); flood_fill(x,y+1,ncolor,ocolor); flood_fill(x-1,y,ncolor,ocolor); flood_fill(x-1,y-1,ncolor,ocolor); flood_fill(x-1,y+1,ncolor,ocolor); } } void main() { int x,y,ncolor,ocolor; clrscr(); printf( "Enter the seed point\n" ); scanf( "%d%d" ,&x,&y); printf( "Ent

Ellipse

# include <graphics.h> # include <conio.h> # include <stdio.h> void plotpoints( int cx, int cy, int x, int y) { putpixel(cx + x, cy + y, 4 ); putpixel(cx - x, cy + y, 4 ); putpixel(cx + x, cy - y, 4 ); putpixel(cx - x, cy - y, 4 ); } void main() { int cx,cy,rx,ry; printf( "Enter the center " ); scanf( "%d%d" ,&cx,&cy); printf( "x radius : " ); scanf( "%d" ,&rx); printf( "y radius : " ); scanf( "%d" ,&ry); long rx2 = ( long )rx*rx; long ry2 = ( long )ry*ry; long trx2 = 2 *rx2; long try2 = 2 *ry2; long p,x= 0 , y=ry; long px= 0 ; long py = trx2 * y; p = ( long ) ( ( ry2 - (rx2 * ry) + ( 0 .25 * rx2) ) + 0 .5 ); int gd=DETECT,gm=DETECT; initgraph(&gd,&gm, "" ); cleardevice(); putpixel(cx,cy, 15 ); while (px<py) {

Wheel

# include <conio.h> # include <graphics.h> # include <math.h> int L= 0 ; void ddaline( int x1, int y1, int x2, int y2) { int s,dx,dy,m,c= 0 ; 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; if (getpixel(x+ 0 .5 ,y+ 0 .5 )== 4 ) { c++; continue ; } if (c% 2 == 0 ) { putpixel(x+ 0 .5 ,y+ 0 .5 , 15 ); } } } void plotpoints( int x, int y, int cx, int cy, int s) { if (s== 0 ) { if (L% 20 == 0 ) { ddaline(cx-x,cy-y,cx+x,cy+y); ddaline(cx-y,cy+x,cx+y,cy-x); ddaline(cx-y,cy-x,cx+y,cy+x); ddaline(cx-x,cy+y,cx+