crescent.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. center,
  14. center2,
  15. lower,
  16. upper,
  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.fullscreen = true;
  26. program.draw_once = [](auto frame)
  27. {
  28. points[point::center] = trand_float2() * frame.size;
  29. points[point::center2] = trand_float2() * frame.size;
  30. points[point::lower] = trand_float2() * frame.size;
  31. points[point::upper] = trand_float2() * frame.size;
  32. // points[point::lower] = frame.size/2 - float2::i(30);
  33. // points[point::upper] = frame.size/2 - float2::i(30);
  34. };
  35. program.key_up = [&program](scancode code, keycode)
  36. {
  37. switch(code)
  38. {
  39. case scancode::leftbracket:
  40. case scancode::c:
  41. if(pressed(scancode::rctrl) || pressed(scancode::lctrl))
  42. case scancode::escape:
  43. program.end();
  44. break;
  45. default: break;
  46. }
  47. };
  48. program.mouse_down = [](float2 position, auto)
  49. {
  50. for(int i = 0; i < point::count; ++i)
  51. if(is_near(points[i], position))
  52. dragged_point = &points[i];
  53. };
  54. program.mouse_up = [](auto, auto)
  55. {
  56. dragged_point = nullptr;
  57. };
  58. program.mouse_move = [](auto, float2 motion)
  59. {
  60. if(dragged_point)
  61. (*dragged_point) += motion;
  62. };
  63. program.draw_loop = [](auto frame, auto)
  64. {
  65. frame.begin_sketch()
  66. .rectangle(rect{ frame.size })
  67. .fill(0x0_rgb)
  68. ;
  69. points[point::upper] = points[point::center] + common::normalize(points[point::upper] - points[point::center]) * (points[point::lower] - points[point::center]).length();
  70. points[point::center2] = points[point::center] + common::project(points[point::center2] - points[point::center], support::halfway(points[point::lower], points[point::upper]) - points[point::center]);
  71. frame.begin_sketch()
  72. .arc(points[point::center], range2f{points[point::lower], points[point::upper]} - points[point::center], 1)
  73. .sector(points[point::center2], range2f{points[point::upper], points[point::lower]} - points[point::center2], -1)
  74. .line_width(5).outline(0x7700ff_rgb)
  75. .fill(0xffffff_rgb)
  76. ;
  77. { auto sketch = frame.begin_sketch();
  78. for(int i = 0; i < point::count; ++i)
  79. sketch.ellipse(rect{float2::one(corner_radius), points[i], half});
  80. sketch.line_width(1).outline(0x999999_rgb);
  81. }
  82. };
  83. }
  84. bool is_near(float2 corner, float2 position)
  85. {
  86. return (corner - position).magnitude() < corner_radius * corner_radius;
  87. }