Skip to content
Merged
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
25 changes: 24 additions & 1 deletion src/main/java/net/onelitefeather/coris/shape/CuboidShape.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.onelitefeather.coris.shape;

import net.minestom.server.coordinate.Point;
import net.minestom.server.coordinate.Vec;
import org.jetbrains.annotations.ApiStatus;

Expand All @@ -9,7 +10,7 @@
* @param start the starting point of the cuboid
* @param end the ending point of the cuboid
* @author theEvilReaper
* @version 1.3.0
* @version 1.4.0
* @since 0.1.0
*/
@ApiStatus.Experimental
Expand All @@ -30,6 +31,9 @@ public record CuboidShape(Vec start, Vec end) implements Shape {
}
}

/**
* {@inheritDoc}
*/
@Override
public int compareTo(Shape o) {
if (!(o instanceof CuboidShape(Vec start1, Vec end1))) {
Expand All @@ -40,6 +44,25 @@ public int compareTo(Shape o) {
return compareVec(this.end, end1);
}

/**
* {@inheritDoc}
*/
@Override
public boolean intersect2D(Point position) {
return position.blockX() >= start.blockX() && position.blockX() <= end.blockX() &&
position.blockZ() >= start.blockZ() && position.blockZ() <= end.blockZ();
}

/**
* {@inheritDoc}
*/
@Override
public boolean intersect3D(Point position) {
return position.blockX() >= start.blockX() && position.blockX() <= end.blockX() &&
position.blockY() >= start.blockY() && position.blockY() <= end.blockY() &&
position.blockZ() >= start.blockZ() && position.blockZ() <= end.blockZ();
}

/**
* Compares two vectors lexicographically.
*
Expand Down
22 changes: 21 additions & 1 deletion src/main/java/net/onelitefeather/coris/shape/PointShape.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.onelitefeather.coris.shape;

import net.minestom.server.coordinate.Point;
import net.minestom.server.coordinate.Vec;

/**
Expand All @@ -8,11 +9,14 @@
*
* @param position the position of the point in the 3D space
* @author theEvilReaper
* @version 1.3.0
* @version 1.4.0
* @since 0.1.0
*/
public record PointShape(Vec position) implements Shape {

/**
* {@inheritDoc}
*/
@Override
public int compareTo(Shape o) {
if (!(o instanceof PointShape(Vec position1))) return -1;
Expand All @@ -22,4 +26,20 @@ public int compareTo(Shape o) {
if (cmpY != 0) return cmpY;
return Double.compare(this.position.z(), position1.z());
}

/**
* {@inheritDoc}
*/
@Override
public boolean intersect2D(Point position) {
return this.intersect3D(position);
}

/**
* {@inheritDoc}
*/
@Override
public boolean intersect3D(Point position) {
return this.position.equals(position);
}
Comment on lines +30 to +44
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there some difference?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no real difference for points. Two points intersect if they share the same position, so intersect2D simply delegates to intersect3D to avoid code duplication.

}
6 changes: 4 additions & 2 deletions src/main/java/net/onelitefeather/coris/shape/Shape.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package net.onelitefeather.coris.shape;

import net.minestom.server.coordinate.Point;
import net.onelitefeather.coris.util.Intersect;
import org.jetbrains.annotations.ApiStatus;

/**
Expand All @@ -8,9 +10,9 @@
* The shape is a 2D or 3D representation of the area.
*
* @author theEvilReaper
* @version 1.2.0
* @version 1.3.0
* @since 0.1.0
*/
@ApiStatus.Experimental
public interface Shape extends Comparable<Shape> {
public interface Shape extends Comparable<Shape>, Intersect<Point> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package net.onelitefeather.coris.shape.intersect;

import net.minestom.server.coordinate.Vec;
import net.onelitefeather.coris.shape.CuboidShape;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

import java.util.stream.Stream;

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

class CuboidShapeIntersectTest {

private static final CuboidShape SHAPE = new CuboidShape(new Vec(0, 0, 0), new Vec(5, 5, 5));

static Stream<Arguments> intersect3DProvider() {
return Stream.of(
Arguments.of(new Vec(2, 2, 2), true, "Inside shape"),
Arguments.of(new Vec(0, 0, 0), true, "On start border"),
Arguments.of(new Vec(5, 5, 5), true, "On end border"),
Arguments.of(new Vec(6, 6, 6), false, "Outside shape"),
Arguments.of(new Vec(-1, 2, 2), false, "Negative X outside"),
Arguments.of(new Vec(2, 6, 2), false, "Outside only Y")
);
}

static Stream<Arguments> intersect2DProvider() {
return Stream.of(
Arguments.of(new Vec(2, 99, 2), true, "Inside, Y ignored"),
Arguments.of(new Vec(0, 0, 0), true, "On start border"),
Arguments.of(new Vec(5, 0, 5), true, "On end border XZ"),
Arguments.of(new Vec(6, 2, 2), false, "Outside X"),
Arguments.of(new Vec(2, 2, 6), false, "Outside Z"),
Arguments.of(new Vec(2, 999, 2), true, "Large Y ignored"),
Arguments.of(new Vec(2, -999, 2), true, "Negative Y ignored")
);
}

@ParameterizedTest(name = "{2}")
@MethodSource("intersect3DProvider")
void testIntersect3D(Vec position, boolean expected, String description) {
assertEquals(expected, SHAPE.intersect3D(position));
}

@ParameterizedTest(name = "{2}")
@MethodSource("intersect2DProvider")
void testIntersect2D(Vec position, boolean expected, String description) {
assertEquals(expected, SHAPE.intersect2D(position));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package net.onelitefeather.coris.shape.intersect;

import net.minestom.server.coordinate.Vec;
import net.onelitefeather.coris.shape.PointShape;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

import java.util.stream.Stream;

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

class PointShapeIntersectTest {

private static final PointShape SHAPE = new PointShape(new Vec(2, 2, 2));

static Stream<Arguments> intersectProvider() {
return Stream.of(
Arguments.of(new Vec(2, 2, 2), true, "Exact match"),
Arguments.of(new Vec(3, 2, 2), false, "X mismatch"),
Arguments.of(new Vec(2, 3, 2), false, "Y mismatch"),
Arguments.of(new Vec(2, 2, 3), false, "Z mismatch")
);
}

@ParameterizedTest(name = "{2}")
@MethodSource("intersectProvider")
void testIntersect3D(Vec position, boolean expected, String description) {
assertEquals(expected, SHAPE.intersect3D(position));
}

@ParameterizedTest(name = "{2}")
@MethodSource("intersectProvider")
void testIntersect2D(Vec position, boolean expected, String description) {
assertEquals(expected, SHAPE.intersect2D(position));
}
}
Loading