line function works from 2nd quadrant to 4th quadrant
This commit is contained in:
40
graphics.c
40
graphics.c
@@ -20,6 +20,7 @@ void render(Canvas* c){
|
|||||||
for(int i=0; i < c->y; i++){
|
for(int i=0; i < c->y; i++){
|
||||||
for(int j=0; j < c->x; j++){
|
for(int j=0; j < c->x; j++){
|
||||||
putchar(c->data[i*(c->x)+j]);
|
putchar(c->data[i*(c->x)+j]);
|
||||||
|
putchar(' ');
|
||||||
}
|
}
|
||||||
putchar('\n');
|
putchar('\n');
|
||||||
}
|
}
|
||||||
@@ -39,23 +40,25 @@ void dot(Canvas* c, uint x, uint y){
|
|||||||
void line(Canvas* c, uint start_x, uint start_y, uint end_x, uint end_y){
|
void line(Canvas* c, uint start_x, uint start_y, uint end_x, uint end_y){
|
||||||
#ifndef abs
|
#ifndef abs
|
||||||
#define abs(x) ((x<0)?(-x):(x))
|
#define abs(x) ((x<0)?(-x):(x))
|
||||||
|
|
||||||
|
|
||||||
int dx = abs(end_x - start_x);
|
int dx = abs(end_x - start_x);
|
||||||
int dy = abs(end_y - start_y);
|
int dy = abs(end_y - start_y);
|
||||||
|
|
||||||
int direction_x = start_x < end_x ? 1 : -1;
|
int direction_x = start_x < end_x ? 1 : -1;
|
||||||
int direction_y = start_y < end_y ? 1 : -1;
|
int direction_y = start_y < end_y ? 1 : -1;
|
||||||
|
|
||||||
printf("dx:%i dy:%i direction:%i,%i", dx, dy, direction_x, direction_y);
|
printf("dx:%i dy:%i direction:%i,%i \n", dx, dy, direction_x, direction_y);
|
||||||
|
|
||||||
if(dx>dy){
|
if(dx>=dy){
|
||||||
for(int i=0; i<dx; i++){
|
float small = direction_y * (float)dy/(float)dx;
|
||||||
dot(c, start_x+i*direction_x, start_y+(dy/(i+1))*direction_y);
|
|
||||||
|
for(int i=0; i<dx+1; i++){
|
||||||
|
dot(c, start_x + i*direction_x, start_y + i*small);
|
||||||
}
|
}
|
||||||
}else{
|
}else{
|
||||||
for(int i=0; i<dy; i++){
|
float small = direction_x * (float)dx/(float)dy;
|
||||||
dot(c, start_x+(dx/(i+1))*direction_x, start_y+i*direction_y);
|
|
||||||
|
for(int i=0; i<dy+1; i++){
|
||||||
|
dot(c, start_x + i*small, start_y + i*direction_y);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -66,16 +69,25 @@ void line(Canvas* c, uint start_x, uint start_y, uint end_x, uint end_y){
|
|||||||
|
|
||||||
int main(){
|
int main(){
|
||||||
char data[] =
|
char data[] =
|
||||||
"---"
|
"----------"
|
||||||
"---"
|
"----------"
|
||||||
"---";
|
"----------"
|
||||||
|
"----------"
|
||||||
|
"----------"
|
||||||
|
"----------"
|
||||||
|
"----------"
|
||||||
|
"----------"
|
||||||
|
"----------"
|
||||||
|
"----------";
|
||||||
|
|
||||||
Canvas a = {3, 3, data};
|
Canvas a = {10, 10, data};
|
||||||
|
|
||||||
//line(&a, 0, 0, 3, 3);
|
//line(&a, 1, 1, 8, 8);
|
||||||
|
line(&a, 8, 8, 1, 1);
|
||||||
|
//line(&a, 5, 5, 8, 8);
|
||||||
|
|
||||||
dot(&a, 0, 0);
|
dot(&a, 0, 0);
|
||||||
dot(&a, 2, 2);
|
dot(&a, 9, 9);
|
||||||
|
|
||||||
render(&a);
|
render(&a);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user