Skip to content

Commit 60ec98b

Browse files
authored
Merge pull request #151 from rahulkumaran/rahulkumaran-graphics_algos
Graphics Folder with an implementation of DDA Line Algo in C
2 parents 62a9211 + 1b2a31d commit 60ec98b

File tree

2 files changed

+105
-0
lines changed

2 files changed

+105
-0
lines changed

Graphics Algos/README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
## GRAPHICS ALGORITHMS
2+
3+
This is a folder which contains graphics algorithms and their implementations.
4+
5+
### Robo.c
6+
7+
The robo.c program is a program which helps you in drawing a robot.
8+
The robot is drawn around a reference point. The DDA Line Algorithm was used in making the program.
9+
The DDA Line algorithm exists inside the program in a function so you can have a look at what the DDA line algorithm is,
10+
inside this program itself.<br><br>
11+
The image below shows the Output of the program!
12+
13+
![picture](https://user-images.githubusercontent.com/26206171/33807711-e08a11ca-de00-11e7-8635-78aa0f8a2884.png)

Graphics Algos/robo.c

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
#include <graphics.h>
2+
3+
float FX,FY,BX,BY,ax,bx,ay,by;
4+
5+
void DDALine(float x1,float y1,float x2,float y2)
6+
{
7+
float x,y,dx,dy,step;
8+
if(x1>x2 || y1>y2)
9+
{
10+
x1=x1+x2;x2=x1-x2;x1=x1-x2; // x1 x2 swap
11+
y1=y1+y2;y2=y1-y2;y1=y1-y2; // y1 y2 swap
12+
}
13+
dx=abs(x2-x1); //finding the positive difference between x1 and x2
14+
dy=abs(y2-y1); //finding positive difference between y1 and y2
15+
if(dx>=dy)
16+
step=dx;
17+
else
18+
step=dy;
19+
dx/=step;
20+
dy/=step;
21+
x=x1;
22+
y=y1;
23+
for(int i=0;i<=step;i++)
24+
{
25+
putpixel(x,y,WHITE);
26+
x+=dx;
27+
y+=dy;
28+
}
29+
}
30+
void DDARect(float x1,float y1,float x2,float y2) //Creating a rectangle using dda line
31+
{
32+
DDALine(x1,y1,x2,y1);
33+
DDALine(x2,y1,x2,y2);
34+
DDALine(x2,y2,x1,y2);
35+
DDALine(x1,y1,x1,y2);
36+
}
37+
void main( )
38+
{
39+
BX=320; BY=240; FX=BX; FY=BY-60; //BX and BY are some constants to make sure we place the robo somewhere in the centre of screen
40+
int gd=DETECT,gm; // FX and FY are translating constants to make sure all parts of the body are drawns
41+
initgraph(&gd,&gm,"NULL");
42+
//--Head
43+
//Cap
44+
circle(FX,FY-40,5);
45+
DDARect(FX-2.5,FY-25,FX+2.5,FY-35);
46+
arc(FX,FY-20,180,360,5);
47+
//Face
48+
DDARect(FX-20,FY+20,FX+20,FY-20);//Face Outline
49+
DDARect(FX-15,FY,FX-5,FY-10);//Left Eye
50+
DDARect(FX+5,FY,FX+15,FY-10);//Right Eye
51+
//Mouth
52+
DDARect(FX-10,FY+15,FX+10,FY+10);
53+
//TEETH
54+
DDARect(FX-2,FY+15,FX+2,FY+10);
55+
DDARect(FX-6,FY+15,FX+6,FY+10);
56+
//LEFT_EAR
57+
DDARect(FX-25,FY,FX-20,FY-10);
58+
DDARect(FX-30,FY-2.5,FX-25,FY-7.5);
59+
//RIGHT_EAR
60+
DDARect(FX+25,FY,FX+20,FY-10);
61+
DDARect(FX+25,FY-2.5,FX+30,FY-7.5);
62+
DDARect(BX-5,BY-30,BX+5,BY-40);//Neck
63+
//--BODY
64+
DDARect(BX-30,BY+30,BX+30,BY-30); // Body Outline
65+
DDARect(BX-20,BY+15,BX+20,BY-20); // Inner Square
66+
DDARect(BX-25,BY+70,BX-10,BY+30); // Left Leg
67+
arc(BX-17.5,BY+70,180,360,7.5); // Left Foot
68+
DDARect(BX+25,BY+70,BX+10,BY+30); // Right Leg
69+
arc(BX+17.5,BY+70,180,360,7.5); // Right Foot
70+
//Left_Hand
71+
ax=BX-47,ay=BY-5; //ax and bx become relative points to draw both hands
72+
bx=BX-47,by=BY+5;
73+
for(int i=0;i<10;i++)
74+
{
75+
putpixel(ax,ay,WHITE);
76+
putpixel(bx,by,WHITE);
77+
ax+=1.7;
78+
bx+=1.7;
79+
ay-=1;
80+
by-=1;
81+
}
82+
circle(BX-47,BY,5);
83+
//Right_Hand
84+
DDALine(BX+47,BY-5,BX+30,BY-15);
85+
DDALine(BX+47,BY+5,BX+30,BY-5);
86+
circle(BX+47,BY,5);
87+
ax=BX+47,ay=BY-5;
88+
bx=BX+47,by=BY+5;
89+
//END
90+
delay(5000);
91+
getch();
92+
}

0 commit comments

Comments
 (0)