Created
May 14, 2014 14:46
-
-
Save kube/ad334ac978cfdc54ffa0 to your computer and use it in GitHub Desktop.
Simple Antialiased-Line Drawing
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
/*#######. | |
draw_aa_line.c ########",#: | |
#########',##". | |
KUBE : www.kube.io ##'##'##".##',##. | |
Created : Dec 21 2013 23:33:03 ## ## ## # ##",#. | |
Modified : May 14 2014 16:42:04 ## ## ## ## ##' | |
## ## ## :## | |
## ## ##*/ | |
#include <stdlib.h> | |
typedef struct s_point | |
{ | |
float x; | |
float y; | |
} t_point; | |
void pixel_put(float x, float y, int color, float opacity); | |
static void draw_loop_x(t_point *start, t_point *end, int color) | |
{ | |
float derivative; | |
t_point current; | |
current.x = (int)start->x; | |
current.y = start->y; | |
derivative = (end->y - start->y) / (end->x - start->x); | |
while ((int)current.x <= (int)end->x) | |
{ | |
pixel_put(current.x, current.y, color, | |
current.y - (int)current.y); | |
current.y--; | |
pixel_put(current.x, current.y, color, | |
1 - (current.y - (int)current.y)); | |
current.y++; | |
current.x = current.x + 1; | |
current.y = current.y + derivative; | |
} | |
} | |
static void draw_loop_y(t_point *start, t_point *end, int color) | |
{ | |
float derivative; | |
t_point current; | |
current.x = start->x; | |
current.y = (int)start->y; | |
derivative = (end->x - start->x) / (end->y - start->y); | |
while ((int)current.y <= (int)end->y) | |
{ | |
pixel_put(current.x, current.y, color, | |
current.x - (int)current.x); | |
current.x--; | |
pixel_put(current.x, current.y, color, | |
1 - (current.x - (int)current.x)); | |
current.x++; | |
current.x = current.x + derivative; | |
current.y = current.y + 1; | |
} | |
} | |
static void swap_points(t_point **a, t_point **b) | |
{ | |
t_point *cache_point; | |
cache_point = *a; | |
*a = *b; | |
*b = cache_point; | |
} | |
void draw_line(t_point *a, t_point *b, int color) | |
{ | |
if (abs((int)(a->x - b->x)) <= abs((int)(a->y - b->y))) | |
{ | |
if (b->y < a->y) | |
swap_points(&a, &b); | |
draw_loop_y(a, b, color); | |
} | |
else | |
{ | |
if (b->x < a->x) | |
swap_points(&a, &b); | |
draw_loop_x(a, b, color); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment