#include #include typedef unsigned char uint8_t; typedef char int8_t; typedef short int16_t; typedef unsigned short uint16_t; #define _BV(x) (1<<(x)) uint8_t bresenham_data[0xff+1]; typedef void (*calc_t)(uint8_t size, uint8_t brightness); typedef struct bresenham_struct { uint8_t size; uint8_t value; int16_t error; uint8_t stepNumber; } bresenham_struct; void bresenham_init(struct bresenham_struct *st, uint8_t size) { st->size = size; } void bresenham_setValue(struct bresenham_struct *st, uint8_t val) { st->stepNumber = 0; st->value = val; st->error = st->size/2; } bool bresenham_getNext(struct bresenham_struct *st) { bool result; st->error -= st->value; if ( st->error < 0 ) { st->error += st->size; result = true; } else { result = false; } if ( ++st->stepNumber >= st->size) { st->stepNumber = 0; st->error = st->size/2; } return result; } void calcBresenham(uint8_t size, uint8_t brightness) { size--; brightness--; int16_t error = size - brightness; uint8_t x = 0; uint8_t y = 0; uint8_t prevY = 1; while ( x <= size ) { const int error2 = error * 2; bool value = y != prevY && brightness != 0xff; bresenham_data[x] = value; prevY = y; if ( error2 > -brightness ) { error -= brightness; x++; } if ( error2 < size ) { error += size; y++; } } } void calcBresenham2(uint8_t size, uint8_t brightness) { size--; brightness--; int16_t error = size - brightness; uint8_t x; uint8_t y = 0; uint8_t prevY = 1; for (x = 0; x <= size; x++) { const int error2 = error * 2; bool value = y != prevY && brightness != 0xff; bresenham_data[x] = value; prevY = y; error -= brightness; if ( error2 < size ) { error += size; y++; } } } void calcBresenham3(uint8_t size, uint8_t brightness) { int16_t error = size - brightness; uint8_t x; for (x = 0; x < size; x++) { const int error2 = error * 2; error -= brightness; if ( error2 < size ) { error += size; bresenham_data[x] = 1; } else { bresenham_data[x] = 0; } } } int minE, maxE; void calcBresenham4(uint8_t size, uint8_t brightness) { int16_t error = size - brightness; uint8_t x; for (x = 0; x < size; x++) { if ( error < size/2 ) { error += size; bresenham_data[x] = 1; } else { bresenham_data[x] = 0; } error -= brightness; } } void calcBresenham5(uint8_t size, uint8_t brightness) { bresenham_struct bs; uint8_t x; bresenham_init(&bs, size); bresenham_setValue(&bs, brightness); for (x = 0; x < size; x++) { bresenham_data[x] = bresenham_getNext(&bs); } for (x = 0; x < size; x++) { if ( bresenham_data[x] != bresenham_getNext(&bs) ) { printf("second pass error error. size = %i, value = %i\n", size, brightness); } } } bool test(calc_t func) { int size; uint8_t val, i; for (size = 2; size < 0xff; size++) { for (val = 0; val <= size; val++) { for (i = 0; i < 0xff; i++) { bresenham_data[i] = 0xff; } func(size, val); uint8_t zerroCnt = 0; uint8_t oneCnt = 0; for (i = 0; i < 0xff; i++) { if ( bresenham_data[i] == 0 ) { zerroCnt++; } else if ( bresenham_data[i] == 1 ) { oneCnt++; } else if ( bresenham_data[i] != 0xff || i < size ) { printf("\tinvalid value for offset %i: %i size = %i, val = %i\n", i, bresenham_data[i], size, val); printf("0 1 2 3 4 5 6 7 8 9\n"); for ( i = 0; i < 10; i++ ) { printf("%i ", bresenham_data[i]); } printf("\n"); return false; } } if (oneCnt != val) { printf("\tinvalid count of '1': %i, for size = %i, val = %i\n", oneCnt, size, val); printf("0 1 2 3 4 5 6 7 8 9\n"); for ( i = 0; i < 10; i++ ) { printf("%i ", bresenham_data[i]); } printf("\n"); return false; } if (zerroCnt != size-val) { printf("\tinvalid count of '0': %i, for size = %i, val = %i\n", zerroCnt, size, val); return false; } } } return true; } int main(int argc, char* argv[]) { int i; printf("TEST 1: %i\n", test(calcBresenham)); printf("TEST 2: %i\n", test(calcBresenham2)); printf("TEST 3: %i\n", test(calcBresenham3)); printf("TEST 4: %i\n", test(calcBresenham4)); printf("TEST 5: %i\n", test(calcBresenham5)); printf("\n\n"); for (i = 0; i <= 10; i++) { printf("-[%i]--------------------------------\n", i); calcBresenham5(10, i); int i; printf("0 1 2 3 4 5 6 7 8 9\n"); for ( i = 0; i < 10; i++ ) { printf("%i ", bresenham_data[i]); } printf("\n"); } }