Skip to content

Commit

Permalink
Cleanup: Remove unused buggy code from Rectangle
Browse files Browse the repository at this point in the history
The interval intersection test in Rectangle was (probably) buggy. Luckily it was never used.
--> Remove Interval class and related code within Rectangle.
  • Loading branch information
mgmax committed Dec 28, 2023
1 parent 9c3182c commit f8b6202
Showing 1 changed file with 1 addition and 77 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,47 +28,7 @@
*/
public class Rectangle {
private double x1, x2, y1, y2;

/**
* Interval helper class
* represents the closed interval {@code min <= x <= max}
*
* min must be < max
*/
private static class Interval
{

private final double min;
private final double max;

private Interval(double min, double max)
{
this.min = min;
this.max = max;
if (min>max) {
throw new RuntimeException("Interval: min must be < max");
}
}

/**
* @return true if the other interval is equal or is a subset
*/
private boolean isSubsetOf(Interval o)
{
return o.min <= min && o.max >= max;
}

/**
* @return true if the other interval has at least one element in common with this
*/
private boolean intersects(Interval o)
{
return (o.min >= min && o.min <= max) || (o.max >= min && o.max <= max);
}

}

/**
/**
* construct a rectangle with the corners (x1,y1) and (x2,y2)
*/
public Rectangle(double x1, double y1, double x2, double y2)
Expand Down Expand Up @@ -154,20 +114,6 @@ public double getYMax() {
return y2;
}

/**
* X interval from left to right
*/
private Interval getXInterval() {
return new Interval(x1, x2);
}

/**
* Y interval from top to bottom
*/
private Interval getYInterval() {
return new Interval(y1, y2);
}

@Override
public String toString() {
return "Rectangle(x1="+x1+",y1="+y1+",x2="+x2+",y2="+y2+")";
Expand All @@ -179,26 +125,4 @@ public Rectangle clone()
return new Rectangle(x1,y1,x2,y2);
}

public Rectangle scale(double c)
{
return new Rectangle(x1 * c, y1 * c, x2 * c, y2 * c);
}

/**
* check if this is inside of (or equal) another rectangle
* @return true if this rectangle is equal to or inside of the other one
*/
public boolean isInsideOf(Rectangle other) {
return this.getXInterval().isSubsetOf(other.getXInterval())
&& this.getYInterval().isSubsetOf(other.getYInterval());
}

/**
* check if the intersection of this rectangle with another one is not empty
* @return true if rectangles have at least one point in common
*/
public boolean intersects(Rectangle other) {
return this.getXInterval().intersects(other.getXInterval())
&& this.getYInterval().intersects(other.getYInterval());
}
}

0 comments on commit f8b6202

Please sign in to comment.