Skip to content
Closed
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ solution.get(0).forEach(p -> System.out.println(p.toString()));
* Wrapper objects are used to replicate C# `ref` (pass-by-reference) behaviour. This isn't very Java-esque but avoids an unmanageable refactoring effort.
* Code passes all tests: polygon, line and polytree.
* Uses lower-case (x, y) for point coordinates.
* Private local variables have been renamed to their _camelCase_ variant but public methods (i.e. those of `Clipper.class`) retain their C# _PascalCase_ names (for now...).
* Private local variables have been renamed to their _camelCase_ variant. Public methods on `Clipper.class` retain their C# _PascalCase_ names, while core geometry types expose Java-style `camelCase` aliases for common helpers.
* Benchmarks can be run by including `jmh:benchmark` to the chosen maven goal.
* `scanlineList` from `ClipperBase` uses Java `TreeSet` (variable renamed to `scanlineSet`).

Expand Down
6 changes: 5 additions & 1 deletion src/main/java/clipper2/core/PointD.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ public void Negate() {
y = -y;
}

public void negate() {
Negate();
}

@Override
public final String toString() {
return String.format("(%1$f,%2$f) ", x, y);
Expand Down Expand Up @@ -78,4 +82,4 @@ public final int hashCode() {
public PointD clone() {
return new PointD(x, y);
}
}
}
30 changes: 29 additions & 1 deletion src/main/java/clipper2/core/Rect64.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,30 +67,58 @@ public Path64 AsPath() {
return result;
}

public Path64 asPath() {
return AsPath();
}

public boolean IsEmpty() {
return bottom <= top || right <= left;
}

public boolean isEmpty() {
return IsEmpty();
}

public boolean IsValid() {
return left < Long.MAX_VALUE;
}

public boolean isValid() {
return IsValid();
}

public Point64 MidPoint() {
return new Point64((left + right) / 2, (top + bottom) / 2);
}

public Point64 midPoint() {
return MidPoint();
}

public boolean Contains(Point64 pt) {
return pt.x > left && pt.x < right && pt.y > top && pt.y < bottom;
}

public boolean contains(Point64 pt) {
return Contains(pt);
}

public boolean Intersects(Rect64 rec) {
return (Math.max(left, rec.left) <= Math.min(right, rec.right)) && (Math.max(top, rec.top) <= Math.min(bottom, rec.bottom));
}

public boolean intersects(Rect64 rec) {
return Intersects(rec);
}

public boolean Contains(Rect64 rec) {
return rec.left >= left && rec.right <= right && rec.top >= top && rec.bottom <= bottom;
}

public boolean contains(Rect64 rec) {
return Contains(rec);
}

@Override
public Rect64 clone() {
Rect64 varCopy = new Rect64();
Expand All @@ -102,4 +130,4 @@ public Rect64 clone() {

return varCopy;
}
}
}
26 changes: 25 additions & 1 deletion src/main/java/clipper2/core/RectD.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,28 +64,52 @@ public boolean IsEmpty() {
return bottom <= top || right <= left;
}

public boolean isEmpty() {
return IsEmpty();
}

public PointD MidPoint() {
return new PointD((left + right) / 2, (top + bottom) / 2);
}

public PointD midPoint() {
return MidPoint();
}

public boolean Contains(PointD pt) {
return pt.x > left && pt.x < right && pt.y > top && pt.y < bottom;
}

public boolean contains(PointD pt) {
return Contains(pt);
}

public boolean Contains(RectD rec) {
return rec.left >= left && rec.right <= right && rec.top >= top && rec.bottom <= bottom;
}

public boolean contains(RectD rec) {
return Contains(rec);
}

public boolean Intersects(RectD rec) {
return (Math.max(left, rec.left) < Math.min(right, rec.right)) && (Math.max(top, rec.top) < Math.min(bottom, rec.bottom));
}

public boolean intersects(RectD rec) {
return Intersects(rec);
}

public PathD AsPath() {
PathD result = new PathD(
Arrays.asList(new PointD(left, top), new PointD(right, top), new PointD(right, bottom), new PointD(left, bottom)));
return result;
}

public PathD asPath() {
return AsPath();
}

@Override
public RectD clone() {
RectD varCopy = new RectD();
Expand All @@ -97,4 +121,4 @@ public RectD clone() {

return varCopy;
}
}
}
52 changes: 52 additions & 0 deletions src/test/java/clipper2/TestJavaIdiomaticAliases.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package clipper2;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

import org.junit.jupiter.api.Test;

import clipper2.core.Point64;
import clipper2.core.PointD;
import clipper2.core.Rect64;
import clipper2.core.RectD;

class TestJavaIdiomaticAliases {

@Test
void rect64CamelCaseAliasesMatchPascalCaseMethods() {
Rect64 rect = new Rect64(0, 0, 10, 10);
Point64 point = new Point64(5, 5);
Rect64 inner = new Rect64(1, 1, 9, 9);

assertEquals(rect.AsPath(), rect.asPath());
assertEquals(rect.IsEmpty(), rect.isEmpty());
assertEquals(rect.IsValid(), rect.isValid());
assertEquals(rect.MidPoint(), rect.midPoint());
assertEquals(rect.Contains(point), rect.contains(point));
assertEquals(rect.Contains(inner), rect.contains(inner));
assertEquals(rect.Intersects(inner), rect.intersects(inner));
}

@Test
void rectDCamelCaseAliasesMatchPascalCaseMethods() {
RectD rect = new RectD(0, 0, 10, 10);
PointD point = new PointD(5, 5);
RectD inner = new RectD(1, 1, 9, 9);

assertEquals(rect.AsPath(), rect.asPath());
assertEquals(rect.IsEmpty(), rect.isEmpty());
assertEquals(rect.MidPoint(), rect.midPoint());
assertEquals(rect.Contains(point), rect.contains(point));
assertEquals(rect.Contains(inner), rect.contains(inner));
assertEquals(rect.Intersects(inner), rect.intersects(inner));
}

@Test
void pointDNegateAliasMatchesExistingMethod() {
PointD point = new PointD(3, -4);

point.negate();

assertTrue(point.equals(new PointD(-3, 4)));
}
}