Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * @file fenster.c
- *
- * @author Laura Vettin <laura.vettin@ivei.uni-stuttgart.de>
- * @date 06.11.2017
- * @brief Draws a rectangle and many circles
- * TODO: color depending on position
- */
- #include <gdpc.h>
- #include <gdptools.h>
- #include <stdlib.h>
- _Bool touching(int x, int y, int r, int x_r, int y_r, int w_r, int h_r, int mode);
- int main(void) {
- srand(time(0));
- const int w = 500, h = 500;
- // create the window
- GDPC* c=GDPC_create(w, h);
- // draw an rectangle
- const int x_r = w/2;
- const int y_r = h/2;
- const int w_r = 100;
- const int h_r = 50;
- GDPC_rectangle(c, x_r, y_r, w_r, h_r, 1);
- // draw many blue circles
- int x = 0, y = 0;
- int radius = 10;
- for(int i = 0; i < 200; i++){
- //x, y central values
- x = radius+rand()%(w-2*radius);
- y = radius+rand()%(h-2*radius);
- // TODO: blue only outside rect, green inside rect
- //if(((x >= x_r) && (y >= y_r)) && ((x <= x_r+w_r) && (y <= y_r+h_r)))
- if (touching(x, y, radius, x_r, y_r, w_r, h_r, 1))
- GDPC_color(c, 0, 255, 0); // BLUE
- else
- GDPC_color(c, 0, 0, 255); // BLUE
- GDPC_circle(c, x, y, radius, 1);
- }
- if(GDPC_destroy(c)) exit(1);
- return 0;
- }
- _Bool touching(int x, int y, int r, int x_r, int y_r, int w_r, int h_r, int mode)
- {
- switch(mode)
- {
- case 0:
- if(((x >= x_r) && (y >= y_r)) && ((x <= x_r+w_r) && (y <= y_r+h_r)))
- return 1;
- else
- return 0;
- break;
- case 1:
- if((((x + r >= x_r) && (y + r >= y_r)) || ((x - r >= x_r) && (y - r >= y_r)))
- && (((x + r <= x_r+w_r) && (y + r <= y_r+h_r)) || ((x - r <= x_r+w_r) && (y - r <= y_r+h_r))))
- return 1;
- else
- return 0;
- break;
- default: //do nothing
- break;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement