Skip to content
Snippets Groups Projects
Rectangle.cpp 2.01 KiB
Newer Older
  • Learn to ignore specific revisions
  • //  ************************************************************************************************
    
    //  BornAgain: simulate and fit reflection and scattering
    
    //! @file      Device/Mask/Rectangle.cpp
    
    //! @brief     Implements class Rectangle.
    //!
    //! @homepage  http://www.bornagainproject.org
    //! @license   GNU General Public License v3 or higher (see COPYING)
    
    //! @copyright Forschungszentrum Jülich GmbH 2018
    //! @authors   Scientific Computing Group at MLZ (see CITATION, AUTHORS)
    
    //  ************************************************************************************************
    
    #include "Device/Mask/Rectangle.h"
    
    #include "Base/Axis/Bin.h"
    
    //! @param xlow x-coordinate of lower left corner
    //! @param ylow y-coordinate of lower left corner
    //! @param xup x-coordinate of upper right corner
    //! @param yup y-coordinate of upper right corner
    
    Rectangle::Rectangle(double xlow, double ylow, double xup, double yup, bool inverted)
        : IShape2D("Rectangle"), m_inverted(inverted)
    
        if (xup <= xlow) {
    
            std::ostringstream message;
            message << "Rectangle(double xlow, double ylow, double xup, double yup) -> Error. ";
            message << " xup <= xlow" << std::endl;
    
            throw std::runtime_error(message.str());
    
        if (yup <= ylow) {
    
            std::ostringstream message;
            message << "Rectangle(double xlow, double ylow, double xup, double yup) -> Error. ";
            message << " yup <= ylow" << std::endl;
    
            throw std::runtime_error(message.str());
    
        }
        m_xlow = xlow;
        m_ylow = ylow;
        m_xup = xup;
        m_yup = yup;
    }
    
    
    void Rectangle::setInverted(bool inverted /*= true*/)
    {
        m_inverted = inverted;
    }
    
    
    bool Rectangle::contains(double x, double y) const
    {
    
        const bool inRect = x <= m_xup && x >= m_xlow && y <= m_yup && y >= m_ylow;
        return m_inverted ? !inRect : inRect;
    
    bool Rectangle::contains(const Bin1D& binx, const Bin1D& biny) const
    {
    
        return contains(binx.center(), biny.center());
    
    double Rectangle::getArea() const
    {
    
        return (m_xup - m_xlow) * (m_yup - m_ylow);