/
shvidkuli
/
kkrjava
Обзор
Документация
Войти
/
shvidkuli
/
kkrjava
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
src/main/java/com/montecarlo/geometry/BoundingBox.java
97 строк
3 KB
kroc400
first_commit
30 апр 2026, 22:44
30 апр 2026, 22:44
2347fdb
Код
Авторство
О чём код?
package com.montecarlo.geometry; import java.util.Random; /** * An axis-aligned bounding box (AABB) defined by its minimum and maximum x/y extents. * * <p>Used by Monte Carlo calculations to define the sampling region and by each shape * to expose its spatial extent.</p> */ public class BoundingBox { private final double minX; private final double minY; private final double maxX; private final double maxY; /** * Constructs a BoundingBox with the given corner coordinates. * * @param minX left boundary * @param minY bottom boundary * @param maxX right boundary * @param maxY top boundary * @throws IllegalArgumentException if minX > maxX or minY > maxY */ public BoundingBox(double minX, double minY, double maxX, double maxY) { if (minX > maxX || minY > maxY) { throw new IllegalArgumentException( "Invalid bounding box: min must be <= max. Got [" + minX + "," + maxX + "] x [" + minY + "," + maxY + "]"); } this.minX = minX; this.minY = minY; this.maxX = maxX; this.maxY = maxY; } /** @return left boundary */ public double getMinX() { return minX; } /** @return bottom boundary */ public double getMinY() { return minY; } /** @return right boundary */ public double getMaxX() { return maxX; } /** @return top boundary */ public double getMaxY() { return maxY; } /** @return width of the bounding box */ public double getWidth() { return maxX - minX; } /** @return height of the bounding box */ public double getHeight() { return maxY - minY; } /** * Returns the area of this bounding box. * * @return width * height */ public double area() { return getWidth() * getHeight(); } /** * Generates a uniformly distributed random {@link Point} inside this bounding box. * * @param random the random number generator to use * @return a random point uniformly distributed in [minX, maxX] × [minY, maxY] */ public Point randomPoint(Random random) { double x = minX + random.nextDouble() * getWidth(); double y = minY + random.nextDouble() * getHeight(); return new Point(x, y); } /** * Returns the smallest bounding box that contains both this box and {@code other}. * * @param other another bounding box * @return merged bounding box */ public BoundingBox merge(BoundingBox other) { return new BoundingBox( Math.min(this.minX, other.minX), Math.min(this.minY, other.minY), Math.max(this.maxX, other.maxX), Math.max(this.maxY, other.maxY) ); } @Override public String toString() { return String.format("BoundingBox([%.4f, %.4f] x [%.4f, %.4f])", minX, maxX, minY, maxY); } }