#include "raylib.h"
#include <stdcountof.h>
#include <stdlib.h>
constexpr int SCREEN_WIDTH = 400, SCREEN_HEIGHT = 400, SHAPE_COUNT = 100,
FPS = 1, // Frames per second
RADIUS_MAX = 20, // Maximum shape radius
RADIUS_MIN = 5; // Minimum shape radius
constexpr char TITLE[] = "Shape Collage";
Color RAYLIB_COLORS[] = {
// These colors are defined in `raylib.h`
// Used to pick a random color.
LIGHTGRAY, GRAY, DARKGRAY, YELLOW, GOLD, ORANGE, PINK,
RED, MAROON, GREEN, LIME, DARKGREEN, SKYBLUE, BLUE,
DARKBLUE, PURPLE, VIOLET, DARKPURPLE, BEIGE, BROWN, DARKBROWN,
};
typedef enum { CIRCLE, ELLIPSE, TRIANGLE, RECTANGLE } ShapeForm;
constexpr size_t SHAPE_TYPE_COUNT = 4;
typedef struct {
//// BEGIN SOLUTION
Color color;
ShapeForm form;
union {
struct {
Vector2 center; // position
float radius;
} circle;
struct {
Vector2 center;
float radiusH, radiusV;
} ellipse;
struct {
Vector2 v1, v2, v3;
} triangle;
struct {
Vector2 pos, size;
} rectangle;
};
//// END SOLUTION
} Shape;
Shape shapes[SHAPE_COUNT];
//// BEGIN SOLUTION
float random_radius() {
return rand() % (RADIUS_MAX - RADIUS_MIN) + RADIUS_MIN;
}
Vector2 random_vector2() {
return (Vector2){rand() % SCREEN_WIDTH, rand() % SCREEN_HEIGHT};
}
//// END SOLUTION
// Initializes shapes with random features.
// Returns `shapes`
Shape *initialize_shapes() {
//// BEGIN SOLUTION
for (size_t i = 0; i < countof shapes; ++i) {
auto s = &shapes[i];
s->color = RAYLIB_COLORS[rand() % sizeof RAYLIB_COLORS];
s->form = rand() % SHAPE_TYPE_COUNT;
switch (s->form) {
case CIRCLE:
s->circle.center = random_vector2();
s->circle.radius = random_radius();
break;
case ELLIPSE:
s->ellipse.center = random_vector2();
s->ellipse.radiusH = random_radius();
s->ellipse.radiusV = random_radius();
break;
case TRIANGLE:
s->triangle.v1 = random_vector2();
s->triangle.v2 = random_vector2();
s->triangle.v3 = random_vector2();
break;
case RECTANGLE:
s->rectangle.pos = random_vector2();
s->rectangle.size = random_vector2();
}
}
//// END SOLUTION
return shapes;
}
void draw_shape(const Shape *s) {
//// BEGIN SOLUTION
switch (s->form) {
case CIRCLE:
auto c = s->circle;
DrawCircleV(c.center, c.radius, s->color);
break;
case ELLIPSE:
auto e = s->ellipse;
DrawEllipseV(e.center, e.radiusH, e.radiusV, s->color);
break;
case TRIANGLE:
auto t = s->triangle;
DrawTriangle(t.v1, t.v2, t.v3, s->color);
break;
case RECTANGLE:
auto r = s->rectangle;
DrawRectangleV(r.pos, r.size, s->color);
}
//// END SOLUTION
}
void draw_elements() {
for (size_t i = 0; i < countof shapes; ++i) {
draw_shape(&shapes[i]);
}
}
int main() {
InitWindow(SCREEN_WIDTH, SCREEN_HEIGHT, TITLE);
SetTargetFPS(FPS);
initialize_shapes();
while (!WindowShouldClose()) {
BeginDrawing();
ClearBackground(RAYWHITE);
draw_elements();
EndDrawing();
}
TakeScreenshot("shape_collage.png");
CloseWindow();
}