|
| 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