arc.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. lower,
  18. upper,
  19. count
  20. };
  21. };
  22. std::array<float2, point::count> points;
  23. float2* dragged_point = nullptr;
  24. bool is_near(float2 corner, float2 position);
  25. void start(Program& program)
  26. {
  27. program.key_up = [&program](scancode code, keycode)
  28. {
  29. switch(code)
  30. {
  31. case scancode::leftbracket:
  32. case scancode::c:
  33. if(pressed(scancode::rctrl) || pressed(scancode::lctrl))
  34. case scancode::escape:
  35. program.end();
  36. break;
  37. default: break;
  38. }
  39. };
  40. program.mouse_down = [](float2 position, auto)
  41. {
  42. for(int i = 0; i < point::count; ++i)
  43. if(is_near(points[i], position))
  44. dragged_point = &points[i];
  45. };
  46. program.mouse_up = [](auto, auto)
  47. {
  48. dragged_point = nullptr;
  49. };
  50. program.mouse_move = [](auto, float2 motion)
  51. {
  52. if(dragged_point)
  53. (*dragged_point) += motion;
  54. };
  55. program.draw_loop = [](auto frame, auto)
  56. {
  57. frame.begin_sketch()
  58. .rectangle(rect{ frame.size })
  59. .fill(0xffffff_rgb)
  60. ;
  61. const float2 angle_start = tau * points[point::angle_start]/frame.size;
  62. const float2 angle_end = tau * points[point::angle_end]/frame.size;
  63. frame.begin_sketch()
  64. .arc(points[point::center], rangef{angle_start.x(), angle_end.x()},
  65. points[point::radius].x())
  66. .line_width(2).outline(0x0_rgb)
  67. .fill(0xaaaaaa_rgb)
  68. ;
  69. // so much simpler and more natural
  70. frame.begin_sketch()
  71. .arc(points[point::center], range2f{points[point::lower], points[point::upper]} - points[point::center], 1)
  72. .line_width(2).outline(0x0_rgb)
  73. .fill(0xaa00aa_rgb)
  74. ;
  75. // and just reverse the range to go other way around the circle, aint that great
  76. // frame.begin_sketch()
  77. // .arc(points[point::center], range2f{points[point::upper], points[point::lower]} - points[point::center], 1)
  78. // .line_width(2).outline(0x0_rgb)
  79. // .fill(0x00aa00_rgb)
  80. // ;
  81. { auto sketch = frame.begin_sketch();
  82. for(int i = 0; i < point::count; ++i)
  83. sketch.ellipse(rect{float2::one(corner_radius), points[i], half});
  84. sketch.line_width(1).outline(0x555555_rgb);
  85. }
  86. };
  87. }
  88. bool is_near(float2 corner, float2 position)
  89. {
  90. return (corner - position).magnitude() < corner_radius * corner_radius;
  91. }