Skip to content

Commit 0cd74e6

Browse files
committed
Unified all lgorithms in one single file
1 parent 9bf86e0 commit 0cd74e6

File tree

8 files changed

+214
-187
lines changed

8 files changed

+214
-187
lines changed

include/bresenham.h

-25
This file was deleted.

include/dda.h

-17
This file was deleted.

include/ldalg.h

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Bresenham's Algorithm Interface (Integer Version)
3+
* ----------------------------------------------------------------------------
4+
* Protoypes of all functions used for ldalg algorithm.
5+
* ----------------------------------------------------------------------------
6+
* Author: Renan Bomtempo
7+
* ----------------------------------------------------------------------------
8+
*/
9+
#ifndef LDALG_H
10+
#define LDALG_H
11+
12+
/*
13+
* Axis of maximum movement.
14+
*/
15+
enum DrivingAxis {
16+
X,
17+
Y
18+
};
19+
20+
/*
21+
* Draw a line between points p1 and p2 using the Bresenham algorithm.
22+
*/
23+
void Bresenham ( screen *scr, point p1, point p2 );
24+
25+
/*
26+
* Draw a line between points p1 and p2 using the Digital Differential Analyser algorithm.
27+
*/
28+
void DigitalDifferentialAnalyser ( screen *scr, point p1, point p2 );
29+
30+
/*
31+
* Draw a line between points p1 and p2 using the INTEGER version of Midpoint algorithm.
32+
*/
33+
void FloatMidpoint ( screen *scr, point p0, point p1 );
34+
35+
/*
36+
* Draw a line between points p1 and p2 using the FLOAT version of Midpoint algorithm.
37+
*/
38+
void IntMidpoint ( screen *scr, point p0, point p1 );
39+
40+
#endif // LDALG_H

makefile

+4-7
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,12 @@ SRC = .\src\\
77
all: main clean
88

99
#BINARY FILE
10-
main: screen.o dda.o point.o bresenham.o
11-
$(CFLAGS) $(SRC)main.c screen.o dda.o point.o bresenham.o $(OUT)
10+
main: screen.o point.o ldalg.o
11+
$(CFLAGS) $(SRC)main.c screen.o point.o ldalg.o $(OUT)
1212

1313
#OBJECT FILES
14-
bresenham.o:
15-
$(CFLAGS) $(OBJ) $(SRC)bresenham.c
16-
17-
dda.o:
18-
$(CFLAGS) $(OBJ) $(SRC)dda.c
14+
ldalg.o:
15+
$(CFLAGS) $(OBJ) $(SRC)ldalg.c
1916

2017
point.o:
2118
$(CFLAGS) $(OBJ) $(SRC)point.c

src/bresenham.c

-62
This file was deleted.

src/dda.c

-69
This file was deleted.

src/ldalg.c

+151
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
/*
2+
* Bresenham's Algorithm Implementation (Integer Version)
3+
* ----------------------------------------------------------------------------
4+
* Implementations of all functions used for Bresenham's algorithm.
5+
* ----------------------------------------------------------------------------
6+
* Version: 0.0
7+
* Author: Renan Bomtempo
8+
* ----------------------------------------------------------------------------
9+
* *This version only plots lines within the first octant of the plane*
10+
*/
11+
#include <stdlib.h>
12+
#include <stdio.h>
13+
#include <math.h>
14+
#include "screen.h"
15+
#include "point.h"
16+
#include "ldalg.h"
17+
#define _CONSOLE_LOG_(X) printf("..."#X"...\n");
18+
19+
20+
void DigitalDifferentialAnalyser ( screen *scr, point p0, point p1 )
21+
{
22+
_CONSOLE_LOG_(Initiating DDA Algorithm)
23+
24+
//X and Y differentials
25+
float dx = p1.x - p0.x;
26+
float dy = p1.y - p0.y;
27+
28+
//unit steps
29+
int steps;
30+
31+
//X and Y values of the pixel that will be turned ON
32+
float x_buffer = p0.x;
33+
float y_buffer = p0.y;
34+
35+
//X and Y increments
36+
float x_increment;
37+
float y_increment;
38+
39+
//Define the number of steps based on the greater differential
40+
if (abs(dx) > abs(dy)) {
41+
steps = abs(dx);
42+
} else {
43+
steps = abs(dy);
44+
}
45+
46+
//Calculate X and Y increment
47+
x_increment = dx / steps;
48+
y_increment = dy / steps;
49+
50+
//Turn on the first pixel (exact coordinates of p0)
51+
SetPixelState(scr, round(x_buffer), round(y_buffer), ON);
52+
53+
//Loop for incrementing X and Y coordinates
54+
for (int i = 0; i < steps; i++) {
55+
x_buffer += x_increment;
56+
y_buffer += y_increment;
57+
SetPixelState(scr, round(x_buffer), round(y_buffer), ON);
58+
}
59+
60+
_CONSOLE_LOG_(Finished executing DDA Algorithm)
61+
}
62+
63+
void Bresenham ( screen *scr, point p0, point p1 )
64+
{
65+
_CONSOLE_LOG_(Initiating Bresenham Algorithm)
66+
67+
if (IsSamePoint(p1, p0)) {
68+
SetPixelState(scr, p0.x, p0.y, ON);
69+
} else {
70+
//X and Y differentials
71+
int dx = p1.x - p0.x;
72+
int dy = p1.y - p0.y;
73+
74+
//Auxilary variables
75+
int twice_dy = 2 * dy;
76+
int twice_dy_dx = twice_dy - 2*dx;
77+
int decision_variable = twice_dy - dx;
78+
79+
//X and Y values of the pixel that will be turned ON
80+
int buffer_x = p0.x;
81+
int buffer_y = p0.y;
82+
83+
while (buffer_x != p1.x) {
84+
SetPixelState(scr, buffer_x++, buffer_y, ON);
85+
if (decision_variable < 0) {
86+
decision_variable += twice_dy;
87+
} else {
88+
buffer_y++;
89+
decision_variable += twice_dy_dx;
90+
}
91+
}
92+
}
93+
94+
_CONSOLE_LOG_(Finished executing Bresenham Algorithm)
95+
}
96+
97+
void FloatMidpoint ( screen *scr, point p0, point p1 )
98+
{
99+
_CONSOLE_LOG_(Initiating Float Midpoint Algorithm)
100+
101+
//X and Y differentials
102+
float dx = p1.x - p0.x;
103+
float dy = p1.y - p0.y;
104+
105+
//Slope
106+
float m = dy/dx;
107+
108+
//Real value of Y coordinate
109+
float real_y = 0;
110+
111+
//X and Y values of the pixel that will be turned ON
112+
int buffer_x = p0.x;
113+
int buffer_y = p0.y;
114+
115+
while (buffer_x != p1.x) {
116+
SetPixelState(scr, buffer_x++, buffer_y, ON);
117+
real_y = m * buffer_x;
118+
if (real_y > (float)buffer_y + 0.5f) {
119+
buffer_y += 1;
120+
}
121+
}
122+
_CONSOLE_LOG_(Finished executing Float Midpoint Algorithm)
123+
}
124+
125+
void IntMidpoint ( screen *scr, point p0, point p1 )
126+
{
127+
_CONSOLE_LOG_(Initiating Integer Midpoint Algorithm)
128+
129+
//X and Y differentials
130+
int dx = p1.x - p0.x;
131+
int dy = p1.y - p0.y;
132+
133+
//X and Y values of the pixel that will be turned ON
134+
int buffer_x = p0.x;
135+
int buffer_y = p0.y;
136+
137+
//Integer values of the Slope and real Y coordinate
138+
int m2dx = 2*dy;
139+
int real_y2dx = 0;
140+
141+
while (buffer_x != p1.x) {
142+
SetPixelState(scr, buffer_x, buffer_y, ON);
143+
real_y2dx = m2dx * (++buffer_x); //Same as the float version, but multiplied by 2dx
144+
if (real_y2dx > (2*buffer_y + 1)*dx) {
145+
buffer_y += 1;
146+
}
147+
}
148+
149+
_CONSOLE_LOG_(Finished executing Integer Midpoint Algorithm)
150+
}
151+

0 commit comments

Comments
 (0)