Created
January 20, 2016 13:34
-
-
Save TobleMiner/6effc75acedbc255bfaa to your computer and use it in GitHub Desktop.
Chris RectangleDistanceCalculator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package test.chris; | |
import java.awt.geom.Rectangle2D; | |
public class RectangleDistanceCalculator | |
{ | |
// PRIVATE METHODS | |
// TESTMETHODE | |
public static void main(String[] args) | |
{ | |
RectangleDistanceCalculator instance = new RectangleDistanceCalculator(); | |
Rectangle2D.Double[] rects = new Rectangle2D.Double[2]; | |
rects = generateRectangles(rects); | |
// System.out.println(rects[0].outcode(rects[1].getX(), | |
// rects[1].getY())); | |
double mininmalDistance = instance.minDistance(rects); | |
System.out.println( | |
"Kleinste Entfernung zweiter Rechtecke: " + mininmalDistance + "LE"); | |
} | |
/** | |
* Füllt das Array {@code rects} mit Rectangles | |
* | |
* @param array | |
* Array, welches gefüllt werden soll | |
*/ | |
private static Rectangle2D.Double[] generateRectangles(Rectangle2D.Double[] array) | |
{ | |
array[0] = new Rectangle2D.Double(0, 0, 10, 10); | |
array[1] = new Rectangle2D.Double(13, 14, 20, 20); | |
// array[2] = new Rectangle2D.Double(0, 0, 10, 10); | |
return array; | |
} | |
/** | |
* A method that calculates the Pythagoras theorem..grr.. | |
* | |
* @param distanceOne | |
* The first length of the triangle | |
* @param distanceTwo | |
* The second length of the triangle | |
* | |
* @return returns the length of the hypotenuse. | |
*/ | |
private double hiPythagoras(double distanceOne, double distanceTwo) | |
{ | |
// Wurzel aus der Summe der beiden Quadrate. | |
return Math.sqrt((distanceOne * distanceOne) + (distanceTwo * distanceTwo)); | |
} | |
/** | |
* Calculates the distance of two rectangles, when one rectangle is at the | |
* top left. The rectangle with index y is considered to be the other | |
* rectangle on the left side. | |
* | |
* @param rectangles | |
* the given array | |
* @param x | |
* the index of the first rectangle | |
* @param y | |
* the index of the second rectangle | |
* | |
* @return the length between the first rectangle and the other rectangle. | |
*/ | |
private double distanceLeftUp(Rectangle2D.Double rect1, Rectangle2D.Double rect2) | |
{ | |
// OBEN LINKS | |
// Kathete, Abstand von X Koordinate | |
double distanceX = rect1.getX() - (rect2.getX() + rect2.getWidth()); | |
// andere Kathete, Abstand von Y Koordinate | |
double distanceY = rect1.getY() - (rect2.getY() + rect2.getHeight()); | |
// Berechnet die Hypotenuse für beide Katheten | |
double value = hiPythagoras(distanceX, distanceY); | |
return value; | |
} | |
/** | |
* Calculates the distance of two rectangles, when one rectangle is at the | |
* bottom left. The rectangle with index y is considered to be the other | |
* rectangle on the bottom left side. | |
* | |
* @param rectangles | |
* the given array | |
* @param x | |
* the index of the first rectangle | |
* @param y | |
* the index of the second rectangle | |
* | |
* @return the length between the first rectangle and the other rectangle. | |
*/ | |
private double distanceLeftDown(Rectangle2D.Double rect1, Rectangle2D.Double rect2) | |
{ | |
// UNTEN LINKS | |
// Erste Kathete, X-Koordinaten Abstand | |
double distanceXTwo = rect1.getX() - (rect2.getX() + rect2.getWidth()); | |
// Zweite Kathete, Y-Koordinaten Abstand | |
double distanceYTwo = (rect1.getY() + rect1.getHeight()) - rect2.getY(); | |
// Hypotenuse berechnen | |
double valueTwo = hiPythagoras(distanceXTwo, distanceYTwo); | |
return valueTwo; | |
} | |
/** | |
* Calculates the distance of two rectangles, when one rectangle is at the | |
* bottom right. The rectangle with index y is considered to be the other | |
* rectangle on the bottom right side. | |
* | |
* @param rectangles | |
* the given array | |
* @param x | |
* the index of the first rectangle | |
* @param y | |
* the index of the second rectangle | |
* | |
* @return the length between the first rectangle and the other rectangle. | |
*/ | |
private double distanceRightDown(Rectangle2D.Double rect1, Rectangle2D.Double rect2) | |
{ | |
// UNTEN RECHTS | |
// Erste Kathete | |
double distanceXThree = (rect1.getX() + rect1.getWidth()) - rect2.getX(); | |
// Zweite Kathete... | |
double distanceYThree = (rect1.getY() + rect1.getHeight()) - rect2.getY(); | |
// Hypotenuse | |
double valueThree = hiPythagoras(distanceXThree, distanceYThree); | |
return valueThree; | |
} | |
/** | |
* Calculates the distance of two rectangles, when one rectangle is at the | |
* top right. The rectangle with index y is considered to be the other | |
* rectangle on the top right side. | |
* | |
* @param rectangles | |
* the given array | |
* @param x | |
* the index of the first rectangle | |
* @param y | |
* the index of the second rectangle | |
* | |
* @return the length between the first rectangle and the other rectangle. | |
*/ | |
private double distanceRightUp(Rectangle2D.Double rect1, Rectangle2D.Double rect2) | |
{ | |
// OBEN RECHTS *-* | |
// Erste Kathete | |
double distanceXFour = (rect1.getX() + rect1.getWidth()) - rect2.getX(); | |
// Zweite Kathete | |
double distanceYFour = rect1.getY() - (rect2.getY() + rect2.getHeight()); | |
// Hypotenuse | |
double valueFour = hiPythagoras(distanceXFour, distanceYFour); | |
return valueFour; | |
} | |
/** | |
* Checks if one of the two rectangles is at the top right side. The | |
* parameter y indicates to the other rectangle on the top right side. | |
* | |
* @param rectangles | |
* the given array | |
* @param x | |
* the index of the first rectangle | |
* @param y | |
* the index of the second rectangle. | |
* | |
* @return if the other rectangle-object is at the top right, return @true, | |
* if not return @false. | |
*/ | |
private boolean isRightUp(Rectangle2D.Double rect1, Rectangle2D.Double rect2) | |
{ | |
// Wenn Koordinaten zeigen, dass das andere Rechteck oben rechts liegt | |
if(rect1.getX() + rect1.getWidth() < rect2.getX() && | |
rect1.getY() > rect2.getY() + rect2.getHeight()) | |
{ | |
return true; | |
} | |
return false; | |
} | |
/** | |
* Checks if one of the two rectangles is at the bottom right side. The | |
* parameter y indicates to the other rectangle on the bottom right side. | |
* | |
* @param rectangles | |
* the given array | |
* @param x | |
* the index of the first rectangle | |
* @param y | |
* the index of the second rectangle. | |
* | |
* @return if the other rectangle-object is at the bottom right, | |
* return @true, if not return @false. | |
*/ | |
private boolean isRightDown(Rectangle2D.Double rect1, Rectangle2D.Double rect2) | |
{ | |
// Wenn Koordinaten zeigen, dass das andere Rechteck unten rechts liegt | |
if(rect1.getX() + rect1.getWidth() < rect2.getX() && | |
rect1.getY() + rect1.getHeight() < rect2.getY()) | |
{ | |
return true; | |
} | |
return false; | |
} | |
/** | |
* Checks if one of the two rectangles is at the top left side. The | |
* parameter y indicates to the other rectangle on the top left side. | |
* | |
* @param rectangles | |
* the given array | |
* @param x | |
* the index of the first rectangle | |
* @param y | |
* the index of the second rectangle. | |
* | |
* @return if the other rectangle-object is at the top left, return @true, | |
* if not return @false. | |
*/ | |
private boolean isLeftUp(Rectangle2D.Double rect1, Rectangle2D.Double rect2) | |
{ | |
// Wenn Koordinaten zeigen, dass das andere Rechteck oben links liegt | |
if(rect1.getX() > rect2.getX() + rect2.getWidth() && | |
rect1.getY() > rect2.getY() + rect2.getHeight()) | |
{ | |
return true; | |
} | |
return false; | |
} | |
/** | |
* Checks if one of the two rectangles is at the bottom left side. The | |
* parameter y indicates to the other rectangle on the bottom left side. | |
* | |
* @param rectangles | |
* the given array | |
* @param x | |
* the index of the first rectangle | |
* @param y | |
* the index of the second rectangle. | |
* | |
* @return if the other rectangle-object is at the bottom left, | |
* return @true, if not return @false. | |
*/ | |
private boolean isLeftDown(Rectangle2D.Double rect1, Rectangle2D.Double rect2) | |
{ | |
// Wenn Koordinaten zeigen, dass das andere Rechteck oben links liegt | |
if(rect1.getX() > rect2.getX() + rect2.getWidth() && | |
rect1.getY() + rect1.getHeight() < rect2.getY()) | |
{ | |
return true; | |
} | |
return false; | |
} | |
/** | |
* Checks if the other rectangle is just at the right side. | |
* | |
* @param rectangles | |
* the given array | |
* @param x | |
* the index for the first object | |
* @param y | |
* the index for the other object | |
* | |
* @return if the other rectangle is just at the right side of the first | |
* rectangle, return @true, if not return @false | |
*/ | |
private boolean isRight(Rectangle2D.Double rect1, Rectangle2D.Double rect2) | |
{ | |
// Wenn die Koordinaten zeigen, dass das andere Rechteck nur rechts | |
// liegt. | |
if(rect1.getX() + rect1.getWidth() < rect2.getX() && | |
rect1.getY() < rect2.getY() + rect2.getHeight() && | |
rect1.getY() + rect1.getHeight() > rect2.getY()) | |
{ | |
return true; | |
} | |
return false; | |
} | |
/** | |
* Checks if the other rectangle is just at the left side. | |
* | |
* @param rectangles | |
* the given array | |
* @param x | |
* the index for the first object | |
* @param y | |
* the index for the other object | |
* | |
* @return if the other rectangle is just at the left side of the first | |
* rectangle, return @true, if not return @false | |
*/ | |
private boolean isLeft(Rectangle2D.Double rect1, Rectangle2D.Double rect2) | |
{ | |
// Wenn die Koordinaten zeigen, dass das andere Rechteck nur rechts | |
// liegt. | |
if(rect1.getX() > rect2.getX() + rect2.getWidth() && | |
rect1.getY() < rect2.getY() + rect2.getHeight() && | |
rect1.getY() + rect1.getHeight() > rect2.getY()) | |
{ | |
return true; | |
} | |
return false; | |
} | |
/** | |
* Checks if the other rectangle is just above. | |
* | |
* @param rectangles | |
* the given array | |
* @param x | |
* the index for the first object | |
* @param y | |
* the index for the other object | |
* | |
* @return if the other rectangle is just above of the first rectangle, | |
* return @true, if not return @false | |
*/ | |
private boolean isUp(Rectangle2D.Double rect1, Rectangle2D.Double rect2) | |
{ | |
// Wenn die Koordinaten zeigen, dass das andere Rechteck nur rechts | |
// liegt. | |
if(rect1.getY() > rect2.getY() + rect2.getHeight() && | |
rect1.getX() < rect2.getX() + rect2.getWidth() && | |
rect1.getX() + rect1.getWidth() > rect2.getX()) | |
{ | |
return true; | |
} | |
return false; | |
} | |
/** | |
* Checks if the other rectangle is just at the bottom. | |
* | |
* @param rectangles | |
* the given array | |
* @param x | |
* the index for the first object | |
* @param y | |
* the index for the other object | |
* | |
* @return if the other rectangle is just at the bottom... of the first | |
* rectangle, return @true, if not return @false | |
*/ | |
private boolean isDown(Rectangle2D.Double rect1, Rectangle2D.Double rect2) | |
{ | |
// Wenn die Koordinaten zeigen, dass das andere Rechteck nur rechts | |
// liegt. | |
if(rect1.getY() + rect1.getHeight() < rect2.getY() && | |
rect1.getX() < rect2.getX() + rect2.getWidth() && | |
rect1.getX() + rect1.getWidth() > rect2.getX()) | |
{ | |
return true; | |
} | |
return false; | |
} | |
/** | |
* Calculates the minimum distance between any two rectangles in the given | |
* array. The distance between two rectangles is defined as the length of | |
* the shortest line that can be drawn between them. If two rectangles | |
* overlap, their distance is defined to be 0. A distance can of course | |
* never be negative. | |
* | |
* @param rectangles | |
* array of rectangles. | |
* @return minimum distance between the rectangles, or -1 if the array | |
* contains fewer than 2 rectangles. | |
*/ | |
public double minDistance(Rectangle2D.Double[] rectangles) | |
{ | |
// Wenn das Array weniger als 2 Elemente besitzt. | |
if(rectangles.length < 2) | |
{ | |
return -1.0; | |
} | |
// Hilfsvariable, die die Werte merkt und nach einem kleineren Wert | |
// ändert. | |
double memo = Double.POSITIVE_INFINITY; | |
for(int index = 0; index < rectangles.length - 1; index++) | |
{ | |
for(int compare = index + 1; compare < rectangles.length; compare++) | |
{ | |
// Wenn das andere Rechteck oben liegt. | |
if(isUp(rectangles[index], rectangles[compare])) | |
{ | |
// Wenn der Abstand der berechnet worden ist kleiner als der | |
// jetzige ist. | |
if(memo >= rectangles[index].getY() - (rectangles[compare].getY() + | |
rectangles[compare].getHeight())) | |
{ | |
memo = rectangles[index].getY() - (rectangles[compare].getY() + | |
rectangles[compare].getHeight()); | |
} | |
// Wenn das andere Rechteck unten liegt. | |
} | |
else if(isDown(rectangles[index], rectangles[compare])) | |
{ | |
// Wenn der Abstand der berechnet worden ist kleiner als der | |
// jetzige ist. | |
if(memo >= rectangles[compare].getY() - | |
(rectangles[index].getY() + rectangles[index].getHeight())) | |
{ | |
memo = rectangles[compare].getY() - (rectangles[index].getY() + | |
rectangles[index].getHeight()); | |
} | |
// Rechteck steht links | |
} | |
else if(isLeft(rectangles[index], rectangles[compare])) | |
{ | |
// Wenn der berechnete Wert kleiner ist. | |
if(memo >= rectangles[index].getX() - | |
(rectangles[compare].getX() + rectangles[compare].getWidth())) | |
{ | |
memo = rectangles[index].getX() - (rectangles[compare].getX() + | |
rectangles[compare].getWidth()); | |
} | |
// Rechteck steht rechts | |
} | |
else if(isRight(rectangles[index], rectangles[compare])) | |
{ | |
// Wenn der berechnete Wert kleiner ist. | |
if(memo >= rectangles[compare].getX() - | |
(rectangles[index].getX() + rectangles[index].getWidth())) | |
{ | |
memo = rectangles[compare].getX() - | |
(rectangles[index].getX() + rectangles[index].getWidth()); | |
} | |
// Oben rechts *-*! | |
} | |
else if(isRightUp(rectangles[index], rectangles[compare])) | |
{ | |
// Wenn der Abstand oben rechts kleiner als der jetzige ist. | |
if(distanceRightUp(rectangles[index], rectangles[compare]) <= memo) | |
{ | |
memo = distanceRightUp(rectangles[index], rectangles[compare]); | |
} | |
// unten rechts | |
} | |
else if(isRightDown(rectangles[index], rectangles[compare])) | |
{ | |
// Wenn der Abstand unten rechts kleiner als der jetzige | |
// ist. | |
if(distanceRightDown(rectangles[index], rectangles[compare]) <= memo) | |
{ | |
memo = distanceRightDown(rectangles[index], rectangles[compare]); | |
} | |
// oben links | |
} | |
else if(isLeftUp(rectangles[index], rectangles[compare])) | |
{ | |
// Wenn der Abstand oben links kleiner als der jetzige ist. | |
if(distanceLeftUp(rectangles[index], rectangles[compare]) <= memo) | |
{ | |
memo = distanceLeftUp(rectangles[index], rectangles[compare]); | |
} | |
// unten links | |
} | |
else if(isLeftDown(rectangles[index], rectangles[compare])) | |
{ | |
// Wenn der Absand oben links kleiner als der jetzige ist. | |
if(distanceLeftDown(rectangles[index], rectangles[compare]) <= memo) | |
{ | |
memo = distanceLeftDown(rectangles[index], rectangles[compare]); | |
} | |
// Wenn Rechtecke sich überlappen | |
} | |
else | |
{ | |
return 0.0; | |
} | |
} | |
} | |
// gebe den kleinsten abstand als positives double aus! | |
return Math.abs(memo); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment