Skip to content

Instantly share code, notes, and snippets.

@celestialphineas
Created October 3, 2017 13:02
Show Gist options
  • Save celestialphineas/589cb956cadd9e073d3c151be22ad99e to your computer and use it in GitHub Desktop.
Save celestialphineas/589cb956cadd9e073d3c151be22ad99e to your computer and use it in GitHub Desktop.
Efficiency testing between Bresenham’s and floating point methods
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
void write_pixel(int a, int b)
{
int *k = (int*)malloc(2 * sizeof(int));
k[0] = a; k[1] = b;
free(k);
return;
}
int main(void)
{
unsigned i;
unsigned n;
int *r_list;
double t0, t1, t2;
srand((unsigned)time(0));
scanf("%d", &n);
r_list = (unsigned*)malloc(sizeof(unsigned) * n);
for(i = 0; i < n; i++)
r_list[i] = rand();
t0 = clock();
for(i = 0; i < n; i++)
{
int j;
int r = r_list[i];
int x = 0, y = r;
while(x <= y)
{
write_pixel(x, y); write_pixel(y, x);
write_pixel(-x, y); write_pixel(y, -x);
write_pixel(x, -y); write_pixel(-y, x);
write_pixel(-x, -y); write_pixel(-y, -x);
x++;
y = 2*(x*x+y*y-r*r) + 4*x - 2*y + 3 > 0 ? y - 1 : y;
}
}
t1 = clock();
for(i = 0; i < n; i++)
{
int r = r_list[i];
int x = 0, y = r;
while(x <= y)
{
write_pixel(x, y); write_pixel(y, x);
write_pixel(-x, y); write_pixel(y, -x);
write_pixel(x, -y); write_pixel(-y, x);
write_pixel(-x, -y); write_pixel(-y, -x);
x++;
y = sqrt(r*r - x*x) + 0.5;
}
}
t2 = clock();
printf("%lf %lf\n", t1-t0, t2-t1);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment