Skip to content

Conversation

oqu
Copy link

@oqu oqu commented Oct 5, 2025

TL;DR #5209 is asking for more examples. This PR is for the example shapes_math_sine_cosine

Summary

Added a new example shapes_math_sine_cosine.c that demonstrates the relationship
between the unit circle and the sine / cosine functions. The example is a C port
of the p5.js "angles and motion: sine/cosine" demo and uses raylib for rendering
and raygui for a small control panel.

Key features:

  • Interactive animation of a point moving around a circle (uniform circular motion).
  • Sine (red) and Cosine (orange) graphs plotted below the circle.
  • Vertical tracker line and moving graph points synchronized with the circular motion.
  • Play / Pause control implemented with a raygui toggle.
  • Control panel positioned on the right side of the window.

Changelog

  • examples/shapes/shapes_math_sine_cosine.c — NEW: main example source
  • examples/shapes/shapes_math_sine_cosine.png — optional screenshot.

Possible areas of improvement

  • Make the circle and line thicker
  • Use constant for the menu layout

Tests

Manually tested on macos by building raylib, then building the example.

cd raylib/src
make -s

cd ../examples
make -s shapes/shapes_math_sine_cosine

Let me know if you need any changes

@raysan5 raysan5 changed the title [examples] Add examples shapes_math_sine_cosine (#5209) [examples] Add shapes_math_sine_cosine (#5209) Oct 7, 2025
@raysan5
Copy link
Owner

raysan5 commented Oct 7, 2025

@oqu Thanks! It looks nice! Please, could you change the background to RAYWHITE, for consistency with most other examples?


int main(void)
{
// Window initialization
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that Init, Update, Draw sections should be defined by:
//--------------------------------------------------------------------------------------
following the template structure: https://github.com/raysan5/raylib/blob/master/examples/examples_template.c

const int screenHeight = 450;

InitWindow(screenWidth, screenHeight, "raylib [shapes] example - math sine cosine");
const char* playText = "Play";
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, review raylib code conventions: https://github.com/raysan5/raylib/blob/master/CONVENTIONS.md


// Animation / UI state
bool playing = true; // play / pause toggle
int frameCountLocal = 0; // local frame counter (used when playing)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, review raylib code conventions: https://github.com/raysan5/raylib/blob/master/CONVENTIONS.md

Comments should start with capital letter

// Main loop
while (!WindowShouldClose())
{
// Handle GUI and input first
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, note that Update and Draw should be in separate sections.

DrawLine((int)(circleX - circleRadius), (int)circleY, (int)(circleX + circleRadius), (int)circleY, Fade(GRAY, 0.6f));

// Compute moving point on circle (cos for x, sin for y). Note: cos/sin in C expect radians
float angRad = angleDeg * (PI / 180.0f);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, review raylib code conventions: https://github.com/raysan5/raylib/blob/master/CONVENTIONS.md

* and / should not have spaces

@raysan5 raysan5 changed the title [examples] Add shapes_math_sine_cosine (#5209) [examples] Added shapes_math_sine_cosine (#5209) Oct 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants