diff --git a/c++/vector/.gitignore b/c++/vector/.gitignore index fa79505..c70101c 100644 --- a/c++/vector/.gitignore +++ b/c++/vector/.gitignore @@ -1,2 +1 @@ /vector -/*.d diff --git a/c++/vector/activearea.cpp b/c++/vector/activearea.cpp index 90c186f..dbbbf16 100644 --- a/c++/vector/activearea.cpp +++ b/c++/vector/activearea.cpp @@ -85,7 +85,7 @@ bool ActiveArea::on_motion_notify_event(GdkEventMotion* event) { Vector newposition = position + drag_offset; if (newposition != oldposition) { drag_point->position = newposition; - on_point_move(drag_point, oldposition); + on_point_move(drag_point, oldposition, newposition); queue_draw(); } } else { @@ -118,7 +118,7 @@ void ActiveArea::on_draw(const Context &context) { context.point(**i); } -void ActiveArea::on_point_move(const ActivePoint::Handle &/*point*/, const Vector &/*oldposition*/) +void ActiveArea::on_point_move(const ActivePoint::Handle &/*point*/, const Vector &/*oldposition*/, const Vector &/*newposition*/) { } void ActiveArea::on_draw_content(const Context &/*context*/) diff --git a/c++/vector/activearea.h b/c++/vector/activearea.h index 6839412..9a32217 100644 --- a/c++/vector/activearea.h +++ b/c++/vector/activearea.h @@ -29,7 +29,7 @@ protected: ActivePoint::Handle get_point_at(const Vector &position) const; - virtual void on_point_move(const ActivePoint::Handle &point, const Vector &oldposition); + virtual void on_point_move(const ActivePoint::Handle &point, const Vector &oldposition, const Vector &newposition); virtual void on_draw(const Context &context); virtual void on_draw_content(const Context &context); diff --git a/c++/vector/curvesarea.cpp b/c++/vector/curvesarea.cpp index 9672fda..10d8b56 100644 --- a/c++/vector/curvesarea.cpp +++ b/c++/vector/curvesarea.cpp @@ -39,7 +39,8 @@ namespace { } -CurvesArea::CurvesArea() +CurvesArea::CurvesArea(): + w(new ActivePoint(Vector(), Color::red())) { set_size_request(500, 500); @@ -54,6 +55,31 @@ CurvesArea::CurvesArea() curve.back().add_to(*this); } curve.loop = true; + add_point(w); + set_width(50); +} + +Real CurvesArea::get_width() const + { return fabs(dot(curve.front().get_t().perp().norm(), w->position - curve.front().get_p())); } + +void CurvesArea::set_width(const Real &width) + { w->position = curve.front().get_p() + curve.front().get_t().perp().norm()*width; } + +void CurvesArea::on_point_move(const ActivePoint::Handle &point, const Vector &oldposition, const Vector &newposition) { + Real width = -1; + for(Curve::const_iterator i = curve.begin(); i != curve.end(); ++i) { + if (i == curve.begin() && (i->p == point || i->t == point)) { + point->position = oldposition; + width = get_width(); + point->position = newposition; + } + if (i->p == point) + { i->t->position += i->p->position - oldposition; break; } + } + if (point == w) + width = get_width(); + if (width >= 0) + set_width(width); } void CurvesArea::put_segment(const Context &context, const Segment &segment, const Real width) { @@ -68,10 +94,9 @@ void CurvesArea::put_curve(const Context &context, const Curve &curve, const Rea } } -void CurvesArea::draw_curve(const Context &context, const Curve &curve) { +void CurvesArea::draw_curve(const Context &context, const Curve &curve, const Real width) { context->save(); - Real width = 50; context.set_line_width_px(1); // tangents @@ -99,5 +124,5 @@ void CurvesArea::draw_curve(const Context &context, const Curve &curve) { void CurvesArea::on_draw_content(const Context &context) { - draw_curve(context, curve); + draw_curve(context, curve, get_width()); } diff --git a/c++/vector/curvesarea.h b/c++/vector/curvesarea.h index 3d26d35..c233511 100644 --- a/c++/vector/curvesarea.h +++ b/c++/vector/curvesarea.h @@ -37,14 +37,20 @@ public: protected: Curve curve; + ActivePoint::Handle w; + + Real get_width() const; + void set_width(const Real &width); void put_segment(const Context &context, const Segment &segment, const Real width = 0); void put_curve(const Context &context, const Curve &curve, const Real width = 0); - void draw_curve(const Context &context, const Curve &curve); + void draw_curve(const Context &context, const Curve &curve, const Real width); + + void on_point_move(const ActivePoint::Handle &point, const Vector &oldposition, const Vector &newposition) override; + void on_draw_content(const Context &context) override; public: CurvesArea(); - void on_draw_content(const Context &context) override; };