Drawing Polygons with Bresenham's Algorithm in C
Classified in Computers
Written at on English with a size of 5.19 KB.
This program demonstrates how to draw polygons using Bresenham's line algorithm in C. It utilizes the graphics.h
library for graphics operations.
Code Overview
The program consists of several functions:
main
: The main function initializes the graphics mode, reads polygon coordinates, draws the polygon, and waits for a mouse click before closing the graphics window.mostrarEjes
: Draws the X and Y axes on the screen.leerCoordenadasPoligono
: Prompts the user to enter the number of vertices and their coordinates for the polygon.dibujarPoligono
: Iterates through the vertices and draws the polygon by connecting consecutive points using Bresenham's line algorithm.lineaBresenham
: Implements Bresenham's line algorithm to draw a line between two points.dibujarPunto
: Draws a pixel at the specified coordinates.waitForLeftMouseClick
: Pauses the program execution until a left mouse click is detected.
Code:
#include
#include
#include
#include
#define MAX_PUNTOS 8
void mostrarEjes(void);
void leerCoordenadasPoligono(int x[20], int y[20], int* vertices);
void dibujarPoligono(int x[20], int y[20], int vertices, int color);
void lineaBresenham(int, int, int, int, int, int);
void dibujarPunto(int, int, int, int);
void waitForLeftMouseClick();
int main(int argc, char *argv[]) {
int x[20], y[20], vertices = 0, color = 4;
int n, i;
int x1, x2, y1, y2;
int errorcode;
int gdriver = IBM8514;
int gmodo = IBM8514HI;
leerCoordenadasPoligono(x, y, &vertices);
system("cls");
initgraph(&gdriver, &gmodo, "");
errorcode = graphresult();
if (errorcode != grOk) {
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Pulse una tecla para salir: ");
getch();
exit(1);
}
mostrarEjes();
dibujarPoligono(x, y, vertices, color);
waitForLeftMouseClick();
closegraph();
return 0;
}
void mostrarEjes(void) {
setcolor(WHITE);
line(512, 0, 512, 768);
line(0, 384, 1024, 384);
}
void leerCoordenadasPoligono(int x[20], int y[20], int *vertices) {
int i;
char str[10];
printf("Numero de vertices: ");
gets(str);
sscanf(str, "%d", vertices);
printf("\n");
if ((*vertices < 0) || (*vertices > MAX_PUNTOS))
*vertices = MAX_PUNTOS;
for (i = 0; i < (*vertices); i++) {
printf("\n\tIntroduce vertice %d (-512 - 512) --> X: ", i);
gets(str);
sscanf(str, "%d", &x[i]);
printf("\tIntroduce vertice %d (-384 - 384) --> Y: ", i);
gets(str);
sscanf(str, "%ld", &y[i]);
}
}
void dibujarPoligono(int x[], int y[], int vertices, int color) {
int cont;
setcolor(RED);
outtextxy(0, 0, "Bresenham");
for (cont = 0; cont < vertices - 1; cont++)
lineaBresenham(x[cont] + (1024 / 2), (768 / 2) - y[cont], x[cont + 1] + (1024 / 2), (768 / 2) - y[cont + 1], color, 1);
lineaBresenham(x[cont] + (1024 / 2), (768 / 2) - y[cont], x[0] + (1024 / 2), (768 / 2) - y[0], color, 1);
}
void lineaBresenham(int x0, int y0, int x1, int y1, int color, int oct) {
int x, y, i, e, delta_x, delta_y, temp, incx, incy, aldatu;
x = x0;
y = y0;
delta_x = x1 - x0;
delta_y = y1 - y0;
if (delta_x > 0)
incx = 1;
else if (delta_x == 0)
incx = 0;
else
incx = -1;
if (delta_y > 0)
incy = 1;
else if (delta_y == 0)
incy = 0;
else
incy = -1;
delta_x = abs(delta_x);
delta_y = abs(delta_y);
aldatu = 0;
if (delta_y > delta_x) {
temp = delta_x;
delta_x = delta_y;
delta_y = temp;
aldatu = 1;
}
e = (2 * delta_y) - delta_x;
for (i = 1; i <= delta_x; i++) {
dibujarPunto(x, y, color, oct);
if (e >= 0) {
if (aldatu == 1)
x = x + incx;
else
y = y + incy;
e = e - (2 * delta_x);
}
if (aldatu == 1)
y = y + incy;
else
x = x + incx;
e = e + (2 * delta_y);
}
}
void dibujarPunto(int x, int y, int color, int oct) {
int xx, yy;
if (oct != 1) {
xx = x + (1024 / 2);
yy = (768 / 2) - y;
putpixel(xx, yy, color);
} else {
xx = x;
yy = (768) - y;
putpixel(xx, yy, color);
}
}
void waitForLeftMouseClick() {
clearmouseclick(WM_LBUTTONDOWN);
const int DELAY = 50; //Miliseconds of delay between checks
int x, y;
while (!ismouseclick(WM_LBUTTONDOWN)) {
delay(DELAY);
}
getmouseclick(WM_LBUTTONDOWN, x, y);
}
Explanation:
- The program initializes the graphics system using
initgraph
. - The
leerCoordenadasPoligono
function takes user input for the polygon's vertices. - The
dibujarPoligono
function then useslineaBresenham
to draw lines between these vertices. lineaBresenham
implements the Bresenham's line algorithm, which efficiently determines which pixels should be drawn to form a line between two points.- The
dibujarPunto
function plots the pixel on the screen. - Finally,
waitForLeftMouseClick
keeps the window open until the user clicks the left mouse button.
Note:
This code requires the graphics.h
library, which is specific to certain compilers (like Turbo C++). Ensure that the library is properly configured in your development environment.