Last active
January 5, 2016 20:24
-
-
Save saucecode/a61fe4200c3abceb554f to your computer and use it in GitHub Desktop.
first working version
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
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <math.h> | |
#define PI 3.14159265 | |
// https://www.reddit.com/r/dailyprogrammer/comments/3zfajl/20160104_challenge_248_easy_draw_me_like_one_of/ | |
struct rgb_t { | |
unsigned char r; | |
unsigned char g; | |
unsigned char b; | |
}; | |
typedef struct rgb_t rgb_t; | |
void printGraph(int outputWidth, int outputHeight, rgb_t *pixels){ | |
int x, y; | |
for(y = 0; y<outputHeight; y+=1){ | |
for(x = 0; x<outputWidth; x+=1){ | |
rgb_t *pixel = &pixels[y*outputWidth + x]; | |
printf("%i %i %i ", pixel->r, pixel->g, pixel->b); | |
if(x != outputWidth-1) printf(" "); | |
} | |
printf("\n"); | |
} | |
} | |
int main(int argc, char **argv){ | |
if(argc == 1){ | |
printf("Requires input file.\n"); | |
return 1; | |
} | |
rgb_t BACKGROUND_COLOR = {0,0,0}; | |
char *filename = argv[1]; | |
// printf("Loading file: %s\n", filename); | |
// Read the input file. | |
FILE *fd = fopen(filename, "rb"); | |
fseek(fd, 0, SEEK_END); | |
int filesize = ftell(fd); | |
rewind(fd); | |
char *buffer = (char*) malloc(filesize+1); | |
fread(buffer, sizeof(char), filesize, fd); | |
buffer[filesize] = '\0'; | |
fclose(fd); | |
// Print our input. | |
// printf("INPUT:\n%s\n", buffer); | |
// Replace new lines and spaces with null bytes. | |
int x, count = 0; | |
for(x = 0; x<filesize; x+=1){ | |
if(buffer[x] == '\n' || buffer[x] == ' '){ | |
buffer[x] = '\0'; | |
count += 1; | |
} | |
} | |
// Convert to a C-string array for easy access! | |
char *elements[count]; | |
elements[0] = buffer; | |
int ncount=1; | |
for(x = 0; x<filesize; x+=1){ | |
if(buffer[x] == '\0'){ | |
elements[ncount] = buffer+x+1; // +1 to skip over the null byte. | |
ncount += 1; | |
} | |
} | |
// Create 1D pixel array - left to right, top to bottom. | |
int outputWidth = atoi(elements[0]); | |
int outputHeight = atoi(elements[1]); | |
rgb_t *pixels = (rgb_t*) malloc(outputWidth * outputHeight * sizeof(rgb_t)); | |
memset(pixels, '\0', outputWidth * outputHeight * sizeof(rgb_t)); | |
// Begin translation to ppm. | |
int index = 2; | |
char *position = elements[index]; | |
while(1){ | |
if(strcmp(position, "point") == 0){ | |
int y = atoi(elements[index+4]), x = atoi(elements[index+5]); | |
rgb_t *pixel = &pixels[y*outputHeight + x]; | |
pixel->r = (unsigned char) atoi(elements[index+1]); | |
pixel->g = (unsigned char) atoi(elements[index+2]); | |
pixel->b = (unsigned char) atoi(elements[index+3]); | |
index += 6; // jump ahead 6 elements | |
if(index > count) break; | |
position = elements[index]; | |
}else if(strcmp(position, "line") == 0){ | |
int sx = atoi(elements[index+5]), sy = atoi(elements[index+4]); | |
int w = atoi(elements[index+7])-sx, h = atoi(elements[index+6])-sy; | |
rgb_t color = { | |
atoi(elements[index+1]), | |
atoi(elements[index+2]), | |
atoi(elements[index+3]) | |
}; | |
// Write the start and end of the line. | |
rgb_t *pixel_start = &pixels[sy*outputWidth + sx]; | |
pixel_start->r = color.r; | |
pixel_start->g = color.g; | |
pixel_start->b = color.b; | |
rgb_t *pixel_end = &pixels[(sy+h)*outputWidth + sx+w]; | |
pixel_end->r = color.r; | |
pixel_end->g = color.g; | |
pixel_end->b = color.b; | |
// Determine the angle of attack. | |
double theta = atan2(h, w); | |
double xStep = cos(theta), yStep = sin(theta); | |
double xi = (double) sx; | |
double yi = (double) sy; | |
double distance = sqrt( w*w + h*h ); | |
double distanceTraveled = 0; | |
// Integrate to destination at sx+w,sy+h! | |
while(1){ | |
xi += xStep; | |
yi += yStep; | |
distanceTraveled += 1;// sin^2(x) + cos^2(x) = 1 | |
if(distanceTraveled >= distance-1) break; | |
int cx = round(xi), cy = round(yi); | |
rgb_t *pixel = &pixels[cy*outputWidth + cx]; | |
pixel->r = color.r; | |
pixel->g = color.g; | |
pixel->b = color.b; | |
} | |
index += 8; // jump ahead 8 elements | |
if(index > count) break; | |
position = elements[index]; | |
}else if(strcmp(position, "rect") == 0){ | |
int y = atoi(elements[index+4]), x = atoi(elements[index+5]); | |
int w = atoi(elements[index+7]), h = atoi(elements[index+6]); | |
rgb_t color = { | |
atoi(elements[index+1]), | |
atoi(elements[index+2]), | |
atoi(elements[index+3]) | |
}; | |
int i,j; | |
for(i = y; i<h+y; i+=1){ | |
for(j = x; j<w+x; j+=1){ | |
rgb_t *pixel = &pixels[(i)*outputWidth + j]; | |
pixel->r = color.r; | |
pixel->g = color.g; | |
pixel->b = color.b; | |
} | |
} | |
index += 8; // jump ahead 8 elements | |
if(index >= count) break; | |
position = elements[index]; | |
}else{ | |
// Translation finished. | |
break; | |
} | |
} | |
printf("P3 %s %s 255\n", elements[0], elements[1]); | |
printGraph(outputWidth,outputHeight, pixels); | |
free(buffer); | |
free(pixels); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment