BornAgain  1.19.79
Open-source research software to simulate and fit neutron and x-ray reflectometry and grazing-incidence small-angle scattering
Rectangle.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file Device/Mask/Rectangle.cpp
6 //! @brief Implements class Rectangle.
7 //!
8 //! @homepage http://www.bornagainproject.org
9 //! @license GNU General Public License v3 or higher (see COPYING)
10 //! @copyright Forschungszentrum Jülich GmbH 2018
11 //! @authors Scientific Computing Group at MLZ (see CITATION, AUTHORS)
12 //
13 // ************************************************************************************************
14 
15 #include "Device/Mask/Rectangle.h"
16 #include "Base/Axis/Bin.h"
17 
18 //! @param xlow x-coordinate of lower left corner
19 //! @param ylow y-coordinate of lower left corner
20 //! @param xup x-coordinate of upper right corner
21 //! @param yup y-coordinate of upper right corner
22 //! @param inverted swap inside/outside
23 Rectangle::Rectangle(double xlow, double ylow, double xup, double yup, bool inverted)
24  : IShape2D("Rectangle")
25  , m_inverted(inverted)
26 {
27  if (xup <= xlow) {
28  std::ostringstream message;
29  message << "Rectangle(double xlow, double ylow, double xup, double yup) -> Error. ";
30  message << " xup <= xlow" << std::endl;
31  throw std::runtime_error(message.str());
32  }
33  if (yup <= ylow) {
34  std::ostringstream message;
35  message << "Rectangle(double xlow, double ylow, double xup, double yup) -> Error. ";
36  message << " yup <= ylow" << std::endl;
37  throw std::runtime_error(message.str());
38  }
39  m_xlow = xlow;
40  m_ylow = ylow;
41  m_xup = xup;
42  m_yup = yup;
43 }
44 
45 void Rectangle::setInverted(bool inverted /*= true*/)
46 {
47  m_inverted = inverted;
48 }
49 
50 bool Rectangle::contains(double x, double y) const
51 {
52  const bool inRect = x <= m_xup && x >= m_xlow && y <= m_yup && y >= m_ylow;
53  return m_inverted ? !inRect : inRect;
54 }
55 
56 bool Rectangle::contains(const Bin1D& binx, const Bin1D& biny) const
57 {
58  return contains(binx.center(), biny.center());
59 }
60 
61 double Rectangle::getArea() const
62 {
63  return (m_xup - m_xlow) * (m_yup - m_ylow);
64 }
Defines structs Bin1D, Bin1DCVector.
Defines class Rectangle.
Definition: Bin.h:20
double center() const
Definition: Bin.h:30
Basic class for all shapes in 2D.
Definition: IShape2D.h:26
double m_xup
Definition: Rectangle.h:46
double m_ylow
Definition: Rectangle.h:46
bool m_inverted
Definition: Rectangle.h:47
void setInverted(bool inverted=true)
Definition: Rectangle.cpp:45
Rectangle(double xlow, double ylow, double xup, double yup, bool inverted=false)
Definition: Rectangle.cpp:23
double m_xlow
Definition: Rectangle.h:46
double getArea() const
Definition: Rectangle.cpp:61
bool contains(double x, double y) const override
Returns true if point with given coordinates is inside or on border of the shape.
Definition: Rectangle.cpp:50
double m_yup
Definition: Rectangle.h:46