123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200 |
- /* Nothing interesting here, just testing the arc function...
- * There bunch of points in top left stacked on top of each other...
- * You can drag them around and see what they do I guess...
- */
- #include "common/sketchbook.hpp"
- constexpr float corner_radius = 14.f;
- constexpr float2 half = float2::one(.5f);
- const float tau = 2*std::acos(-1);
- struct point
- {
- enum
- {
- radius,
- angle_start,
- angle_end,
- center,
- lower,
- upper,
- count
- };
- };
- std::array<float2, point::count> points{};
- float2* dragged_point = nullptr;
- bool is_near(float2 corner, float2 position);
- void start(Program& program)
- {
- program.draw_once = [](auto frame)
- {
- points[point::center] = frame.size/2;
- // points[point::lower] = trand_float2() * frame.size;
- // points[point::upper] = trand_float2() * frame.size;
- points[point::lower] = frame.size/2 - float2::i(30);
- points[point::upper] = frame.size/2 - float2::i(30);
- };
- program.key_up = [&program](scancode code, keycode)
- {
- switch(code)
- {
- case scancode::leftbracket:
- case scancode::c:
- if(pressed(scancode::rctrl) || pressed(scancode::lctrl))
- case scancode::escape:
- program.end();
- break;
- default: break;
- }
- };
- program.mouse_down = [](float2 position, auto)
- {
- for(int i = 0; i < point::count; ++i)
- if(is_near(points[i], position))
- dragged_point = &points[i];
- };
- program.mouse_up = [](auto, auto)
- {
- dragged_point = nullptr;
- };
- program.mouse_move = [](auto, float2 motion)
- {
- if(dragged_point)
- (*dragged_point) += motion;
- };
- program.draw_loop = [](auto frame, auto)
- {
- frame.begin_sketch()
- .rectangle(rect{ frame.size })
- .fill(0xffffff_rgb)
- ;
- const float2 angle_start = tau * points[point::angle_start]/frame.size;
- const float2 angle_end = tau * points[point::angle_end]/frame.size;
- frame.begin_sketch()
- .arc(points[point::center], rangef{angle_start.x(), angle_end.x()},
- points[point::radius].x())
- .line_width(2).outline(0x0_rgb)
- .fill(0xaaaaaa_rgb)
- ;
- // so much simpler and more natural
- frame.begin_sketch()
- .arc(points[point::center], range2f{points[point::lower], points[point::upper]} - points[point::center], 1)
- .line_width(2).outline(0x0_rgb)
- .fill(0xaa00aa_rgb)
- ;
- frame.begin_sketch()
- .arc(float2::one(70), {float2::i(25), -float2::i(25)}, 1)
- .line_width(2).outline(0x0_rgb)
- .fill(0xaa0000_rgb)
- ;
- frame.begin_sketch()
- .arc(float2::one(70) + float2::i(60), {float2::i(25), float2::i(25)}, 1)
- .line_width(2).outline(0x0_rgb)
- .fill(0xaa0000_rgb)
- ;
- frame.begin_sketch()
- .arc(float2::one(70) + float2::i(120), {-float2::i(25), float2::i(25)}, 1)
- .line_width(2).outline(0x0_rgb)
- .fill(0xaa0000_rgb)
- ;
- frame.begin_sketch()
- .arc(float2::one(70) + float2::i(180), {-float2::i(25), -float2::i(25)}, 1)
- .line_width(2).outline(0x0_rgb)
- .fill(0xaa0000_rgb)
- ;
- ///////////////////////////////////////////////////////////////////////////
- frame.begin_sketch()
- .arc(float2::one(70) + float2::i(0) + float2::j(60), {float2::j(25), float2::j(25)}, 1)
- .line_width(2).outline(0x0_rgb)
- .fill(0xaa0000_rgb)
- ;
- frame.begin_sketch()
- .arc(float2::one(70) + float2::i(60) + float2::j(60), {-float2::j(25), float2::j(25)}, 1)
- .line_width(2).outline(0x0_rgb)
- .fill(0xaa0000_rgb)
- ;
- frame.begin_sketch()
- .arc(float2::one(70) + float2::i(120) + float2::j(60), {-float2::j(25), -float2::j(25)}, 1)
- .line_width(2).outline(0x0_rgb)
- .fill(0xaa0000_rgb)
- ;
- frame.begin_sketch()
- .arc(float2::one(70) + float2::i(180) + float2::j(60), {float2::j(25), -float2::j(25)}, 1)
- .line_width(2).outline(0x0_rgb)
- .fill(0xaa0000_rgb)
- ;
- /////////////////////////////////////////////////////////////////////////////
- frame.begin_sketch()
- .arc(float2::one(70) + float2::i(0) + float2::j(120), {float2::i(25), float2::j(25)}, 1)
- .line_width(2).outline(0x0_rgb)
- .fill(0xaa0000_rgb)
- ;
- frame.begin_sketch()
- .arc(float2::one(70) + float2::i(60) + float2::j(120), {float2::i(25), -float2::j(25)}, 1)
- .line_width(2).outline(0x0_rgb)
- .fill(0xaa0000_rgb)
- ;
- frame.begin_sketch()
- .arc(float2::one(70) + float2::i(120) + float2::j(120), {-float2::j(25), float2::i(25)}, 1)
- .line_width(2).outline(0x0_rgb)
- .fill(0xaa0000_rgb)
- ;
- frame.begin_sketch()
- .arc(float2::one(70) + float2::i(180) + float2::j(120), {float2::j(25), -float2::i(25)}, 1)
- .line_width(2).outline(0x0_rgb)
- .fill(0xaa0000_rgb)
- ;
- // and just reverse the range to go other way around the circle, aint that great
- // frame.begin_sketch()
- // .arc(points[point::center], range2f{points[point::upper], points[point::lower]} - points[point::center], 1)
- // .line_width(2).outline(0x0_rgb)
- // .fill(0x00aa00_rgb)
- // ;
- { auto sketch = frame.begin_sketch();
- for(int i = 0; i < point::count; ++i)
- sketch.ellipse(rect{float2::one(corner_radius), points[i], half});
- sketch.line_width(1).outline(0x555555_rgb);
- }
- };
- }
- bool is_near(float2 corner, float2 position)
- {
- return (corner - position).magnitude() < corner_radius * corner_radius;
- }
|