Advertisement
Guest User

adasdad

a guest
Nov 20th, 2017
394
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.75 KB | None | 0 0
  1. /**
  2.  * @file fenster.c
  3.  *
  4.  * @author Laura Vettin <laura.vettin@ivei.uni-stuttgart.de>
  5.  * @date 06.11.2017
  6.  * @brief Draws a rectangle and many circles
  7.  * TODO: color depending on position
  8.  */
  9.  
  10. #include <gdpc.h>
  11. #include <gdptools.h>
  12. #include <stdlib.h>
  13.  
  14. _Bool touching(int x, int y, int r, int x_r, int y_r, int w_r, int h_r, int mode);
  15.  
  16. int main(void) {
  17.  
  18.   srand(time(0));
  19.   const int w = 500, h = 500;
  20.   // create the window
  21.   GDPC* c=GDPC_create(w, h);
  22.   // draw an rectangle
  23.   const int x_r = w/2;
  24.   const int y_r = h/2;
  25.   const int w_r = 100;
  26.   const int h_r = 50;
  27.   GDPC_rectangle(c, x_r, y_r, w_r, h_r, 1);
  28.  
  29.  
  30.   // draw many blue circles
  31.   int x = 0, y = 0;
  32.   int radius = 10;
  33.   for(int i = 0; i < 200; i++){
  34.     //x, y central values
  35.      x = radius+rand()%(w-2*radius);
  36.      y = radius+rand()%(h-2*radius);
  37.      // TODO: blue only outside rect, green inside rect
  38.      //if(((x >= x_r) && (y >= y_r)) && ((x <= x_r+w_r) && (y <= y_r+h_r)))
  39.      if (touching(x, y, radius, x_r, y_r, w_r, h_r, 1))
  40.        GDPC_color(c, 0, 255, 0); // BLUE
  41.  
  42.      else
  43.        GDPC_color(c, 0, 0, 255); // BLUE
  44.  
  45.  
  46.  
  47.      GDPC_circle(c, x, y, radius, 1);
  48.   }
  49.  
  50.  
  51. if(GDPC_destroy(c))  exit(1);
  52.     return 0;
  53. }
  54.  
  55. _Bool touching(int x, int y, int r, int x_r, int y_r, int w_r, int h_r, int mode)
  56. {
  57.   switch(mode)
  58.   {
  59.   case 0:
  60.     if(((x >= x_r) && (y >= y_r)) && ((x <= x_r+w_r) && (y <= y_r+h_r)))
  61.       return 1;
  62.     else
  63.       return 0;
  64.     break;
  65.   case 1:
  66.     if((((x + r >= x_r) && (y + r >= y_r)) || ((x - r >= x_r) && (y - r >= y_r)))
  67.         && (((x + r <= x_r+w_r) && (y + r <= y_r+h_r)) || ((x - r <= x_r+w_r) && (y - r <= y_r+h_r))))
  68.        return 1;
  69.     else
  70.       return 0;
  71.     break;
  72.   default: //do nothing
  73.     break;
  74.   }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement