Advertisement
Broatlas

Turlte

Feb 28th, 2016
333
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.41 KB | None | 0 0
  1.  
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. /******************************************/
  5. /* Color */
  6. typedef struct{
  7.   float r;
  8.   float g;
  9.   float b;
  10. } color_t;
  11. /* Keeps track of the turtle postion. */
  12. typedef struct {
  13.   double xPos;
  14.   double yPos;
  15.   double dir;
  16. } turtle_t;
  17.  
  18. /* Keeps track of color + pen + filled */
  19. typedef struct{
  20.   color_t color;
  21.   int down;
  22.   int filled;
  23. } pen_t;
  24.  
  25.  
  26. /******************************************/
  27. /* Function prototypes */
  28. int valid_color(color_t *c);
  29. void pen_set_color( pen_t *p, color_t *c);
  30. void turtle_state( turtle_t *t);
  31. void turtle_reset( turtle_t *t);
  32. void pen_reset( pen_t *p );
  33. int process_commands( turtle_t *t, pen_t *p );
  34. /*************************************************/
  35. int main( void ) {
  36.   turtle_t *boo;
  37.   pen_t pen;
  38.  boo = (turtle_t *) malloc(sizeof( turtle_t) );
  39.  
  40.  boo->xPos = 10;
  41.  boo->yPos = 20;
  42.  
  43.   turtle_state(boo);
  44.  
  45.   turtle_reset( boo);
  46.   /*pen_reset( &pen);*/
  47.   turtle_state(boo);
  48.   process_commands(boo, &pen);
  49.   /*Free boo at the end o code*/
  50.   free(boo);
  51.   return 0;
  52.  
  53.  
  54. }
  55. /* process_commands */
  56. int process_commands(turtle_t *t, pen_t *p){
  57.   char cmd;
  58.   color_t c;
  59.   float x, y;
  60.  
  61.   while ((fscanf(stdin, "%c", &cmd ) != EOF)){
  62.       switch (cmd) {
  63.         case 'C':
  64.           scanf("%f\n", &c.r);
  65.           scanf("%f\n", &c.g);
  66.           scanf("%f\n", &c.b);
  67.           fprintf(stdout, "Pen Color (%f, %f, %f)\n", c.r, c.g, c.b );
  68.           if(valid_color(&c) == 1 ){
  69.             pen_set_color(p, &c);
  70.           }
  71.         break;
  72.         case 'G':
  73.         break;
  74.       }
  75.   }
  76.   return 0;
  77.  
  78. }
  79.  
  80. /*
  81. turtle_reset()
  82. */
  83. void turtle_reset( turtle_t *t ){
  84.   printf("Inside turtle Boo: Xpos: %f yPos: %f \n", (t->xPos), (t->yPos));
  85.   t->xPos = t->yPos = 0;
  86.   t->dir = 0;
  87.   return;
  88. }
  89. /*pen reset */
  90. void pen_reset(pen_t *p){
  91.   p->down = p->filled = 0;
  92.   p->color.r = 0;
  93.   p->color.b = 0;
  94.   p->color.g = 0;
  95. }
  96. /* prints out current stae of the turtle */
  97.  
  98. void turtle_state(turtle_t *t){
  99.   printf("Current Postion X: %f Y: %f \n", (t->xPos),(t->yPos));
  100.   return;
  101. }
  102. /* vaildates the color input */
  103.  
  104. int valid_color(color_t *c){
  105.   if(((c->r) => 0 && (c->r) < 1) && ((c->g) => 0 && (c->g) < 1) && ((c->b) => 0 && (c->b) < 1)){
  106.     return 1;
  107.   }else {
  108.     return 0;
  109.   }
  110.  
  111. }
  112.  
  113. /* sets pen color */
  114.  
  115. void pen_set_color( pen_t *p, color_t *c){
  116.   (p->color.r) = c->r;
  117.   (p->color.g) = c->g;
  118.   (p->color.b) = c->b;
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement