refactor
This commit is contained in:
66
graphics.c
66
graphics.c
@@ -1,4 +1,6 @@
|
||||
|
||||
#include <math.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#define uint unsigned int
|
||||
@@ -11,6 +13,34 @@ typedef struct Canvas {
|
||||
char* data;
|
||||
} Canvas;
|
||||
|
||||
|
||||
|
||||
// DECLARATIONS
|
||||
void render(Canvas* c);
|
||||
void line(Canvas* c, uint start_x, uint start_y, uint end_x, uint end_y);
|
||||
void dot(Canvas* c, uint x, uint y);
|
||||
|
||||
|
||||
/*
|
||||
* main
|
||||
*/
|
||||
int main(){
|
||||
Canvas a = {40, 40, malloc(40*40)};
|
||||
memset(a.data, OFF, a.x * a.y);
|
||||
|
||||
//line(&a, 1, 1, 8, 8);
|
||||
//line(&a, 8, 8, 1, 1);
|
||||
//line(&a, 0, 5, 8, 8);
|
||||
line(&a, 8, 8, 0, 5);
|
||||
|
||||
|
||||
render(&a);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* Draws an x*y rectangle with the characters in canvas* c
|
||||
*/
|
||||
@@ -33,7 +63,6 @@ void dot(Canvas* c, uint x, uint y){
|
||||
c->data[ y*(c->x) + x] = ON;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Draws a line from (start_x, start_y) to (end_x, end_y)
|
||||
*/
|
||||
@@ -42,8 +71,6 @@ void line(Canvas* c, uint start_x, uint start_y, uint end_x, uint end_y){
|
||||
int dy = end_y - start_y;
|
||||
if (dx<0) dx *= -1;
|
||||
if (dy<0) dy *= -1;
|
||||
dx++;
|
||||
dy++;
|
||||
|
||||
if((dx>=dy && start_x>end_x) || ( dx<dy && start_y>end_y)){
|
||||
int temp;
|
||||
@@ -64,41 +91,16 @@ void line(Canvas* c, uint start_x, uint start_y, uint end_x, uint end_y){
|
||||
float small = direction_y * (float)dy/(float)dx;
|
||||
|
||||
for(int i=0; i<dx; i++){
|
||||
dot(c, start_x + i*direction_x, start_y + i*small);
|
||||
dot(c, start_x + i*direction_x, start_y + nearbyintf(i*small));
|
||||
}
|
||||
}else{
|
||||
float small = direction_x * (float)dx/(float)dy;
|
||||
|
||||
for(int i=0; i<dy; i++){
|
||||
dot(c, start_x + i*small, start_y + i*direction_y);
|
||||
dot(c, start_x + nearbyintf(i*small), start_y + i*direction_y);
|
||||
}
|
||||
}
|
||||
|
||||
dot(c, end_x, end_y);
|
||||
}
|
||||
|
||||
|
||||
int main(){
|
||||
char data[] =
|
||||
"----------"
|
||||
"----------"
|
||||
"----------"
|
||||
"----------"
|
||||
"----------"
|
||||
"----------"
|
||||
"----------"
|
||||
"----------"
|
||||
"----------"
|
||||
"----------";
|
||||
|
||||
Canvas a = {10, 10, data};
|
||||
|
||||
//line(&a, 1, 1, 8, 8);
|
||||
//line(&a, 8, 8, 1, 1);
|
||||
//line(&a, 0, 5, 8, 8);
|
||||
line(&a, 8, 8, 0, 5);
|
||||
|
||||
|
||||
render(&a);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user