Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- /******************************************/
- /* Color */
- typedef struct{
- float r;
- float g;
- float b;
- } color_t;
- /* Keeps track of the turtle postion. */
- typedef struct {
- double xPos;
- double yPos;
- double dir;
- } turtle_t;
- /* Keeps track of color + pen + filled */
- typedef struct{
- color_t color;
- int down;
- int filled;
- } pen_t;
- /******************************************/
- /* Function prototypes */
- int valid_color(color_t *c);
- void pen_set_color( pen_t *p, color_t *c);
- void turtle_state( turtle_t *t);
- void turtle_reset( turtle_t *t);
- void pen_reset( pen_t *p );
- int process_commands( turtle_t *t, pen_t *p );
- /*************************************************/
- int main( void ) {
- turtle_t *boo;
- pen_t pen;
- boo = (turtle_t *) malloc(sizeof( turtle_t) );
- boo->xPos = 10;
- boo->yPos = 20;
- turtle_state(boo);
- turtle_reset( boo);
- /*pen_reset( &pen);*/
- turtle_state(boo);
- process_commands(boo, &pen);
- /*Free boo at the end o code*/
- free(boo);
- return 0;
- }
- /* process_commands */
- int process_commands(turtle_t *t, pen_t *p){
- char cmd;
- color_t c;
- float x, y;
- while ((fscanf(stdin, "%c", &cmd ) != EOF)){
- switch (cmd) {
- case 'C':
- scanf("%f\n", &c.r);
- scanf("%f\n", &c.g);
- scanf("%f\n", &c.b);
- fprintf(stdout, "Pen Color (%f, %f, %f)\n", c.r, c.g, c.b );
- if(valid_color(&c) == 1 ){
- pen_set_color(p, &c);
- }
- break;
- case 'G':
- break;
- }
- }
- return 0;
- }
- /*
- turtle_reset()
- */
- void turtle_reset( turtle_t *t ){
- printf("Inside turtle Boo: Xpos: %f yPos: %f \n", (t->xPos), (t->yPos));
- t->xPos = t->yPos = 0;
- t->dir = 0;
- return;
- }
- /*pen reset */
- void pen_reset(pen_t *p){
- p->down = p->filled = 0;
- p->color.r = 0;
- p->color.b = 0;
- p->color.g = 0;
- }
- /* prints out current stae of the turtle */
- void turtle_state(turtle_t *t){
- printf("Current Postion X: %f Y: %f \n", (t->xPos),(t->yPos));
- return;
- }
- /* vaildates the color input */
- int valid_color(color_t *c){
- if(((c->r) => 0 && (c->r) < 1) && ((c->g) => 0 && (c->g) < 1) && ((c->b) => 0 && (c->b) < 1)){
- return 1;
- }else {
- return 0;
- }
- }
- /* sets pen color */
- void pen_set_color( pen_t *p, color_t *c){
- (p->color.r) = c->r;
- (p->color.g) = c->g;
- (p->color.b) = c->b;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement