lets_meet_and_join.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 float2 half = float2::one(.5f);
  7. struct point
  8. {
  9. constexpr static float radius = 14.f;
  10. constexpr static int count = 4;
  11. };
  12. std::array<float2, point::count> points
  13. {
  14. float2{20.f, 20.f},
  15. float2{380.f, 380.f},
  16. float2{380.f, 20.f},
  17. float2{20.f, 380.f}
  18. };
  19. float2* dragged_point = nullptr;
  20. bool is_near(float2 point, float2 position);
  21. void start(Program& program)
  22. {
  23. program.key_up = [&program](scancode code, keycode)
  24. {
  25. switch(code)
  26. {
  27. case scancode::leftbracket:
  28. case scancode::c:
  29. if(pressed(scancode::rctrl) || pressed(scancode::lctrl))
  30. case scancode::escape:
  31. program.end();
  32. break;
  33. default: break;
  34. }
  35. };
  36. program.mouse_down = [](float2 position, auto)
  37. {
  38. for(auto& point : points)
  39. if(is_near(point, position))
  40. dragged_point = &point;
  41. };
  42. program.mouse_up = [](auto, auto)
  43. {
  44. dragged_point = nullptr;
  45. };
  46. program.mouse_move = [](auto, float2 motion)
  47. {
  48. if(dragged_point)
  49. (*dragged_point) += motion;
  50. };
  51. program.draw_loop = [](auto frame, auto)
  52. {
  53. frame.begin_sketch()
  54. .rectangle(rect{ frame.size })
  55. .fill(0xffffff_rgb)
  56. ;
  57. const auto line1 = common::join(points[0], points[1]);
  58. const auto line2 = common::join(points[2], points[3]);
  59. const auto p = common::meet(line1, line2);
  60. geom::loop(frame.size, [&](auto i) {
  61. constexpr auto denominator = geom::vector(1.f);
  62. if(std::abs(line1(i.concat(denominator))) < 200.0f)
  63. frame.begin_sketch().ellipse(rect{float2::one(), i}).fill(0xff0000_rgb);
  64. if(std::abs(line2(i.concat(denominator))) < 200.0f)
  65. frame.begin_sketch().ellipse(rect{float2::one(), i}).fill(0x0000ff_rgb);
  66. });
  67. frame.begin_sketch()
  68. .ellipse(rect{float2::one(point::radius), p.xy()/p.z(), half})
  69. .fill(0xff00ff_rgb);
  70. { auto sketch = frame.begin_sketch();
  71. for(int i = 0; i < point::count; ++i)
  72. sketch.ellipse(rect{float2::one(point::radius), points[i], half});
  73. sketch.line_width(1).outline(0x555555_rgb);
  74. }
  75. };
  76. }
  77. bool is_near(float2 point, float2 position)
  78. {
  79. return (point - position).magnitude() < point::radius * point::radius;
  80. }