Skip to content

Instantly share code, notes, and snippets.

@EliezerSolinger
Created January 31, 2024 20:25
Show Gist options
  • Save EliezerSolinger/0c04ed958b8fe38e8804db7a897a7559 to your computer and use it in GitHub Desktop.
Save EliezerSolinger/0c04ed958b8fe38e8804db7a897a7559 to your computer and use it in GitHub Desktop.
Bresenham C++ Demo
#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