arc.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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.draw_once = [](auto frame)
  28. {
  29. points[point::center] = frame.size/2;
  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(0xffffff_rgb)
  68. ;
  69. const float2 angle_start = tau * points[point::angle_start]/frame.size;
  70. const float2 angle_end = tau * points[point::angle_end]/frame.size;
  71. frame.begin_sketch()
  72. .arc(points[point::center], rangef{angle_start.x(), angle_end.x()},
  73. points[point::radius].x())
  74. .line_width(2).outline(0x0_rgb)
  75. .fill(0xaaaaaa_rgb)
  76. ;
  77. // so much simpler and more natural
  78. frame.begin_sketch()
  79. .arc(points[point::center], range2f{points[point::lower], points[point::upper]} - points[point::center], 1)
  80. .line_width(2).outline(0x0_rgb)
  81. .fill(0xaa00aa_rgb)
  82. ;
  83. frame.begin_sketch()
  84. .arc(float2::one(70), {float2::i(25), -float2::i(25)}, 1)
  85. .line_width(2).outline(0x0_rgb)
  86. .fill(0xaa0000_rgb)
  87. ;
  88. frame.begin_sketch()
  89. .arc(float2::one(70) + float2::i(60), {float2::i(25), float2::i(25)}, 1)
  90. .line_width(2).outline(0x0_rgb)
  91. .fill(0xaa0000_rgb)
  92. ;
  93. frame.begin_sketch()
  94. .arc(float2::one(70) + float2::i(120), {-float2::i(25), float2::i(25)}, 1)
  95. .line_width(2).outline(0x0_rgb)
  96. .fill(0xaa0000_rgb)
  97. ;
  98. frame.begin_sketch()
  99. .arc(float2::one(70) + float2::i(180), {-float2::i(25), -float2::i(25)}, 1)
  100. .line_width(2).outline(0x0_rgb)
  101. .fill(0xaa0000_rgb)
  102. ;
  103. ///////////////////////////////////////////////////////////////////////////
  104. frame.begin_sketch()
  105. .arc(float2::one(70) + float2::i(0) + float2::j(60), {float2::j(25), float2::j(25)}, 1)
  106. .line_width(2).outline(0x0_rgb)
  107. .fill(0xaa0000_rgb)
  108. ;
  109. frame.begin_sketch()
  110. .arc(float2::one(70) + float2::i(60) + float2::j(60), {-float2::j(25), float2::j(25)}, 1)
  111. .line_width(2).outline(0x0_rgb)
  112. .fill(0xaa0000_rgb)
  113. ;
  114. frame.begin_sketch()
  115. .arc(float2::one(70) + float2::i(120) + float2::j(60), {-float2::j(25), -float2::j(25)}, 1)
  116. .line_width(2).outline(0x0_rgb)
  117. .fill(0xaa0000_rgb)
  118. ;
  119. frame.begin_sketch()
  120. .arc(float2::one(70) + float2::i(180) + float2::j(60), {float2::j(25), -float2::j(25)}, 1)
  121. .line_width(2).outline(0x0_rgb)
  122. .fill(0xaa0000_rgb)
  123. ;
  124. /////////////////////////////////////////////////////////////////////////////
  125. frame.begin_sketch()
  126. .arc(float2::one(70) + float2::i(0) + float2::j(120), {float2::i(25), float2::j(25)}, 1)
  127. .line_width(2).outline(0x0_rgb)
  128. .fill(0xaa0000_rgb)
  129. ;
  130. frame.begin_sketch()
  131. .arc(float2::one(70) + float2::i(60) + float2::j(120), {float2::i(25), -float2::j(25)}, 1)
  132. .line_width(2).outline(0x0_rgb)
  133. .fill(0xaa0000_rgb)
  134. ;
  135. frame.begin_sketch()
  136. .arc(float2::one(70) + float2::i(120) + float2::j(120), {-float2::j(25), float2::i(25)}, 1)
  137. .line_width(2).outline(0x0_rgb)
  138. .fill(0xaa0000_rgb)
  139. ;
  140. frame.begin_sketch()
  141. .arc(float2::one(70) + float2::i(180) + float2::j(120), {float2::j(25), -float2::i(25)}, 1)
  142. .line_width(2).outline(0x0_rgb)
  143. .fill(0xaa0000_rgb)
  144. ;
  145. // and just reverse the range to go other way around the circle, aint that great
  146. // frame.begin_sketch()
  147. // .arc(points[point::center], range2f{points[point::upper], points[point::lower]} - points[point::center], 1)
  148. // .line_width(2).outline(0x0_rgb)
  149. // .fill(0x00aa00_rgb)
  150. // ;
  151. { auto sketch = frame.begin_sketch();
  152. for(int i = 0; i < point::count; ++i)
  153. sketch.ellipse(rect{float2::one(corner_radius), points[i], half});
  154. sketch.line_width(1).outline(0x555555_rgb);
  155. }
  156. };
  157. }
  158. bool is_near(float2 corner, float2 position)
  159. {
  160. return (corner - position).magnitude() < corner_radius * corner_radius;
  161. }