Skip to content

Commit

Permalink
8130379: Enhance the Bounds class with getCenter method
Browse files Browse the repository at this point in the history
Reviewed-by: kcr
  • Loading branch information
nlisker committed Apr 6, 2018
1 parent 3558f4c commit 671c7cc
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2008, 2017, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2008, 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -34,6 +34,7 @@
* @since JavaFX 2.0
*/
public abstract class Bounds {

/**
* The x coordinate of the upper-left corner of this {@code Bounds}.
*
Expand All @@ -51,6 +52,7 @@ public abstract class Bounds {
*/
public final double getMinY() { return minY; }
private double minY;

/**
* The minimum z coordinate of this {@code Bounds}.
*
Expand All @@ -59,6 +61,7 @@ public abstract class Bounds {
*/
public final double getMinZ() { return minZ; }
private double minZ;

/**
* The width of this {@code Bounds}.
*
Expand All @@ -67,6 +70,7 @@ public abstract class Bounds {
*/
public final double getWidth() { return width; }
private double width;

/**
* The height of this {@code Bounds}.
*
Expand All @@ -75,6 +79,7 @@ public abstract class Bounds {
*/
public final double getHeight() { return height; }
private double height;

/**
* The depth of this {@code Bounds}.
*
Expand All @@ -83,6 +88,7 @@ public abstract class Bounds {
*/
public final double getDepth() { return depth; }
private double depth;

/**
* The x coordinate of the lower-right corner of this {@code Bounds}.
*
Expand All @@ -91,6 +97,7 @@ public abstract class Bounds {
*/
public final double getMaxX() { return maxX; }
private double maxX;

/**
* The y coordinate of the lower-right corner of this {@code Bounds}.
*
Expand All @@ -99,6 +106,7 @@ public abstract class Bounds {
*/
public final double getMaxY() { return maxY; }
private double maxY;

/**
* The maximum z coordinate of this {@code Bounds}.
*
Expand All @@ -108,6 +116,39 @@ public abstract class Bounds {
public final double getMaxZ() { return maxZ; }
private double maxZ;

/**
* The central x coordinate of this {@code Bounds}.
*
* @return the central x coordinate
* @implSpec This call is equivalent to {@code (getMaxX() + getMinX())/2.0}.
* @since 11
*/
public final double getCenterX() {
return (getMaxX() + getMinX()) * 0.5;
}

/**
* The central y coordinate of this {@code Bounds}.
*
* @return the central y coordinate
* @implSpec This call is equivalent to {@code (getMaxY() + getMinY())/2.0}.
* @since 11
*/
public final double getCenterY() {
return (getMaxY() + getMinY()) * 0.5;
}

/**
* The central z coordinate of this {@code Bounds}.
*
* @return the central z coordinate
* @implSpec This call is equivalent to {@code (getMaxZ() + getMinZ())/2.0}.
* @since 11
*/
public final double getCenterZ() {
return (getMaxZ() + getMinZ()) * 0.5;
}

/**
* Indicates whether any of the dimensions(width, height or depth) of this bounds
* is less than zero.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2010, 2015, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2010, 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -28,10 +28,8 @@
import javafx.geometry.BoundingBox;
import javafx.geometry.Bounds;
import javafx.geometry.Point2D;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import static org.junit.Assert.*;
import org.junit.Test;

public class BoundingBoxTest {
Expand Down Expand Up @@ -59,6 +57,14 @@ public void testIsEmpty3D() {
assertTrue(new BoundingBox(0, 0, 0, -1, -1, -1).isEmpty());
}

@Test
public void testCenter() {
BoundingBox bb = new BoundingBox(-2, -1, 0, 2, 2, 1);
assertEquals(bb.getCenterX(), -1, 0);
assertEquals(bb.getCenterY(), 0, 0);
assertEquals(bb.getCenterZ(), 0.5, 0);
}

@Test
public void testContains() {
BoundingBox bb = new BoundingBox(0, 0, 2, 2);
Expand Down

0 comments on commit 671c7cc

Please sign in to comment.