From cab1a7df173a08e7ec5371e4724b0032cd31588a Mon Sep 17 00:00:00 2001 From: Jiri Vlasak Date: Wed, 14 Jul 2021 23:22:08 +0200 Subject: [PATCH] Move on right side of method to point --- incl/bcar.hh | 20 +++++++++++--------- src/bcar.cc | 32 ++++++++++++++++---------------- ut/bcar.t.cc | 14 +++++++------- 3 files changed, 34 insertions(+), 32 deletions(-) diff --git a/incl/bcar.hh b/incl/bcar.hh index faa8470..0615f53 100644 --- a/incl/bcar.hh +++ b/incl/bcar.hh @@ -11,6 +11,8 @@ template int sgn(T val) { return (T(0) < val) - (val < T(0)); } +class Line; + class Point { private: double x_ = 0.0; @@ -46,6 +48,15 @@ public: * \param poly Polygon to consider. */ bool inside_of(std::vector const& poly) const; + + /*! \brief Return `true` if on the right side of the plane. + * + * The plane is given by the line `li`, where `li->fp()` is the base + * point and the direction is given by `li->lp() - li->fp()`. + * + * \param li The plane to consider is given by `li`. + */ + bool on_right_side_of(Line const& li) const; }; class Line { @@ -92,15 +103,6 @@ public: */ bool intersects_with(Point const& c, double const r); - /*! \brief Return if point `p` is on the right side of the plane. - * - * The plane is given by the line `this`, where `this->fp()` is the base - * point and the direction is given by `this->lp() - this->fp()`. - * - * \param p The point to consider. - */ - bool is_on_right_side(Point const& p) const; - double len() const; }; diff --git a/src/bcar.cc b/src/bcar.cc index 96a2c8c..4922132 100644 --- a/src/bcar.cc +++ b/src/bcar.cc @@ -79,6 +79,22 @@ Point::inside_of(std::vector const& poly) const return c; } +bool +Point::on_right_side_of(Line const& li) const +{ + auto x1 = li.fp().x(); + auto y1 = li.fp().y(); + auto x2 = li.lp().x(); + auto y2 = li.lp().y(); + auto x3 = this->x_; + auto y3 = this->y_; + if (sgn((x3 - x1) * (y2 - y1) - (y3 - y1) * (x2 - x1)) < 0.0) { + return false; + } else { + return true; + } +} + Line::Line(Point const& fp, Point const& lp): first(fp), last(lp), intersection1(Point(0.0, 0.0)), intersection2(Point(0.0, 0.0)) { @@ -175,22 +191,6 @@ Line::intersects_with(Point const& c, double const r) return true; } -bool -Line::is_on_right_side(Point const& p) const -{ - auto x1 = this->fp().x(); - auto y1 = this->fp().y(); - auto x2 = this->lp().x(); - auto y2 = this->lp().y(); - auto x3 = p.x(); - auto y3 = p.y(); - if (sgn((x3 - x1) * (y2 - y1) - (y3 - y1) * (x2 - x1)) < 0.0) { - return false; - } else { - return true; - } -} - double Line::len() const { diff --git a/ut/bcar.t.cc b/ut/bcar.t.cc index 95920e2..bdd4354 100644 --- a/ut/bcar.t.cc +++ b/ut/bcar.t.cc @@ -149,13 +149,13 @@ WVTEST_MAIN("auxiliary angle between three points") auto omo = Point(1.0, -1.0); auto tmo = Point(2.0, -1.0); auto mto = Point(-2.0, 1.0); - WVPASS(Line(momo, oo).is_on_right_side(to)); - WVPASS(!Line(momo, oo).is_on_right_side(ot)); - WVPASS(!Line(moo, omo).is_on_right_side(to)); - WVPASS(!Line(moo, omo).is_on_right_side(ot)); - WVPASS(!Line(moo, omo).is_on_right_side(tmo)); - WVPASS(!Line(moo, omo).is_on_right_side(mot)); - WVPASS(Line(moo, omo).is_on_right_side(mto)); + WVPASS(to.on_right_side_of(Line(momo, oo))); + WVPASS(!ot.on_right_side_of(Line(momo, oo))); + WVPASS(!to.on_right_side_of(Line(moo, omo))); + WVPASS(!ot.on_right_side_of(Line(moo, omo))); + WVPASS(!tmo.on_right_side_of(Line(moo, omo))); + WVPASS(!mot.on_right_side_of(Line(moo, omo))); + WVPASS(mto.on_right_side_of(Line(moo, omo))); } WVTEST_MAIN("drivable") -- 2.39.2