arc.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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.fullscreen = true;
  28. program.draw_once = [](auto frame)
  29. {
  30. points[point::center] = frame.size/2;
  31. points[point::lower] = trand_float2() * frame.size;
  32. points[point::upper] = trand_float2() * frame.size;
  33. // points[point::lower] = frame.size/2 - float2::i(30);
  34. // points[point::upper] = frame.size/2 - float2::i(30);
  35. };
  36. program.key_up = [&program](scancode code, keycode)
  37. {
  38. switch(code)
  39. {
  40. case scancode::leftbracket:
  41. case scancode::c:
  42. if(pressed(scancode::rctrl) || pressed(scancode::lctrl))
  43. case scancode::escape:
  44. program.end();
  45. break;
  46. default: break;
  47. }
  48. };
  49. program.mouse_down = [](float2 position, auto)
  50. {
  51. for(int i = 0; i < point::count; ++i)
  52. if(is_near(points[i], position))
  53. dragged_point = &points[i];
  54. };
  55. program.mouse_up = [](auto, auto)
  56. {
  57. dragged_point = nullptr;
  58. };
  59. program.mouse_move = [](auto, float2 motion)
  60. {
  61. if(dragged_point)
  62. (*dragged_point) += motion;
  63. };
  64. program.draw_loop = [](auto frame, auto)
  65. {
  66. frame.begin_sketch()
  67. .rectangle(rect{ frame.size })
  68. .fill(0xffffff_rgb)
  69. ;
  70. const float2 angle_start = tau * points[point::angle_start]/frame.size;
  71. const float2 angle_end = tau * points[point::angle_end]/frame.size;
  72. frame.begin_sketch()
  73. .arc(points[point::center], rangef{angle_start.x(), angle_end.x()},
  74. points[point::radius].x())
  75. .line_width(2).outline(0x0_rgb)
  76. .fill(0xaaaaaa_rgb)
  77. ;
  78. // so much simpler and more natural
  79. frame.begin_sketch()
  80. .arc(points[point::center], range2f{points[point::lower], points[point::upper]} - points[point::center], 1)
  81. .line_width(2).outline(0x0_rgb)
  82. .fill(0xaa00aa_rgb)
  83. ;
  84. ///////////////////////////////////////////////////////////////////////////
  85. frame.begin_sketch()
  86. .arc(float2::one(70), {float2::i(25), -float2::i(25)}, 1)
  87. .line_width(2).outline(0x0_rgb)
  88. .fill(0xaa0000_rgb)
  89. ;
  90. frame.begin_sketch()
  91. .arc(float2::one(70) + float2::i(60), {float2::i(25), float2::i(25)}, 1)
  92. .line_width(2).outline(0x0_rgb)
  93. .fill(0xaa0000_rgb)
  94. ;
  95. frame.begin_sketch()
  96. .arc(float2::one(70) + float2::i(120), {-float2::i(25), float2::i(25)}, 1)
  97. .line_width(2).outline(0x0_rgb)
  98. .fill(0xaa0000_rgb)
  99. ;
  100. frame.begin_sketch()
  101. .arc(float2::one(70) + float2::i(180), {-float2::i(25), -float2::i(25)}, 1)
  102. .line_width(2).outline(0x0_rgb)
  103. .fill(0xaa0000_rgb)
  104. ;
  105. ///////////////////////////////////////////////////////////////////////////
  106. frame.begin_sketch()
  107. .arc(float2::one(70) + float2::i(0) + float2::j(60), {float2::j(25), float2::j(25)}, 1)
  108. .line_width(2).outline(0x0_rgb)
  109. .fill(0xaa0000_rgb)
  110. ;
  111. frame.begin_sketch()
  112. .arc(float2::one(70) + float2::i(60) + float2::j(60), {-float2::j(25), float2::j(25)}, 1)
  113. .line_width(2).outline(0x0_rgb)
  114. .fill(0xaa0000_rgb)
  115. ;
  116. frame.begin_sketch()
  117. .arc(float2::one(70) + float2::i(120) + float2::j(60), {-float2::j(25), -float2::j(25)}, 1)
  118. .line_width(2).outline(0x0_rgb)
  119. .fill(0xaa0000_rgb)
  120. ;
  121. frame.begin_sketch()
  122. .arc(float2::one(70) + float2::i(180) + float2::j(60), {float2::j(25), -float2::j(25)}, 1)
  123. .line_width(2).outline(0x0_rgb)
  124. .fill(0xaa0000_rgb)
  125. ;
  126. ///////////////////////////////////////////////////////////////////////////
  127. frame.begin_sketch()
  128. .arc(float2::one(70) + float2::i(0) + float2::j(120), {float2::i(25), float2::j(25)}, 1)
  129. .line_width(2).outline(0x0_rgb)
  130. .fill(0xaa0000_rgb)
  131. ;
  132. frame.begin_sketch()
  133. .arc(float2::one(70) + float2::i(60) + float2::j(120), {float2::i(25), -float2::j(25)}, 1)
  134. .line_width(2).outline(0x0_rgb)
  135. .fill(0xaa0000_rgb)
  136. ;
  137. frame.begin_sketch()
  138. .arc(float2::one(70) + float2::i(120) + float2::j(120), {-float2::j(25), float2::i(25)}, 1)
  139. .line_width(2).outline(0x0_rgb)
  140. .fill(0xaa0000_rgb)
  141. ;
  142. frame.begin_sketch()
  143. .arc(float2::one(70) + float2::i(180) + float2::j(120), {float2::j(25), -float2::i(25)}, 1)
  144. .line_width(2).outline(0x0_rgb)
  145. .fill(0xaa0000_rgb)
  146. ;
  147. { auto sketch = frame.begin_sketch();
  148. for(int i = 0; i < point::count; ++i)
  149. sketch.ellipse(rect{float2::one(corner_radius), points[i], half});
  150. sketch.line_width(1).outline(0x555555_rgb);
  151. }
  152. };
  153. }
  154. bool is_near(float2 corner, float2 position)
  155. {
  156. return (corner - position).magnitude() < corner_radius * corner_radius;
  157. }