diff --git a/include/shape/trapezoid.hpp b/include/shape/trapezoid.hpp index 3860d9f..b0ae907 100644 --- a/include/shape/trapezoid.hpp +++ b/include/shape/trapezoid.hpp @@ -70,12 +70,14 @@ class GeneralizedTrapezoid right_side_decreasing_not_vertical_ = (a_right() < -0); } + /** Translate the generalized trapezoid by 'l' along the y-axis. */ void shift_top(LengthDbl l) { yb_ += l; yt_ += l; } + /** Translate the generalized trapezoid by 'l' along the x-axis. */ void shift_right(LengthDbl l) { xbl_ += l; @@ -86,6 +88,10 @@ class GeneralizedTrapezoid x_max_ += l; } + /** + * Replace the left side of the generalized trapezoid by the vertical + * side at x = 'l', which must be on the left of both left corners. + */ void extend_left(LengthDbl l) { xbl_ = l; @@ -96,10 +102,11 @@ class GeneralizedTrapezoid a_left_ = 0; } - void set_top_covered(bool top_covered) { top_covered_ = top_covered; } - - void set_bottom_covered(bool bottom_covered) { bottom_covered_ = bottom_covered; } - + /** + * Return true iff two generalized trapezoids are equal: the + * y-coordinates are compared exactly, and the x-coordinates within + * tolerance. + */ bool operator==(const GeneralizedTrapezoid& trapezoid) const { return (y_bottom() == trapezoid.y_bottom() @@ -150,10 +157,21 @@ class GeneralizedTrapezoid /** Get the area of the generalized trapezoid. */ inline AreaDbl area() const { return area_; } + /** + * Get the slope of the left side, as the variation of x for a unit + * increase of y: 0 if the side is vertical, positive if it goes to the + * right going up. + */ inline double a_left() const { return a_left_; } + /** Get the slope of the right side, in the same way as a_left. */ inline double a_right() const { return a_right_; } + /* + * Directions of the sides going up: x increasing (to the right) or + * decreasing (to the left), the side not being vertical. + */ + inline bool left_side_increasing_not_vertical() const { return left_side_increasing_not_vertical_; } inline bool left_side_decreasing_not_vertical() const { return left_side_decreasing_not_vertical_; } @@ -162,10 +180,6 @@ class GeneralizedTrapezoid inline bool right_side_decreasing_not_vertical() const { return right_side_decreasing_not_vertical_; } - inline bool top_covered() const { return top_covered_; } - - inline bool bottom_covered() const { return bottom_covered_; } - /** Get the x-coordinate of a point on the left side of the trapezoid. */ inline LengthDbl x_left(LengthDbl y) const @@ -191,6 +205,14 @@ class GeneralizedTrapezoid return x_bottom_right() + (y - y_bottom()) * a_right_; } + /** + * Get the area of the part of the generalized trapezoid on the right of + * the vertical line x = 'x_left', which must be on the right of both left + * corners. + * + * Throw std::invalid_argument if 'x_left' is on the right of both right + * corners. + */ AreaDbl area(LengthDbl x_left) const { if (strictly_greater(x_left, x_bottom_right())) { @@ -539,6 +561,7 @@ class GeneralizedTrapezoid return x_shift; } + /** Mirror the generalized trapezoid through the x-axis. */ GeneralizedTrapezoid axial_symmetry_x_axis() const { return GeneralizedTrapezoid( @@ -550,6 +573,7 @@ class GeneralizedTrapezoid x_bottom_right()); } + /** Mirror the generalized trapezoid through the y-axis. */ GeneralizedTrapezoid axial_symmetry_y_axis() const { return GeneralizedTrapezoid( @@ -561,12 +585,26 @@ class GeneralizedTrapezoid -x_top_left()); } + /** + * Return a copy of the generalized trapezoid whose almost vertical sides + * are vertical: each side whose slope (see a_left) is smaller than 1e-2 + * or greater than 1e2 in absolute value is replaced by the vertical side + * through its outermost corner, so that the result contains the + * generalized trapezoid. + * + * The corners of a side are also swapped if its left one is on the right + * of its right one. + */ GeneralizedTrapezoid clean() const; /* * Export */ + /** + * Convert the generalized trapezoid to an anticlockwise polygon of 4 + * line segments; for a triangle, one of them has a null length. + */ Shape to_shape() const { Shape shape; @@ -622,6 +660,11 @@ class GeneralizedTrapezoid + "/>\n"; } + /** + * Read a generalized trapezoid from a JSON object with "y_bottom", + * "y_top", "x_bottom_left", "x_bottom_right", "x_top_left" and + * "x_top_right". + */ template static GeneralizedTrapezoid from_json(basic_json& json_element) { @@ -634,6 +677,7 @@ class GeneralizedTrapezoid json_element["x_top_right"]); } + /** Write the generalized trapezoid to a JSON object read by from_json. */ nlohmann::json to_json() const { nlohmann::json json; @@ -676,7 +720,7 @@ class GeneralizedTrapezoid /** Bottom width of the generalized trapezoid. */ LengthDbl width_bottom_; - /** Bottom height of the generalized trapezoid. */ + /** Top width of the generalized trapezoid. */ LengthDbl width_top_; /** Smallest x of the generalized trapezoid. */ @@ -702,10 +746,6 @@ class GeneralizedTrapezoid bool right_side_decreasing_not_vertical_ = false; - bool bottom_covered_ = false; - - bool top_covered_ = false; - }; inline std::ostream& operator<<(