1
0
mirror of https://github.com/octoleo/plantuml.git synced 2024-09-22 12:09:03 +00:00
plantuml/src/net/sourceforge/plantuml/klimt/geom/MinMax.java

174 lines
4.5 KiB
Java
Raw Normal View History

2013-12-10 19:36:50 +00:00
/* ========================================================================
* PlantUML : a free UML diagram generator
* ========================================================================
*
2023-02-22 18:43:48 +00:00
* (C) Copyright 2009-2024, Arnaud Roques
2013-12-10 19:36:50 +00:00
*
2023-02-22 18:43:48 +00:00
* Project Info: https://plantuml.com
2013-12-10 19:36:50 +00:00
*
2017-03-15 19:13:31 +00:00
* If you like this project or if you find it useful, you can support us at:
*
2023-02-22 18:43:48 +00:00
* https://plantuml.com/patreon (only 1$ per month!)
* https://plantuml.com/paypal
2017-03-15 19:13:31 +00:00
*
2013-12-10 19:36:50 +00:00
* This file is part of PlantUML.
*
* PlantUML is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* PlantUML distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
* License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
* USA.
*
*
* Original Author: Arnaud Roques
*
*
*/
2023-02-02 17:59:43 +00:00
package net.sourceforge.plantuml.klimt.geom;
2013-12-10 19:36:50 +00:00
2023-02-02 17:59:43 +00:00
import net.sourceforge.plantuml.klimt.UTranslate;
import net.sourceforge.plantuml.klimt.color.HColor;
import net.sourceforge.plantuml.klimt.color.HColors;
2023-02-22 18:43:48 +00:00
import net.sourceforge.plantuml.klimt.drawing.UGraphic;
import net.sourceforge.plantuml.klimt.shape.URectangle;
2013-12-10 19:36:50 +00:00
public class MinMax {
private final double maxX;
private final double maxY;
private final double minX;
private final double minY;
2022-09-12 20:08:34 +00:00
public boolean doesHorizontalCross(XPoint2D pt1, XPoint2D pt2) {
if (pt1.getY() != pt2.getY())
2018-06-12 20:50:45 +00:00
throw new IllegalArgumentException();
2022-09-12 20:08:34 +00:00
if (pt1.getX() == pt2.getX())
2018-06-12 20:50:45 +00:00
throw new IllegalArgumentException();
2022-09-12 20:08:34 +00:00
2018-06-12 20:50:45 +00:00
final double y = pt1.getY();
2022-09-12 20:08:34 +00:00
if (y < minY || y > maxY)
2018-06-12 20:50:45 +00:00
return false;
2022-09-12 20:08:34 +00:00
if (pt1.getX() < minX && pt2.getX() > maxX)
2018-06-12 20:50:45 +00:00
return true;
2022-09-12 20:08:34 +00:00
if (pt2.getX() < minX && pt1.getX() > maxX)
2018-06-12 20:50:45 +00:00
return true;
2022-09-12 20:08:34 +00:00
2018-06-12 20:50:45 +00:00
return false;
}
2013-12-10 19:36:50 +00:00
public static MinMax getEmpty(boolean initToZero) {
2022-09-12 20:08:34 +00:00
if (initToZero)
2013-12-10 19:36:50 +00:00
return new MinMax(0, 0, 0, 0);
2022-09-12 20:08:34 +00:00
2013-12-10 19:36:50 +00:00
return new MinMax(Double.MAX_VALUE, Double.MAX_VALUE, -Double.MAX_VALUE, -Double.MAX_VALUE);
}
@Override
public String toString() {
return "(" + minX + "," + minY + ")->(" + maxX + "," + maxY + ")";
}
public static MinMax fromMutable(MinMaxMutable minmax) {
return new MinMax(minmax.getMinX(), minmax.getMinY(), minmax.getMaxX(), minmax.getMaxY());
}
private MinMax(double minX, double minY, double maxX, double maxY) {
this.minX = minX;
this.minY = minY;
this.maxX = maxX;
this.maxY = maxY;
2022-09-12 20:08:34 +00:00
if (Double.isNaN(minX))
throw new IllegalArgumentException();
2022-09-12 20:08:34 +00:00
if (Double.isNaN(maxX))
throw new IllegalArgumentException();
2022-09-12 20:08:34 +00:00
if (Double.isNaN(minY))
throw new IllegalArgumentException();
2022-09-12 20:08:34 +00:00
if (Double.isNaN(maxY))
throw new IllegalArgumentException();
2022-09-12 20:08:34 +00:00
2013-12-10 19:36:50 +00:00
}
2022-09-12 20:08:34 +00:00
public MinMax addPoint(XPoint2D pt) {
2013-12-10 19:36:50 +00:00
return addPoint(pt.getX(), pt.getY());
}
public MinMax addPoint(double x, double y) {
return new MinMax(Math.min(x, minX), Math.min(y, minY), Math.max(x, maxX), Math.max(y, maxY));
}
2015-04-07 18:18:37 +00:00
public MinMax addMinMax(MinMax other) {
2022-09-12 20:08:34 +00:00
return new MinMax(Math.min(other.minX, minX), Math.min(other.minY, minY), Math.max(other.maxX, maxX),
Math.max(other.maxY, maxY));
2015-04-07 18:18:37 +00:00
}
2013-12-10 19:36:50 +00:00
public static MinMax fromMax(double maxX, double maxY) {
return MinMax.getEmpty(true).addPoint(maxX, maxY);
}
2022-09-12 20:08:34 +00:00
public static MinMax fromDim(XDimension2D dim) {
2013-12-10 19:36:50 +00:00
return fromMax(dim.getWidth(), dim.getHeight());
}
public final double getMaxX() {
return maxX;
}
public final double getMaxY() {
return maxY;
}
public final double getMinX() {
return minX;
}
public final double getMinY() {
return minY;
}
public double getHeight() {
return maxY - minY;
}
public double getWidth() {
return maxX - minX;
}
2022-09-12 20:08:34 +00:00
public XDimension2D getDimension() {
return new XDimension2D(maxX - minX, maxY - minY);
2013-12-10 19:36:50 +00:00
}
2023-03-08 20:49:44 +00:00
// ::comment when __HAXE__
2022-01-07 18:04:36 +00:00
public void drawGray(UGraphic ug) {
2022-08-19 16:34:21 +00:00
draw(ug, HColors.GRAY);
2018-11-26 18:46:22 +00:00
}
2020-03-18 10:50:02 +00:00
public void draw(UGraphic ug, HColor color) {
2020-04-19 16:04:39 +00:00
ug = ug.apply(color).apply(color.bg());
2013-12-10 19:36:50 +00:00
ug = ug.apply(new UTranslate(minX, minY));
ug.draw(URectangle.build(getWidth(), getHeight()));
2013-12-10 19:36:50 +00:00
}
2023-03-08 20:49:44 +00:00
// ::done
2013-12-10 19:36:50 +00:00
public MinMax translate(UTranslate translate) {
final double dx = translate.getDx();
final double dy = translate.getDy();
return new MinMax(minX + dx, minY + dy, maxX + dx, maxY + dy);
}
}