Created
January 31, 2024 20:25
-
-
Save EliezerSolinger/0c04ed958b8fe38e8804db7a897a7559 to your computer and use it in GitHub Desktop.
Bresenham C++ Demo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#define WIDTH 20 | |
#define HEIGHT 20 | |
char buffer[HEIGHT][WIDTH] = {} ; | |
void draw() { | |
for(int y=0;y<HEIGHT;y++) { | |
for(int x=0;x<WIDTH;x++){ | |
printf("%c ",buffer[y][x]); | |
} | |
printf("\n"); | |
} | |
} | |
void scan() { | |
for(int y=0;y<HEIGHT;y++) { | |
for(int x=0;x<WIDTH;x++){ | |
buffer[y][x]='0'; | |
} | |
} | |
} | |
// function for line generation | |
void bresenham(int x1, int y1, int x2, int y2,char c) | |
{ | |
int m_new = 2 * (y2 - y1); | |
int slope_error_new = m_new - (x2 - x1); | |
for (int x = x1, y = y1; x <= x2; x++) { | |
buffer[y][x]=c; | |
// Add slope to increment angle formed | |
slope_error_new += m_new; | |
// Slope error reached limit, time to | |
// increment y and update slope error. | |
if (slope_error_new >= 0) { | |
y++; | |
slope_error_new -= 2 * (x2 - x1); | |
} | |
} | |
} | |
int main() { | |
scan(); | |
bresenham(0,0,10,6,'x'); | |
draw(); | |
printf("HELLO WORLD!\n"); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment