|
| 1 | +/******************************************************************************************* |
| 2 | +* |
| 3 | +* raylib [shapes] example - pie chart |
| 4 | +* |
| 5 | +* Example complexity rating: [★☆☆☆] 1/4 |
| 6 | +* |
| 7 | +* Example originally created with raylib 5.5, last time updated with raylib 5.6 |
| 8 | +* |
| 9 | +* Example contributed by Gideon Serfontein (@GideonSerf) and reviewed by Ramon Santamaria (@raysan5) |
| 10 | +* |
| 11 | +* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified, |
| 12 | +* BSD-like license that allows static linking with closed source software |
| 13 | +* |
| 14 | +* Copyright (c) 2025 Gideon Serfontein (@GideonSerf) |
| 15 | +* |
| 16 | +********************************************************************************************/ |
| 17 | + |
| 18 | +#include "raylib.h" |
| 19 | + |
| 20 | +//------------------------------------------------------------------------------------ |
| 21 | +// Program main entry point |
| 22 | +//------------------------------------------------------------------------------------ |
| 23 | +int main(void) |
| 24 | +{ |
| 25 | + // Initialization |
| 26 | + //-------------------------------------------------------------------------------------- |
| 27 | + const int screenWidth = 800; |
| 28 | + const int screenHeight = 450; |
| 29 | + |
| 30 | + InitWindow(screenWidth, screenHeight, "raylib [shapes] example - pie chart"); |
| 31 | + |
| 32 | + // Pie slice angles (must sum to 360) |
| 33 | + const int angles[] = { 30, 10, 45, 35, 60, 38, 75, 67 }; |
| 34 | + const int anglesCount = (int)(sizeof(angles)/sizeof(angles[0])); |
| 35 | + |
| 36 | + const float radius = 150.0f; |
| 37 | + const Vector2 center = { screenWidth/2.0f, screenHeight/2.0f }; |
| 38 | + |
| 39 | + // Set our game to run at 60 frames-per-second |
| 40 | + SetTargetFPS(60); |
| 41 | + //-------------------------------------------------------------------------------------- |
| 42 | + |
| 43 | + // Main game loop |
| 44 | + while (!WindowShouldClose()) // Detect window close button or ESC key |
| 45 | + { |
| 46 | + // Draw |
| 47 | + //---------------------------------------------------------------------------------- |
| 48 | + BeginDrawing(); |
| 49 | + |
| 50 | + ClearBackground(RAYWHITE); |
| 51 | + |
| 52 | + float lastAngleDeg = 0.0f; |
| 53 | + |
| 54 | + // Number of radial segments for sector drawing |
| 55 | + const int segments = 100; |
| 56 | + |
| 57 | + for (int i = 0; i < anglesCount; i++) |
| 58 | + { |
| 59 | + // Gradient gray color per slice |
| 60 | + unsigned char gray = (unsigned char)((i/(float)anglesCount)*255.0f); |
| 61 | + |
| 62 | + // Draw pie piece using DrawCircleSector() |
| 63 | + DrawCircleSector(center, radius, lastAngleDeg, lastAngleDeg + (float)angles[i], |
| 64 | + segments, (Color){ gray, gray, gray, 255 }); |
| 65 | + |
| 66 | + lastAngleDeg += (float)angles[i]; |
| 67 | + } |
| 68 | + |
| 69 | + DrawText("Pie chart drawing example", 260, 400, 20, LIGHTGRAY); |
| 70 | + |
| 71 | + EndDrawing(); |
| 72 | + //---------------------------------------------------------------------------------- |
| 73 | + } |
| 74 | + |
| 75 | + // De-Initialization |
| 76 | + //-------------------------------------------------------------------------------------- |
| 77 | + CloseWindow(); // Close window and OpenGL context |
| 78 | + //-------------------------------------------------------------------------------------- |
| 79 | + |
| 80 | + return 0; |
| 81 | +} |
0 commit comments