shapes_math_sine_cosine.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*******************************************************************************************
  2. *
  3. * raylib [shapes] example - math sine cosine
  4. *
  5. * Example complexity rating: [★★☆☆] 2/4
  6. *
  7. * Example originally created with raylib 5.6-dev, last time updated with raylib 5.6-dev
  8. *
  9. * Example contributed by Jopestpe (@jopestpe) 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 Jopestpe (@jopestpe)
  15. *
  16. ********************************************************************************************/
  17. #include "raylib.h"
  18. #include <math.h>
  19. #include "raymath.h"
  20. #define RAYGUI_IMPLEMENTATION
  21. #include "raygui.h" // Required for GUI controls
  22. // Wave points for sine/cosine visualization
  23. #define WAVE_POINTS 36
  24. //------------------------------------------------------------------------------------
  25. // Program main entry point
  26. //------------------------------------------------------------------------------------
  27. int main(void)
  28. {
  29. // Initialization
  30. //--------------------------------------------------------------------------------------
  31. const int screenWidth = 800;
  32. const int screenHeight = 450;
  33. SetConfigFlags(FLAG_MSAA_4X_HINT);
  34. InitWindow(screenWidth, screenHeight, "raylib [shapes] example - math sine cosine");
  35. Vector2 sinePoints[WAVE_POINTS];
  36. Vector2 cosPoints[WAVE_POINTS];
  37. Vector2 center = { (screenWidth/2.0f) - 30.0f, screenHeight/2.0f };
  38. Rectangle start = { 20.0f, screenHeight - 120.f , 200.0f, 100.0f};
  39. float radius = 130.0f;
  40. float angle = 0.0f;
  41. bool pause = false;
  42. for (int i = 0; i < WAVE_POINTS; i++)
  43. {
  44. float t = i/(float)(WAVE_POINTS - 1);
  45. float currentAngle = t*360.0f*DEG2RAD;
  46. sinePoints[i] = (Vector2){ start.x + t*start.width, start.y + start.height/2.0f - sinf(currentAngle)*(start.height/2.0f) };
  47. cosPoints[i] = (Vector2){ start.x + t*start.width, start.y + start.height/2.0f - cosf(currentAngle)*(start.height/2.0f) };
  48. }
  49. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  50. //--------------------------------------------------------------------------------------
  51. // Main game loop
  52. while (!WindowShouldClose()) // Detect window close button or ESC key
  53. {
  54. // Update
  55. //----------------------------------------------------------------------------------
  56. float angleRad = angle*DEG2RAD;
  57. float cosRad = cosf(angleRad);
  58. float sinRad = sinf(angleRad);
  59. Vector2 point = { center.x + cosRad*radius, center.y - sinRad*radius };
  60. Vector2 limitMin = { center.x - radius, center.y - radius };
  61. Vector2 limitMax = { center.x + radius, center.y + radius };
  62. float complementary = 90.0f - angle;
  63. float supplementary = 180.0f - angle;
  64. float explementary = 360.0f - angle;
  65. float tangent = Clamp(tanf(angleRad), -10.0f, 10.0f);
  66. float cotangent = (fabsf(tangent) > 0.001f) ? Clamp(1.0f/tangent, -radius, radius) : 0.0f;
  67. Vector2 tangentPoint = { center.x + radius, center.y - tangent*radius };
  68. Vector2 cotangentPoint = { center.x + cotangent*radius, center.y - radius };
  69. angle = Wrap(angle + (!pause ? 1.0f : 0.0f), 0.0f, 360.0f);
  70. //----------------------------------------------------------------------------------
  71. // Draw
  72. //----------------------------------------------------------------------------------
  73. BeginDrawing();
  74. ClearBackground(RAYWHITE);
  75. // Cotangent (orange)
  76. DrawLineEx((Vector2){ center.x , limitMin.y }, (Vector2){ cotangentPoint.x, limitMin.y }, 2.0f, ORANGE);
  77. DrawLineDashed(center, cotangentPoint, 10.0f, 4.0f, ORANGE);
  78. // Side background
  79. DrawLine(580, 0, 580, GetScreenHeight(), (Color){ 218, 218, 218, 255 });
  80. DrawRectangle(580, 0, GetScreenWidth(), GetScreenHeight(), (Color){ 232, 232, 232, 255 });
  81. // Base circle and axes
  82. DrawCircleLinesV(center, radius, GRAY);
  83. DrawLineEx((Vector2){ center.x, limitMin.y }, (Vector2){ center.x, limitMax.y }, 1.0f, GRAY);
  84. DrawLineEx((Vector2){ limitMin.x, center.y }, (Vector2){ limitMax.x, center.y }, 1.0f, GRAY);
  85. // Wave graph axes
  86. DrawLineEx((Vector2){ start.x , start.y }, (Vector2){ start.x , start.y + start.height }, 2.0f, GRAY);
  87. DrawLineEx((Vector2){ start.x + start.width, start.y }, (Vector2){ start.x + start.width, start.y + start.height }, 2.0f, GRAY);
  88. DrawLineEx((Vector2){ start.x, start.y + start.height/2 }, (Vector2){ start.x + start.width, start.y + start.height/2 }, 2.0f, GRAY);
  89. // Wave graph axis labels
  90. DrawText("1", start.x - 8, start.y, 6, GRAY);
  91. DrawText("0", start.x - 8, start.y + start.height/2 - 6, 6, GRAY);
  92. DrawText("-1", start.x - 12, start.y + start.height - 8, 6, GRAY);
  93. DrawText("0", start.x - 2, start.y + start.height + 4, 6, GRAY);
  94. DrawText("360", start.x + start.width - 8, start.y + start.height + 4, 6, GRAY);
  95. // Sine (red - vertical)
  96. DrawLineEx((Vector2){ center.x, center.y }, (Vector2){ center.x, point.y }, 2.0f, RED);
  97. DrawLineDashed((Vector2){ point.x, center.y }, (Vector2){ point.x, point.y }, 10.0f, 4.0f, RED);
  98. DrawText(TextFormat("Sine %.2f", sinRad), 640, 190, 6, RED);
  99. DrawCircleV((Vector2){ start.x + (angle/360.0f)*start.width, start.y + ((-sinRad + 1)*start.height/2.0f) }, 4.0f, RED);
  100. DrawSplineLinear(sinePoints, WAVE_POINTS, 1.0f, RED);
  101. // Cosine (blue - horizontal)
  102. DrawLineEx((Vector2){ center.x, center.y }, (Vector2){ point.x, center.y }, 2.0f, BLUE);
  103. DrawLineDashed((Vector2){ center.x , point.y }, (Vector2){ point.x, point.y }, 10.0f, 4.0f, BLUE);
  104. DrawText(TextFormat("Cosine %.2f", cosRad), 640, 210, 6, BLUE);
  105. DrawCircleV((Vector2){ start.x + (angle/360.0f)*start.width, start.y + ((-cosRad + 1)*start.height/2.0f) }, 4.0f, BLUE);
  106. DrawSplineLinear(cosPoints, WAVE_POINTS, 1.0f, BLUE);
  107. // Tangent (purple)
  108. DrawLineEx((Vector2){ limitMax.x , center.y }, (Vector2){ limitMax.x, tangentPoint.y }, 2.0f, PURPLE);
  109. DrawLineDashed(center, tangentPoint, 10.0f, 4.0f, PURPLE);
  110. DrawText(TextFormat("Tangent %.2f", tangent), 640, 230, 6, PURPLE);
  111. // Cotangent (orange)
  112. DrawText(TextFormat("Cotangent %.2f", cotangent), 640, 250, 6, ORANGE);
  113. // Complementary angle (beige)
  114. DrawCircleSectorLines(center, radius*0.6f , -angle, -90.0f , 36.0f, BEIGE);
  115. DrawText(TextFormat("Complementary %0.f°",complementary), 640, 150, 6, BEIGE);
  116. // Supplementary angle (darkblue)
  117. DrawCircleSectorLines(center, radius*0.5f , -angle, -180.0f , 36.0f, DARKBLUE);
  118. DrawText(TextFormat("Supplementary %0.f°",supplementary), 640, 130, 6, DARKBLUE);
  119. // Explementary angle (pink)
  120. DrawCircleSectorLines(center, radius*0.4f , -angle, -360.0f , 36.0f, PINK);
  121. DrawText(TextFormat("Explementary %0.f°",explementary), 640, 170, 6, PINK);
  122. // Current angle - arc (lime), radius (black), endpoint (black)
  123. DrawCircleSectorLines(center, radius*0.7f , -angle, 0.0f, 36.0f, LIME);
  124. DrawLineEx((Vector2){ center.x , center.y }, point, 2.0f, BLACK);
  125. DrawCircleV(point, 4.0f, BLACK);
  126. // Draw GUI controls
  127. //------------------------------------------------------------------------------
  128. GuiSetStyle(LABEL, TEXT_COLOR_NORMAL, ColorToInt(GRAY));
  129. GuiToggle((Rectangle){ 640, 70, 120, 20}, TextFormat("Pause"), &pause);
  130. GuiSetStyle(LABEL, TEXT_COLOR_NORMAL, ColorToInt(LIME));
  131. GuiSliderBar((Rectangle){ 640, 40, 120, 20}, "Angle", TextFormat("%.0f°", angle), &angle, 0.0f, 360.0f);
  132. // Angle values panel
  133. GuiGroupBox((Rectangle){ 620, 110, 140, 170}, "Angle Values");
  134. //------------------------------------------------------------------------------
  135. DrawFPS(10, 10);
  136. EndDrawing();
  137. //----------------------------------------------------------------------------------
  138. }
  139. // De-Initialization
  140. //--------------------------------------------------------------------------------------
  141. CloseWindow(); // Close window and OpenGL context
  142. //--------------------------------------------------------------------------------------
  143. return 0;
  144. }