Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New rectangle methods for retrieving edges #197

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@

import com.google.common.collect.Range;

import java.util.ArrayList;
import java.util.List;

import static java.util.Arrays.asList;

public class DoubleRectangle {
public static DoubleRectangle span(DoubleVector leftTop, DoubleVector rightBottom) {
return new DoubleRectangle(leftTop, rightBottom.subtract(leftTop));
Expand Down Expand Up @@ -136,13 +137,36 @@ public double distance(DoubleVector to) {
return result;
}

public Iterable<DoubleSegment> getParts() {
List<DoubleSegment> result = new ArrayList<>();
result.add(new DoubleSegment(origin, origin.add(new DoubleVector(dimension.x, 0))));
result.add(new DoubleSegment(origin, origin.add(new DoubleVector(0, dimension.y))));
result.add(new DoubleSegment(origin.add(dimension), origin.add(new DoubleVector(dimension.x, 0))));
result.add(new DoubleSegment(origin.add(dimension), origin.add(new DoubleVector(0, dimension.y))));
return result;
public DoubleSegment getLeftEdge() {
return new DoubleSegment(
origin,
new DoubleVector(getLeft(), getBottom())
);
}

public DoubleSegment getTopEdge() {
return new DoubleSegment(
origin,
new DoubleVector(getRight(), getTop())
);
}

public DoubleSegment getRightEdge() {
return new DoubleSegment(
origin.add(dimension),
new DoubleVector(getRight(), getTop())
);
}

public DoubleSegment getBottomEdge() {
return new DoubleSegment(
origin.add(dimension),
new DoubleVector(getLeft(), getBottom())
);
}

public List<DoubleSegment> getParts() {
return asList(getTopEdge(), getLeftEdge(), getRightEdge(), getBottomEdge());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
@RunWith(Suite.class)
@Suite.SuiteClasses({
DistanceTest.class,
DoubleRectangleEdgesTest.class,
DoubleRectangleIntersectionTest.class,
DoubleRectangleTest.class,
DoubleSegmentIntersectionTest.class,
DoubleVectorOperationsTest.class,
RectanglesTest.class,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package jetbrains.jetpad.geometry;

import org.junit.Test;

import static java.util.Arrays.asList;
import static org.junit.Assert.assertEquals;

public class DoubleRectangleEdgesTest {

private static final double LEFT = 5.;
private static final double RIGHT = 15.;

private static final double TOP = 40.;
private static final double BOTTOM = 60.;

private static final DoubleVector LEFT_TOP = new DoubleVector(LEFT, TOP);
private static final DoubleVector LEFT_BOTTOM = new DoubleVector(LEFT, BOTTOM);
private static final DoubleVector RIGHT_TOP = new DoubleVector(RIGHT, TOP);
private static final DoubleVector RIGHT_BOTTOM = new DoubleVector(RIGHT, BOTTOM);

private static final DoubleSegment LEFT_EDGE = edge(LEFT_TOP, LEFT_BOTTOM);
private static final DoubleSegment BOTTOM_EDGE = edge(RIGHT_BOTTOM, LEFT_BOTTOM);
private static final DoubleSegment RIGHT_EDGE = edge(RIGHT_BOTTOM, RIGHT_TOP);
private static final DoubleSegment TOP_EDGE = edge(LEFT_TOP, RIGHT_TOP);

private static final DoubleRectangle RECTANGLE = new DoubleRectangle(LEFT_TOP, RIGHT_BOTTOM.subtract(LEFT_TOP));

@Test
public void getLeftEdge() {
assertEquals(LEFT_EDGE, RECTANGLE.getLeftEdge());
}

@Test
public void getTopEdge() {
assertEquals(TOP_EDGE, RECTANGLE.getTopEdge());
}

@Test
public void getRightEdge() {
assertEquals(RIGHT_EDGE, RECTANGLE.getRightEdge());
}

@Test
public void getBottomEdge() {
assertEquals(BOTTOM_EDGE, RECTANGLE.getBottomEdge());
}

@Test
public void getParts() {
assertEquals(
asList(TOP_EDGE, LEFT_EDGE, RIGHT_EDGE, BOTTOM_EDGE),
RECTANGLE.getParts()
);
}

private static DoubleSegment edge(DoubleVector start, DoubleVector end) {
return new DoubleSegment(start, end);
}

}