001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (https://www.alkacon.com)
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * For further information about Alkacon Software, please see the
018 * company website: https://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: https://www.opencms.org
022 *
023 * You should have received a copy of the GNU Lesser General Public
024 * License along with this library; if not, write to the Free Software
025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026 */
027
028package org.opencms.ade.galleries.client.preview.util;
029
030import org.opencms.ade.galleries.shared.CmsPoint;
031
032/**
033 * Axis-aligned rectangle in 2D space.<p>
034 */
035public class CmsRectangle {
036
037    /** x coordinate of left side of the rectangle. */
038    private double m_left;
039
040    /** y coordinate of the top side of the rectangle. */
041    private double m_top;
042
043    /** The width. */
044    private double m_width;
045
046    /** The height. */
047    private double m_height;
048
049    /**
050     * Hidden default constructor.<p>
051     */
052    protected CmsRectangle() {
053
054        // do nothing
055    }
056
057    /**
058     * Creates a new rectangle given its left, top coordinates and its width and height.<p>
059     *
060     * @param left the left side
061     * @param top the top side
062     * @param width the width
063     * @param height the height
064     *
065     * @return the new rectangel
066     */
067    public static CmsRectangle fromLeftTopWidthHeight(double left, double top, double width, double height) {
068
069        CmsRectangle result = new CmsRectangle();
070        result.m_left = left;
071        result.m_top = top;
072        result.m_width = width;
073        result.m_height = height;
074        return result;
075    }
076
077    /**
078     * Creates a new rectangle from its top left and bottom right corner points.<p>
079     *
080     * @param topLeft the top left corner
081     * @param bottomRight the bottom right corner
082     * @return the new rectangle
083     */
084    public static CmsRectangle fromPoints(CmsPoint topLeft, CmsPoint bottomRight) {
085
086        CmsRectangle result = new CmsRectangle();
087        result.m_left = topLeft.getX();
088        result.m_top = topLeft.getY();
089        result.m_width = bottomRight.getX() - topLeft.getX();
090        result.m_height = bottomRight.getY() - topLeft.getY();
091        return result;
092    }
093
094    /**
095     * Helper method to constrain a number inside a given interval.<p>
096     *
097     * If the number is inside the given range, it will be returned unchanged, otherwise either the maximum
098     * or minimum of the range is returned depending on on which side the value lies.
099     *
100     * @param min the minimum value
101     * @param size the difference between maximum and minimum value
102     * @param h the number to constrain
103     *
104     * @return the constrained value
105     */
106    private static double constrainNum(double min, double size, double h) {
107
108        if (h < min) {
109            return min;
110        }
111        if (h >= (min + size)) {
112            return (min + size) - 1;
113        }
114        return h;
115    }
116
117    /**
118     * Constrains a point to this rectangle.<p>
119     *
120     * If any of the coordinates of the point lie in the projection of this rectangle on the corresponding axis, that coordinate
121     * in the result will be unchanged, otherwise it will be either the maximum or the minimum depending on on which side the original
122     * coordinate is located.
123     *
124     * @param point the point to constrain
125     *
126     * @return the constrained point
127     */
128    public CmsPoint constrain(CmsPoint point) {
129
130        return new CmsPoint(constrainNum(m_left, m_width, point.getX()), constrainNum(m_top, m_height, point.getY()));
131    }
132
133    /**
134     * Checks if this rectangle contains a given point.<p>
135     *
136     * @param point the point to check
137     *
138     * @return true if the point is contained in the rectangle
139     */
140    public boolean contains(CmsPoint point) {
141
142        return (point.getX() >= m_left)
143            && (point.getX() < (m_left + m_width))
144            && (point.getY() >= m_top)
145            && (point.getY() < (m_top + m_height));
146    }
147
148    /**
149     * Gets the bottom right corner.<p>
150     *
151     * @return the bottom right corner
152     */
153    public CmsPoint getBottomRight() {
154
155        return new CmsPoint(m_left + m_width, m_top + m_height);
156    }
157
158    /**
159     * Gets the top left corner.<p>
160     *
161     * @return the top left corner
162     */
163    public CmsPoint getTopLeft() {
164
165        return new CmsPoint(m_left, m_top);
166    }
167
168}