arc.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /* Nothing interesting here, just testing the arc function...
  2. * There bunch of points in top left stacked on top of each other...
  3. * You can drag them around and see what they do I guess...
  4. */
  5. #include "common/sketchbook.hpp"
  6. constexpr float corner_radius = 14.f;
  7. constexpr float2 half = float2::one(.5f);
  8. const float tau = 2*std::acos(-1);
  9. struct point
  10. {
  11. enum
  12. {
  13. radius,
  14. angle_start,
  15. angle_end,
  16. center,
  17. count
  18. };
  19. };
  20. std::array<float2, point::count> points;
  21. float2* dragged_point = nullptr;
  22. bool is_near(float2 corner, float2 position);
  23. void start(Program& program)
  24. {
  25. program.key_up = [&program](scancode code, keycode)
  26. {
  27. switch(code)
  28. {
  29. case scancode::leftbracket:
  30. case scancode::c:
  31. if(pressed(scancode::rctrl) || pressed(scancode::lctrl))
  32. case scancode::escape:
  33. program.end();
  34. break;
  35. default: break;
  36. }
  37. };
  38. program.mouse_down = [](float2 position, auto)
  39. {
  40. for(int i = 0; i < point::count; ++i)
  41. if(is_near(points[i], position))
  42. dragged_point = &points[i];
  43. };
  44. program.mouse_up = [](auto, auto)
  45. {
  46. dragged_point = nullptr;
  47. };
  48. program.mouse_move = [](auto, float2 motion)
  49. {
  50. if(dragged_point)
  51. (*dragged_point) += motion;
  52. };
  53. program.draw_loop = [](auto frame, auto)
  54. {
  55. frame.begin_sketch()
  56. .rectangle(rect{ frame.size })
  57. .fill(0xffffff_rgb)
  58. ;
  59. const float2 angle_start = tau * points[point::angle_start]/frame.size;
  60. const float2 angle_end = tau * points[point::angle_end]/frame.size;
  61. frame.begin_sketch()
  62. .arc(points[point::center], rangef{angle_start.x(), angle_end.x()},
  63. points[point::radius].x())
  64. .line_width(2).outline(0x0_rgb)
  65. .fill(0xaaaaaa_rgb)
  66. ;
  67. { auto sketch = frame.begin_sketch();
  68. for(int i = 0; i < point::count; ++i)
  69. sketch.ellipse(rect{float2::one(corner_radius), points[i], half});
  70. sketch.line_width(1).outline(0x555555_rgb);
  71. }
  72. };
  73. }
  74. bool is_near(float2 corner, float2 position)
  75. {
  76. return (corner - position).magnitude() < corner_radius * corner_radius;
  77. }