From 8fdbb287d9f511ad4e74343c544ad8a9e062fd41 Mon Sep 17 00:00:00 2001 From: Boxuan Wang Date: Fri, 24 Sep 2021 22:29:21 +0800 Subject: [PATCH 1/5] concept initialisation 1 interface define --- .idea/.gitignore | 3 + .idea/ComputationalGeometry.iml | 16 ++++ .idea/misc.xml | 13 +++ .idea/modules.xml | 8 ++ .idea/uiDesigner.xml | 124 ++++++++++++++++++++++++++++ .idea/vcs.xml | 6 ++ pom.xml | 30 +++++++ src/java/IDefinition/ILine.java | 8 ++ src/java/IDefinition/IPoint.java | 9 ++ src/java/TwoD/definition/Point.java | 5 ++ 10 files changed, 222 insertions(+) create mode 100644 .idea/.gitignore create mode 100644 .idea/ComputationalGeometry.iml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/uiDesigner.xml create mode 100644 .idea/vcs.xml create mode 100644 pom.xml create mode 100644 src/java/IDefinition/ILine.java create mode 100644 src/java/IDefinition/IPoint.java create mode 100644 src/java/TwoD/definition/Point.java diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/ComputationalGeometry.iml b/.idea/ComputationalGeometry.iml new file mode 100644 index 0000000..28a252d --- /dev/null +++ b/.idea/ComputationalGeometry.iml @@ -0,0 +1,16 @@ + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..88999e9 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,13 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..9f0a731 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/uiDesigner.xml b/.idea/uiDesigner.xml new file mode 100644 index 0000000..e96534f --- /dev/null +++ b/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..bf1980f --- /dev/null +++ b/pom.xml @@ -0,0 +1,30 @@ + + + 4.0.0 + + groupId + ComputationalGeometry + 1.0-SNAPSHOT + + + 16 + 16 + + + + + junit + junit + 4.13 + test + + + + com.google.truth + truth + 1.0.1 + + + \ No newline at end of file diff --git a/src/java/IDefinition/ILine.java b/src/java/IDefinition/ILine.java new file mode 100644 index 0000000..c02aa3a --- /dev/null +++ b/src/java/IDefinition/ILine.java @@ -0,0 +1,8 @@ +package IDefinition; + +public interface ILine { + boolean sameLine(ILine l); + boolean parallel(ILine l); + boolean distance(ILine line); + IPoint intersect(ILine line); +} diff --git a/src/java/IDefinition/IPoint.java b/src/java/IDefinition/IPoint.java new file mode 100644 index 0000000..2330f50 --- /dev/null +++ b/src/java/IDefinition/IPoint.java @@ -0,0 +1,9 @@ +package IDefinition; + +import java.util.List; + +public interface IPoint { + List getValues(); + double distance(IPoint p); + boolean samePoint(IPoint p); +} diff --git a/src/java/TwoD/definition/Point.java b/src/java/TwoD/definition/Point.java new file mode 100644 index 0000000..58c4723 --- /dev/null +++ b/src/java/TwoD/definition/Point.java @@ -0,0 +1,5 @@ +package TwoD.definition; + +public class Point { + +} From 90306f319816ce3ceca1fc86e7cde0240ac54fec Mon Sep 17 00:00:00 2001 From: Boxuan Wang Date: Sat, 25 Sep 2021 08:20:22 +0800 Subject: [PATCH 2/5] concept initialisation 2 interface defined --- .idea/.gitignore | 7 +++++++ .idea/compiler.xml | 16 ++++++++++++++++ src/java/IDefinition/ILine.java | 10 ++++++++-- src/java/IDefinition/IPoint.java | 1 - src/java/IDefinition/IPolygon.java | 13 +++++++++++++ src/java/IDefinition/IRay.java | 16 ++++++++++++++++ src/java/IDefinition/ITriangle.java | 7 +++++++ src/java/IDefinition/IVector.java | 6 ++++++ 8 files changed, 73 insertions(+), 3 deletions(-) create mode 100644 .idea/compiler.xml create mode 100644 src/java/IDefinition/IPolygon.java create mode 100644 src/java/IDefinition/IRay.java create mode 100644 src/java/IDefinition/ITriangle.java create mode 100644 src/java/IDefinition/IVector.java diff --git a/.idea/.gitignore b/.idea/.gitignore index 26d3352..9ca6048 100644 --- a/.idea/.gitignore +++ b/.idea/.gitignore @@ -1,3 +1,10 @@ # Default ignored files /shelf/ /workspace.xml + +/compiler.xml +/modules.xml +/misc.xml +/uiDesigner.xml +/vcs.xml +/ComputationalGeometry.iml diff --git a/.idea/compiler.xml b/.idea/compiler.xml new file mode 100644 index 0000000..2c6be05 --- /dev/null +++ b/.idea/compiler.xml @@ -0,0 +1,16 @@ + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/java/IDefinition/ILine.java b/src/java/IDefinition/ILine.java index c02aa3a..dd7327a 100644 --- a/src/java/IDefinition/ILine.java +++ b/src/java/IDefinition/ILine.java @@ -1,8 +1,14 @@ package IDefinition; public interface ILine { - boolean sameLine(ILine l); boolean parallel(ILine l); - boolean distance(ILine line); + + double distance(ILine line); + double distance(IPoint point); + double distance(IRay ray); + IPoint intersect(ILine line); + IPoint intersect(IRay ray); + + IVector getDirection(); } diff --git a/src/java/IDefinition/IPoint.java b/src/java/IDefinition/IPoint.java index 2330f50..3678901 100644 --- a/src/java/IDefinition/IPoint.java +++ b/src/java/IDefinition/IPoint.java @@ -5,5 +5,4 @@ public interface IPoint { List getValues(); double distance(IPoint p); - boolean samePoint(IPoint p); } diff --git a/src/java/IDefinition/IPolygon.java b/src/java/IDefinition/IPolygon.java new file mode 100644 index 0000000..7ea28e6 --- /dev/null +++ b/src/java/IDefinition/IPolygon.java @@ -0,0 +1,13 @@ +package IDefinition; + +public interface IPolygon { + int getEdges(); + + double getPerimeter(); + double getArea(); + + IPolygon intersect(IPolygon polygon); + + double isSimplePolygon(); + double isConvexPolygon(); +} diff --git a/src/java/IDefinition/IRay.java b/src/java/IDefinition/IRay.java new file mode 100644 index 0000000..f8762de --- /dev/null +++ b/src/java/IDefinition/IRay.java @@ -0,0 +1,16 @@ +package IDefinition; + +public interface IRay { + boolean parallel(IRay ray); + + IPoint intersect(IRay ray); + IPoint intersect(ILine line); + + double distance(IRay ray); + double distance(IPoint point); + double distance(ILine line); + + IVector getDirection(); + + +} diff --git a/src/java/IDefinition/ITriangle.java b/src/java/IDefinition/ITriangle.java new file mode 100644 index 0000000..6cec134 --- /dev/null +++ b/src/java/IDefinition/ITriangle.java @@ -0,0 +1,7 @@ +package IDefinition; + +public interface ITriangle extends IPolygon { + boolean isRHTriangle(); + boolean isAcute(); + boolean isObtuse(); +} diff --git a/src/java/IDefinition/IVector.java b/src/java/IDefinition/IVector.java new file mode 100644 index 0000000..9d8d655 --- /dev/null +++ b/src/java/IDefinition/IVector.java @@ -0,0 +1,6 @@ +package IDefinition; + +public interface IVector { + boolean parallel(IVector vector); + +} From 1fc8beb7f57be3ef7a7459bb150efaf3e43e61bc Mon Sep 17 00:00:00 2001 From: Boxuan Wang Date: Sat, 25 Sep 2021 21:45:31 +0800 Subject: [PATCH 3/5] concept initialisation 3 line, point, ray almost finished --- .idea/ComputationalGeometry.iml | 18 ++- .idea/jarRepositories.xml | 20 ++++ ...uto_value_auto_value_annotations_1_6_3.xml | 13 +++ ..._com_google_code_findbugs_jsr305_3_0_2.xml | 13 +++ ...rorprone_error_prone_annotations_2_3_3.xml | 13 +++ ...__com_google_guava_failureaccess_1_0_1.xml | 13 +++ ...n__com_google_guava_guava_28_1_android.xml | 13 +++ ...9_0_empty_to_avoid_conflict_with_guava.xml | 13 +++ ...m_google_j2objc_j2objc_annotations_1_3.xml | 13 +++ .../Maven__com_google_truth_truth_1_0_1.xml | 13 +++ ...lecode_java_diff_utils_diffutils_1_3_0.xml | 13 +++ .idea/libraries/Maven__junit_junit_4_13.xml | 13 +++ ...kerframework_checker_compat_qual_2_5_5.xml | 13 +++ ...s_mojo_animal_sniffer_annotations_1_18.xml | 13 +++ .../Maven__org_hamcrest_hamcrest_core_1_3.xml | 13 +++ ...aven__org_jetbrains_annotations_22_0_0.xml | 13 +++ pom.xml | 6 + src/java/IDefinition/ILine.java | 5 + src/java/IDefinition/ISegment.java | 16 +++ src/java/IDefinition/IVector.java | 8 ++ src/java/TwoD/definition/Line2D.java | 94 ++++++++++++++++ src/java/TwoD/definition/Point2D.java | 64 +++++++++++ .../definition/{Point.java => Polygon.java} | 3 +- src/java/TwoD/definition/Ray2D.java | 74 +++++++++++++ src/java/TwoD/definition/Segment2D.java | 4 + src/java/TwoD/definition/Triangle.java | 4 + src/java/TwoD/definition/Vector2D.java | 103 ++++++++++++++++++ 27 files changed, 597 insertions(+), 4 deletions(-) create mode 100644 .idea/jarRepositories.xml create mode 100644 .idea/libraries/Maven__com_google_auto_value_auto_value_annotations_1_6_3.xml create mode 100644 .idea/libraries/Maven__com_google_code_findbugs_jsr305_3_0_2.xml create mode 100644 .idea/libraries/Maven__com_google_errorprone_error_prone_annotations_2_3_3.xml create mode 100644 .idea/libraries/Maven__com_google_guava_failureaccess_1_0_1.xml create mode 100644 .idea/libraries/Maven__com_google_guava_guava_28_1_android.xml create mode 100644 .idea/libraries/Maven__com_google_guava_listenablefuture_9999_0_empty_to_avoid_conflict_with_guava.xml create mode 100644 .idea/libraries/Maven__com_google_j2objc_j2objc_annotations_1_3.xml create mode 100644 .idea/libraries/Maven__com_google_truth_truth_1_0_1.xml create mode 100644 .idea/libraries/Maven__com_googlecode_java_diff_utils_diffutils_1_3_0.xml create mode 100644 .idea/libraries/Maven__junit_junit_4_13.xml create mode 100644 .idea/libraries/Maven__org_checkerframework_checker_compat_qual_2_5_5.xml create mode 100644 .idea/libraries/Maven__org_codehaus_mojo_animal_sniffer_annotations_1_18.xml create mode 100644 .idea/libraries/Maven__org_hamcrest_hamcrest_core_1_3.xml create mode 100644 .idea/libraries/Maven__org_jetbrains_annotations_22_0_0.xml create mode 100644 src/java/IDefinition/ISegment.java create mode 100644 src/java/TwoD/definition/Line2D.java create mode 100644 src/java/TwoD/definition/Point2D.java rename src/java/TwoD/definition/{Point.java => Polygon.java} (54%) create mode 100644 src/java/TwoD/definition/Ray2D.java create mode 100644 src/java/TwoD/definition/Segment2D.java create mode 100644 src/java/TwoD/definition/Triangle.java create mode 100644 src/java/TwoD/definition/Vector2D.java diff --git a/.idea/ComputationalGeometry.iml b/.idea/ComputationalGeometry.iml index 28a252d..759a0d1 100644 --- a/.idea/ComputationalGeometry.iml +++ b/.idea/ComputationalGeometry.iml @@ -1,8 +1,8 @@ - - + + @@ -12,5 +12,19 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/jarRepositories.xml b/.idea/jarRepositories.xml new file mode 100644 index 0000000..712ab9d --- /dev/null +++ b/.idea/jarRepositories.xml @@ -0,0 +1,20 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__com_google_auto_value_auto_value_annotations_1_6_3.xml b/.idea/libraries/Maven__com_google_auto_value_auto_value_annotations_1_6_3.xml new file mode 100644 index 0000000..b2aae23 --- /dev/null +++ b/.idea/libraries/Maven__com_google_auto_value_auto_value_annotations_1_6_3.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__com_google_code_findbugs_jsr305_3_0_2.xml b/.idea/libraries/Maven__com_google_code_findbugs_jsr305_3_0_2.xml new file mode 100644 index 0000000..1c380d0 --- /dev/null +++ b/.idea/libraries/Maven__com_google_code_findbugs_jsr305_3_0_2.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__com_google_errorprone_error_prone_annotations_2_3_3.xml b/.idea/libraries/Maven__com_google_errorprone_error_prone_annotations_2_3_3.xml new file mode 100644 index 0000000..1c81eb0 --- /dev/null +++ b/.idea/libraries/Maven__com_google_errorprone_error_prone_annotations_2_3_3.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__com_google_guava_failureaccess_1_0_1.xml b/.idea/libraries/Maven__com_google_guava_failureaccess_1_0_1.xml new file mode 100644 index 0000000..36e948e --- /dev/null +++ b/.idea/libraries/Maven__com_google_guava_failureaccess_1_0_1.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__com_google_guava_guava_28_1_android.xml b/.idea/libraries/Maven__com_google_guava_guava_28_1_android.xml new file mode 100644 index 0000000..22e1968 --- /dev/null +++ b/.idea/libraries/Maven__com_google_guava_guava_28_1_android.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__com_google_guava_listenablefuture_9999_0_empty_to_avoid_conflict_with_guava.xml b/.idea/libraries/Maven__com_google_guava_listenablefuture_9999_0_empty_to_avoid_conflict_with_guava.xml new file mode 100644 index 0000000..4e15702 --- /dev/null +++ b/.idea/libraries/Maven__com_google_guava_listenablefuture_9999_0_empty_to_avoid_conflict_with_guava.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__com_google_j2objc_j2objc_annotations_1_3.xml b/.idea/libraries/Maven__com_google_j2objc_j2objc_annotations_1_3.xml new file mode 100644 index 0000000..bacaa45 --- /dev/null +++ b/.idea/libraries/Maven__com_google_j2objc_j2objc_annotations_1_3.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__com_google_truth_truth_1_0_1.xml b/.idea/libraries/Maven__com_google_truth_truth_1_0_1.xml new file mode 100644 index 0000000..902b2b3 --- /dev/null +++ b/.idea/libraries/Maven__com_google_truth_truth_1_0_1.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__com_googlecode_java_diff_utils_diffutils_1_3_0.xml b/.idea/libraries/Maven__com_googlecode_java_diff_utils_diffutils_1_3_0.xml new file mode 100644 index 0000000..db8944e --- /dev/null +++ b/.idea/libraries/Maven__com_googlecode_java_diff_utils_diffutils_1_3_0.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__junit_junit_4_13.xml b/.idea/libraries/Maven__junit_junit_4_13.xml new file mode 100644 index 0000000..59fc5c4 --- /dev/null +++ b/.idea/libraries/Maven__junit_junit_4_13.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__org_checkerframework_checker_compat_qual_2_5_5.xml b/.idea/libraries/Maven__org_checkerframework_checker_compat_qual_2_5_5.xml new file mode 100644 index 0000000..a4c92ee --- /dev/null +++ b/.idea/libraries/Maven__org_checkerframework_checker_compat_qual_2_5_5.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__org_codehaus_mojo_animal_sniffer_annotations_1_18.xml b/.idea/libraries/Maven__org_codehaus_mojo_animal_sniffer_annotations_1_18.xml new file mode 100644 index 0000000..7663451 --- /dev/null +++ b/.idea/libraries/Maven__org_codehaus_mojo_animal_sniffer_annotations_1_18.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__org_hamcrest_hamcrest_core_1_3.xml b/.idea/libraries/Maven__org_hamcrest_hamcrest_core_1_3.xml new file mode 100644 index 0000000..f58bbc1 --- /dev/null +++ b/.idea/libraries/Maven__org_hamcrest_hamcrest_core_1_3.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__org_jetbrains_annotations_22_0_0.xml b/.idea/libraries/Maven__org_jetbrains_annotations_22_0_0.xml new file mode 100644 index 0000000..f350de5 --- /dev/null +++ b/.idea/libraries/Maven__org_jetbrains_annotations_22_0_0.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/pom.xml b/pom.xml index bf1980f..de6797c 100644 --- a/pom.xml +++ b/pom.xml @@ -26,5 +26,11 @@ truth 1.0.1 + + org.jetbrains + annotations + RELEASE + compile + \ No newline at end of file diff --git a/src/java/IDefinition/ILine.java b/src/java/IDefinition/ILine.java index dd7327a..ca69034 100644 --- a/src/java/IDefinition/ILine.java +++ b/src/java/IDefinition/ILine.java @@ -3,12 +3,17 @@ public interface ILine { boolean parallel(ILine l); + boolean pass(IPoint point); + double distance(ILine line); double distance(IPoint point); double distance(IRay ray); IPoint intersect(ILine line); IPoint intersect(IRay ray); + IPoint intersect(ISegment segment); + + IPoint getOnePassPoint(); IVector getDirection(); } diff --git a/src/java/IDefinition/ISegment.java b/src/java/IDefinition/ISegment.java new file mode 100644 index 0000000..2e90f1a --- /dev/null +++ b/src/java/IDefinition/ISegment.java @@ -0,0 +1,16 @@ +package IDefinition; + +public interface ISegment { + + IPoint intersect(ISegment segment); + IPoint intersect(ILine line); + IPoint intersect(IRay ray); + + double getLength(); + + double distance(ISegment segment); + double distance(ILine line); + double distance(IRay ray); + + IVector getDirection(); +} diff --git a/src/java/IDefinition/IVector.java b/src/java/IDefinition/IVector.java index 9d8d655..8002f97 100644 --- a/src/java/IDefinition/IVector.java +++ b/src/java/IDefinition/IVector.java @@ -1,6 +1,14 @@ package IDefinition; +import javax.naming.OperationNotSupportedException; + public interface IVector { boolean parallel(IVector vector); + double dotProduct(IVector vector); + + IVector uniform() throws OperationNotSupportedException; + double modular(); + IVector add(IVector vector); + IVector multiply(double a); } diff --git a/src/java/TwoD/definition/Line2D.java b/src/java/TwoD/definition/Line2D.java new file mode 100644 index 0000000..c895985 --- /dev/null +++ b/src/java/TwoD/definition/Line2D.java @@ -0,0 +1,94 @@ +package TwoD.definition; + +import IDefinition.*; + +public class Line2D implements ILine { + private Vector2D direction; + private Point2D passPoint; + + @Override + public Point2D getOnePassPoint() { + return passPoint; + } + + @Override + public boolean parallel(ILine l) { + if(l instanceof Line2D){ + return ((Line2D) l).direction.parallel(direction); + } + else throw new IllegalArgumentException("Dimension not match."); + } + + @Override + public boolean pass(IPoint point) { + if(point instanceof Point2D){ + return new Vector2D(passPoint, (Point2D) point).parallel(direction); + } + else throw new IllegalArgumentException("Dimension not match."); + } + + @Override + public double distance(ILine line) { + if(this.intersect(line)!=null) + return 0; + else{ + Vector2D v = new Vector2D(this.passPoint, ((Line2D)line).getOnePassPoint()); + Vector2D p = this.direction.computePerpendicular(); + + return Math.abs(v.dotProduct(p.multiply(1/p.modular()))); + } + } + + @Override + public double distance(IPoint point) { + if(point instanceof Point2D){ + Vector2D v = new Vector2D((Point2D) point,passPoint); + Vector2D p = this.direction.computePerpendicular(); + return Math.abs(v.dotProduct(p.multiply(1/p.modular()))); + } + else throw new IllegalArgumentException("Dimension not match."); + } + + @Override + public double distance(IRay ray) { + //todo: finish + return 0; + } + + @Override + public IPoint intersect(ILine line) { + if(line instanceof Line2D){ + if(((Line2D) line).direction.parallel(direction)){ + return null; + } + else{ + //todo: finish, has intersection + return null; + } + } + else throw new IllegalArgumentException("Dimension not match."); + } + + @Override + public IPoint intersect(IRay ray) { + if(ray instanceof Ray2D){ + if(ray.intersect(new Ray2D(passPoint,direction))!=null || + ray.intersect(new Ray2D(passPoint,(Vector2D) direction.multiply(-1)))!=null){ + //todo: finish, has intersection + return null; + } + else return null; + } + else throw new IllegalArgumentException("Dimension not match."); + } + + @Override + public IPoint intersect(ISegment segment) { + return null; + } + + @Override + public IVector getDirection() { + return direction; + } +} diff --git a/src/java/TwoD/definition/Point2D.java b/src/java/TwoD/definition/Point2D.java new file mode 100644 index 0000000..f8d37ea --- /dev/null +++ b/src/java/TwoD/definition/Point2D.java @@ -0,0 +1,64 @@ +package TwoD.definition; + +import IDefinition.IPoint; + +import java.util.List; + +public class Point2D implements IPoint { + private double x; + private double y; + + public Point2D(double x, double y) { + this.x = x; + this.y = y; + } + + public double getX() { + return x; + } + + public void setX(double x) { + this.x = x; + } + + public double getY() { + return y; + } + + public void setY(double y) { + this.y = y; + } + + @Override + public List getValues() { + return List.of(x,y); + } + + @Override + public double distance(IPoint p) { + if(p instanceof Point2D point) { + return Math.sqrt(Math.pow(point.getX()-x,2)+Math.pow(point.getY()-y,2)); + } + else throw new IllegalArgumentException("Dimension not match."); + } + + @Override + public boolean equals(Object obj) { + if(obj == this) + return true; + if(obj instanceof Point2D){ + return x == ((Point2D) obj).getX() && y == ((Point2D) obj).getY(); + } + else return false; + } + + @Override + public int hashCode() { + return Double.hashCode(x)+Double.hashCode(y); + } + + @Override + public String toString() { + return String.format("Point2D (%f, %f)",x,y); + } +} diff --git a/src/java/TwoD/definition/Point.java b/src/java/TwoD/definition/Polygon.java similarity index 54% rename from src/java/TwoD/definition/Point.java rename to src/java/TwoD/definition/Polygon.java index 58c4723..81cc944 100644 --- a/src/java/TwoD/definition/Point.java +++ b/src/java/TwoD/definition/Polygon.java @@ -1,5 +1,4 @@ package TwoD.definition; -public class Point { - +public class Polygon { } diff --git a/src/java/TwoD/definition/Ray2D.java b/src/java/TwoD/definition/Ray2D.java new file mode 100644 index 0000000..80e9852 --- /dev/null +++ b/src/java/TwoD/definition/Ray2D.java @@ -0,0 +1,74 @@ +package TwoD.definition; + +import IDefinition.ILine; +import IDefinition.IPoint; +import IDefinition.IRay; +import IDefinition.IVector; + +public class Ray2D implements IRay { + + private Point2D startPoint; + private Vector2D direction; + + public Ray2D(Point2D startPoint, Vector2D direction) { + this.startPoint = startPoint; + this.direction = direction; + } + + public Ray2D(Point2D startPoint, Point2D passPoint){ + this.startPoint = startPoint; + this.direction = new Vector2D(startPoint, passPoint); + } + + public Point2D getStartPoint() { + return startPoint; + } + + public void setStartPoint(Point2D startPoint) { + this.startPoint = startPoint; + } + + public void setDirection(Vector2D direction) { + this.direction = direction; + } + + @Override + public boolean parallel(IRay ray) { + return this.direction.parallel(ray.getDirection()); + } + + @Override + public IPoint intersect(IRay ray) { + //todo: finish + return null; + } + + @Override + public IPoint intersect(ILine line) { + //todo: finish + return null; + } + + @Override + public double distance(IRay ray) { + //todo: finish + return 0; + } + + @Override + public double distance(IPoint point) { + //todo: finish + return 0; + } + + @Override + public double distance(ILine line) { + //todo: finish + return 0; + } + + @Override + public IVector getDirection() { + return direction; + } +} diff --git a/src/java/TwoD/definition/Segment2D.java b/src/java/TwoD/definition/Segment2D.java new file mode 100644 index 0000000..d0691e1 --- /dev/null +++ b/src/java/TwoD/definition/Segment2D.java @@ -0,0 +1,4 @@ +package TwoD.definition; + +public class Segment2D { +} diff --git a/src/java/TwoD/definition/Triangle.java b/src/java/TwoD/definition/Triangle.java new file mode 100644 index 0000000..a4b777c --- /dev/null +++ b/src/java/TwoD/definition/Triangle.java @@ -0,0 +1,4 @@ +package TwoD.definition; + +public class Triangle { +} diff --git a/src/java/TwoD/definition/Vector2D.java b/src/java/TwoD/definition/Vector2D.java new file mode 100644 index 0000000..32aff88 --- /dev/null +++ b/src/java/TwoD/definition/Vector2D.java @@ -0,0 +1,103 @@ +package TwoD.definition; + +import IDefinition.IVector; +import javax.naming.OperationNotSupportedException; + +public class Vector2D implements IVector { + + private double x; + private double y; + + public Vector2D(double x, double y) { + this.x = x; + this.y = y; + } + + public Vector2D(@org.jetbrains.annotations.NotNull Point2D startPoint, @org.jetbrains.annotations.NotNull Point2D endPoint){ + this.x = endPoint.getX()- startPoint.getX(); + this.y = endPoint.getY() - startPoint.getY(); + } + + public double getX() { + return x; + } + + public void setX(double x) { + this.x = x; + } + + public double getY() { + return y; + } + + public void setY(double y) { + this.y = y; + } + + @Override + public boolean parallel(IVector vector) { + if(!(vector instanceof Vector2D)) + throw new IllegalArgumentException("Dimension not match"); + else + return x* ((Vector2D) vector).getY() == y* ((Vector2D) vector).getX(); + } + + @Override + public double dotProduct(IVector vector) { + if(!(vector instanceof Vector2D)) + throw new IllegalArgumentException("Dimension not match"); + + else + return x* ((Vector2D) vector).getX()+y* ((Vector2D) vector).getY(); + } + + @Override + public IVector uniform() throws OperationNotSupportedException { + if(modular()==0) + throw new OperationNotSupportedException("Zero vector."); + else{ + return this.multiply(1/modular()); + } + } + + @Override + public double modular() { + return Math.sqrt(x*x+y*y); + } + + @Override + public IVector add(IVector vector) { + if(vector instanceof Vector2D){ + return new Vector2D(x + ((Vector2D) vector).x, y + ((Vector2D) vector).y); + } + else throw new IllegalArgumentException("Dimension not match."); + } + + @Override + public IVector multiply(double a) { + return new Vector2D(x*a, y*a); + } + + @Override + public boolean equals(Object obj) { + if(obj == this) + return true; + if(obj instanceof Vector2D) + return x == ((Vector2D) obj).getX() && y == ((Vector2D) obj).getY(); + else return false; + } + + @Override + public int hashCode() { + return Double.hashCode(x)+Double.hashCode(y); + } + + @Override + public String toString() { + return String.format("Vector2D (%f, %f)",x,y); + } + + public Vector2D computePerpendicular(){ + return new Vector2D(getY(), -1*getX()); + } +} From ff8b2079d3dbf81abf72745b4d5a43c6359215b9 Mon Sep 17 00:00:00 2001 From: Boxuan Wang Date: Sun, 26 Sep 2021 11:12:11 +0800 Subject: [PATCH 4/5] concept initialisation 4 line, ray, segment intersect function finished --- src/java/IDefinition/IRay.java | 3 + src/java/IDefinition/ISegment.java | 5 ++ src/java/TwoD/definition/Line2D.java | 84 ++++++++++++++++--- src/java/TwoD/definition/Point2D.java | 12 +-- src/java/TwoD/definition/Ray2D.java | 73 ++++++++++++----- src/java/TwoD/definition/Segment2D.java | 104 +++++++++++++++++++++++- src/java/TwoD/definition/Vector2D.java | 10 ++- 7 files changed, 245 insertions(+), 46 deletions(-) diff --git a/src/java/IDefinition/IRay.java b/src/java/IDefinition/IRay.java index f8762de..0ac0582 100644 --- a/src/java/IDefinition/IRay.java +++ b/src/java/IDefinition/IRay.java @@ -2,9 +2,12 @@ public interface IRay { boolean parallel(IRay ray); + boolean pass(IPoint point); IPoint intersect(IRay ray); + IPoint intersect(ISegment segment); IPoint intersect(ILine line); + IPoint getStartPoint(); double distance(IRay ray); double distance(IPoint point); diff --git a/src/java/IDefinition/ISegment.java b/src/java/IDefinition/ISegment.java index 2e90f1a..b0a7da8 100644 --- a/src/java/IDefinition/ISegment.java +++ b/src/java/IDefinition/ISegment.java @@ -1,11 +1,14 @@ package IDefinition; +import java.util.List; + public interface ISegment { IPoint intersect(ISegment segment); IPoint intersect(ILine line); IPoint intersect(IRay ray); + IPoint[] getEnds(); double getLength(); double distance(ISegment segment); @@ -13,4 +16,6 @@ public interface ISegment { double distance(IRay ray); IVector getDirection(); + + boolean pass(IPoint point); } diff --git a/src/java/TwoD/definition/Line2D.java b/src/java/TwoD/definition/Line2D.java index c895985..6b986e9 100644 --- a/src/java/TwoD/definition/Line2D.java +++ b/src/java/TwoD/definition/Line2D.java @@ -2,9 +2,23 @@ import IDefinition.*; +import javax.sound.sampled.Line; + public class Line2D implements ILine { - private Vector2D direction; - private Point2D passPoint; + private final Vector2D direction; + private final Point2D passPoint; + + public Line2D(Vector2D direction, Point2D passPoint) { + this.direction = direction; + this.passPoint = passPoint; + } + + public Line2D(Point2D p1, Point2D p2){ + if(p1.equals(p2)) + throw new IllegalArgumentException("Try to initialise a ray from two same points."); + this.direction = new Vector2D(p1,p2); + this.passPoint = p1; + } @Override public Point2D getOnePassPoint() { @@ -12,9 +26,10 @@ public Point2D getOnePassPoint() { } @Override + //when l == this, return false public boolean parallel(ILine l) { if(l instanceof Line2D){ - return ((Line2D) l).direction.parallel(direction); + return (!l.equals(this)) && ((Line2D)l).direction.parallel(direction); } else throw new IllegalArgumentException("Dimension not match."); } @@ -56,39 +71,82 @@ public double distance(IRay ray) { } @Override - public IPoint intersect(ILine line) { + public Point2D intersect(ILine line) { if(line instanceof Line2D){ if(((Line2D) line).direction.parallel(direction)){ return null; } else{ - //todo: finish, has intersection - return null; + double v_x1 = this.direction.getX(); + double v_y1 = this.direction.getY(); + + double v_x2 = ((Line2D) line).direction.getX(); + double v_y2 = ((Line2D) line).direction.getY(); + + double a1 = v_x1*passPoint.getY() - v_y1*passPoint.getX(); + double a2 = v_x2*((Line2D)line).passPoint.getY() - v_y2*((Line2D)line).passPoint.getX(); + + double d0 = v_x1*v_y2 - v_y1*v_x2; + double dx = a2*v_x1 - a1*v_x2; + double dy = v_y1*a2 + v_y2*a1; + + double x = dx/d0; + double y = dy/d0; + + return new Point2D(x,y); } } else throw new IllegalArgumentException("Dimension not match."); } @Override - public IPoint intersect(IRay ray) { + public Point2D intersect(IRay ray) { if(ray instanceof Ray2D){ if(ray.intersect(new Ray2D(passPoint,direction))!=null || - ray.intersect(new Ray2D(passPoint,(Vector2D) direction.multiply(-1)))!=null){ - //todo: finish, has intersection - return null; + ray.intersect(new Ray2D(passPoint, direction.multiply(-1)))!=null){ + return this.intersect( + new Line2D( + ((Ray2D) ray).getDirection(), + ((Ray2D) ray).getStartPoint())); } else return null; } else throw new IllegalArgumentException("Dimension not match."); } + @Override - public IPoint intersect(ISegment segment) { - return null; + public Point2D intersect(ISegment segment) { + if(segment instanceof Segment2D){ + if(segment.intersect(new Ray2D(passPoint,direction))!=null || + segment.intersect(new Ray2D(passPoint, direction.multiply(-1)))!=null){ + return this.intersect( + new Line2D( + (Vector2D) segment.getDirection(), + (Point2D) segment.getEnds()[0])); + } + else return null; + } + else throw new IllegalArgumentException("Dimension not match."); } @Override - public IVector getDirection() { + public Vector2D getDirection() { return direction; } + + @Override + public int hashCode() { + return this.direction.hashCode(); + } + + @Override + public boolean equals(Object obj) { + if(obj == this) + return true; + if(obj instanceof Line2D){ + return ((Line2D) obj).direction.parallel(this.direction) && this.pass(((Line2D) obj).getOnePassPoint()); + } + else return false; + } } diff --git a/src/java/TwoD/definition/Point2D.java b/src/java/TwoD/definition/Point2D.java index f8d37ea..d47b6b0 100644 --- a/src/java/TwoD/definition/Point2D.java +++ b/src/java/TwoD/definition/Point2D.java @@ -5,8 +5,8 @@ import java.util.List; public class Point2D implements IPoint { - private double x; - private double y; + private final double x; + private final double y; public Point2D(double x, double y) { this.x = x; @@ -17,18 +17,10 @@ public double getX() { return x; } - public void setX(double x) { - this.x = x; - } - public double getY() { return y; } - public void setY(double y) { - this.y = y; - } - @Override public List getValues() { return List.of(x,y); diff --git a/src/java/TwoD/definition/Ray2D.java b/src/java/TwoD/definition/Ray2D.java index 80e9852..7e80bee 100644 --- a/src/java/TwoD/definition/Ray2D.java +++ b/src/java/TwoD/definition/Ray2D.java @@ -1,14 +1,11 @@ package TwoD.definition; -import IDefinition.ILine; -import IDefinition.IPoint; -import IDefinition.IRay; -import IDefinition.IVector; +import IDefinition.*; public class Ray2D implements IRay { - private Point2D startPoint; - private Vector2D direction; + private final Point2D startPoint; + private final Vector2D direction; public Ray2D(Point2D startPoint, Vector2D direction) { this.startPoint = startPoint; @@ -16,37 +13,75 @@ public Ray2D(Point2D startPoint, Vector2D direction) { } public Ray2D(Point2D startPoint, Point2D passPoint){ + if(startPoint.equals(passPoint)) + throw new IllegalArgumentException("Try to initialise a ray from two same points."); this.startPoint = startPoint; this.direction = new Vector2D(startPoint, passPoint); } + @Override public Point2D getStartPoint() { return startPoint; } - public void setStartPoint(Point2D startPoint) { - this.startPoint = startPoint; + @Override + public boolean parallel(IRay ray) { + return this.direction.parallel(ray.getDirection()); } - public void setDirection(Vector2D direction) { - this.direction = direction; + @Override + public boolean pass(IPoint point) { + if(point instanceof Point2D){ + return point.equals(startPoint)||this.equals(new Ray2D(startPoint,(Point2D) point)); + } + else throw new IllegalArgumentException("Dimension not match."); } @Override - public boolean parallel(IRay ray) { - return this.direction.parallel(ray.getDirection()); + public Point2D intersect(IRay ray) { + if(ray instanceof Ray2D){ + boolean intersect = + this.pass(((Ray2D) ray).getStartPoint()) + || ray.pass(this.startPoint) + ||( + this.getDirection().crossProductMod(new Vector2D(this.startPoint, ((Ray2D) ray).getStartPoint()))* + this.getDirection().crossProductMod(((Ray2D) ray).getDirection())<0 + && ((Ray2D)ray).getDirection().crossProductMod(new Vector2D(((Ray2D) ray).startPoint, this.startPoint))* + ((Ray2D)ray).getDirection().crossProductMod(this.direction)<0 + ); + if(intersect) + return new Line2D(direction,startPoint).intersect(new Line2D(((Ray2D) ray).direction, ((Ray2D) ray).getStartPoint())); + else return null; + } + else throw new IllegalArgumentException("Dimension not match."); } @Override - public IPoint intersect(IRay ray) { - //todo: finish - return null; + public Point2D intersect(ISegment segment) { + if(segment instanceof Segment2D){ + boolean intersect = + this.pass(((Segment2D)segment).getP1()) + ||this.pass(((Segment2D)segment).getP2()) + || segment.pass(startPoint) + ||( + this.direction.crossProductMod(new Vector2D(startPoint, ((Segment2D) segment).getP1()))* + this.direction.crossProductMod(new Vector2D(startPoint, ((Segment2D) segment).getP2()))<0 + && ((Segment2D)segment).getDirection().crossProductMod(new Vector2D(((Segment2D) segment).getP1(), startPoint))* + ((Segment2D)segment).getDirection().crossProductMod(direction)<0 + ); + if(intersect) + return new Line2D(direction,startPoint).intersect(new Line2D(((Segment2D) segment).getP1(), ((Segment2D) segment).getP2())); + else return null; + } + else throw new IllegalArgumentException("Dimension not match."); } @Override - public IPoint intersect(ILine line) { - //todo: finish - return null; + public Point2D intersect(ILine line) { + if(line instanceof Line2D){ + return ((Line2D) line).intersect(this); + } + else throw new IllegalArgumentException("Dimension not match."); } @Override @@ -68,7 +103,7 @@ public double distance(ILine line) { } @Override - public IVector getDirection() { + public Vector2D getDirection() { return direction; } } diff --git a/src/java/TwoD/definition/Segment2D.java b/src/java/TwoD/definition/Segment2D.java index d0691e1..82f5809 100644 --- a/src/java/TwoD/definition/Segment2D.java +++ b/src/java/TwoD/definition/Segment2D.java @@ -1,4 +1,106 @@ package TwoD.definition; -public class Segment2D { +import IDefinition.*; + +public class Segment2D implements ISegment { + + private final Point2D p1; + private final Point2D p2; + private final Vector2D v; + + public Segment2D(Point2D p1, Point2D p2) { + this.p1 = p1; + this.p2 = p2; + this.v = new Vector2D(p1,p2); + } + + + @Override + public Point2D intersect(ISegment segment) { + if(segment instanceof Segment2D){ + boolean intersect = + segment.pass(p1) + ||segment.pass(p2) + ||this.pass(((Segment2D) segment).getP1()) + ||this.pass(((Segment2D) segment).getP2()) + ||( + v.crossProductMod(new Vector2D(p1, ((Segment2D) segment).getP1()))* + v.crossProductMod(new Vector2D(p1, ((Segment2D) segment).getP2()))<0 + && ((Segment2D)segment).getDirection().crossProductMod(new Vector2D(((Segment2D) segment).getP1(), p1))* + ((Segment2D)segment).getDirection().crossProductMod(new Vector2D(((Segment2D) segment).getP1(), p2))<0 + ); + if (intersect) + return new Line2D(p1,p2).intersect(new Line2D(((Segment2D) segment).getP1(), ((Segment2D) segment).getP2())); + else return null; + } + else throw new IllegalArgumentException("Dimension not match."); + } + + @Override + public Point2D intersect(ILine line) { + if(line instanceof Line2D){ + return ((Line2D) line).intersect(this); + } + else throw new IllegalArgumentException("Dimension not match."); + } + + @Override + public Point2D intersect(IRay ray) { + if (ray instanceof Ray2D){ + return ((Ray2D)ray).intersect(this); + } + else throw new IllegalArgumentException("Dimension not match."); + } + + @Override + public Point2D[] getEnds() { + return new Point2D[]{p1,p2}; + } + + @Override + public double getLength() { + return new Vector2D(p1,p2).modular(); + } + + @Override + public double distance(ISegment segment) { + if(segment instanceof Segment2D){ + //todo: finish + return 0; + } + else throw new IllegalArgumentException("Dimension not match."); + } + + @Override + public double distance(ILine line) { + return 0; + } + + @Override + public double distance(IRay ray) { + return 0; + } + + @Override + public Vector2D getDirection() { + return v; + } + + @Override + public boolean pass(IPoint point) { + if(point instanceof Point2D){ + return ((Point2D) point).getX() <=Math.max(p1.getX(),p2.getX()) + && ((Point2D) point).getX() >= Math.min(p1.getX(),p2.getX()) + && new Vector2D((Point2D) point,p1).parallel(v); + } + else throw new IllegalArgumentException("Dimension not match."); + } + + public Point2D getP1() { + return p1; + } + + public Point2D getP2() { + return p2; + } } diff --git a/src/java/TwoD/definition/Vector2D.java b/src/java/TwoD/definition/Vector2D.java index 32aff88..4173f05 100644 --- a/src/java/TwoD/definition/Vector2D.java +++ b/src/java/TwoD/definition/Vector2D.java @@ -52,7 +52,7 @@ public double dotProduct(IVector vector) { } @Override - public IVector uniform() throws OperationNotSupportedException { + public Vector2D uniform() throws OperationNotSupportedException { if(modular()==0) throw new OperationNotSupportedException("Zero vector."); else{ @@ -66,7 +66,7 @@ public double modular() { } @Override - public IVector add(IVector vector) { + public Vector2D add(IVector vector) { if(vector instanceof Vector2D){ return new Vector2D(x + ((Vector2D) vector).x, y + ((Vector2D) vector).y); } @@ -74,7 +74,7 @@ public IVector add(IVector vector) { } @Override - public IVector multiply(double a) { + public Vector2D multiply(double a) { return new Vector2D(x*a, y*a); } @@ -100,4 +100,8 @@ public String toString() { public Vector2D computePerpendicular(){ return new Vector2D(getY(), -1*getX()); } + + public double crossProductMod(Vector2D v){ + return this.x*v.y - this.y*v.x; + } } From 1ca778909788dca4010b208b6fcc6c8071b0cf9c Mon Sep 17 00:00:00 2001 From: Boxuan Wang Date: Tue, 5 Oct 2021 10:12:54 +0100 Subject: [PATCH 5/5] intersect function finished 4 tests in place --- .idea/misc.xml | 7 ++ .idea/runConfigurations.xml | 10 ++ README.md | 10 +- src/java/IDefinition/ILine.java | 12 +-- src/java/IDefinition/IPoint.java | 5 +- src/java/IDefinition/IPolygon.java | 2 +- src/java/IDefinition/IRay.java | 13 +-- src/java/IDefinition/ISegment.java | 12 +-- src/java/IDefinition/IShape.java | 5 + src/java/TwoD/definition/Line2D.java | 99 ++++++++++------- src/java/TwoD/definition/Point2D.java | 7 +- src/java/TwoD/definition/Ray2D.java | 100 ++++++++++++++---- src/java/TwoD/definition/Segment2D.java | 95 +++++++++++------ src/java/TwoD/definition/Vector2D.java | 4 + .../java/BasicElementInitialisationTest.java | 98 +++++++++++++++++ src/test/java/LineLineIntersectionTest.java | 89 ++++++++++++++++ src/test/java/RaysIntersection.java | 91 ++++++++++++++++ src/test/java/SegmentRayIntersectionTest.java | 92 ++++++++++++++++ src/test/java/SegmentsIntersectTest.java | 90 ++++++++++++++++ 19 files changed, 719 insertions(+), 122 deletions(-) create mode 100644 .idea/runConfigurations.xml create mode 100644 src/java/IDefinition/IShape.java create mode 100644 src/test/java/BasicElementInitialisationTest.java create mode 100644 src/test/java/LineLineIntersectionTest.java create mode 100644 src/test/java/RaysIntersection.java create mode 100644 src/test/java/SegmentRayIntersectionTest.java create mode 100644 src/test/java/SegmentsIntersectTest.java diff --git a/.idea/misc.xml b/.idea/misc.xml index 88999e9..fecf653 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -10,4 +10,11 @@ + + + \ No newline at end of file diff --git a/.idea/runConfigurations.xml b/.idea/runConfigurations.xml new file mode 100644 index 0000000..797acea --- /dev/null +++ b/.idea/runConfigurations.xml @@ -0,0 +1,10 @@ + + + + + + \ No newline at end of file diff --git a/README.md b/README.md index d48e770..a47b449 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,10 @@ computational geometry on 2D ## Problems solved in this part -1. Define point segment, ray and line. -2. Tell whether two segments intersect -3. Tell whether three points co-line +####1. Definitions +2D: point, line, ray, segment, vector +####2. Intersection +between line, ray and segment +####3. Distance + + diff --git a/src/java/IDefinition/ILine.java b/src/java/IDefinition/ILine.java index ca69034..c07f740 100644 --- a/src/java/IDefinition/ILine.java +++ b/src/java/IDefinition/ILine.java @@ -1,17 +1,13 @@ package IDefinition; -public interface ILine { +public interface ILine extends IShape{ boolean parallel(ILine l); boolean pass(IPoint point); - double distance(ILine line); - double distance(IPoint point); - double distance(IRay ray); - - IPoint intersect(ILine line); - IPoint intersect(IRay ray); - IPoint intersect(ISegment segment); + IShape intersect(ILine line) throws Exception; + IShape intersect(IRay ray); + IShape intersect(ISegment segment); IPoint getOnePassPoint(); diff --git a/src/java/IDefinition/IPoint.java b/src/java/IDefinition/IPoint.java index 3678901..6f4accf 100644 --- a/src/java/IDefinition/IPoint.java +++ b/src/java/IDefinition/IPoint.java @@ -2,7 +2,6 @@ import java.util.List; -public interface IPoint { - List getValues(); - double distance(IPoint p); +public interface IPoint extends IShape { + List toList(); } diff --git a/src/java/IDefinition/IPolygon.java b/src/java/IDefinition/IPolygon.java index 7ea28e6..9070cf1 100644 --- a/src/java/IDefinition/IPolygon.java +++ b/src/java/IDefinition/IPolygon.java @@ -6,7 +6,7 @@ public interface IPolygon { double getPerimeter(); double getArea(); - IPolygon intersect(IPolygon polygon); + IShape intersect(IPolygon polygon); double isSimplePolygon(); double isConvexPolygon(); diff --git a/src/java/IDefinition/IRay.java b/src/java/IDefinition/IRay.java index 0ac0582..b0e7d0c 100644 --- a/src/java/IDefinition/IRay.java +++ b/src/java/IDefinition/IRay.java @@ -1,17 +1,14 @@ package IDefinition; -public interface IRay { +public interface IRay extends IShape{ boolean parallel(IRay ray); boolean pass(IPoint point); - IPoint intersect(IRay ray); - IPoint intersect(ISegment segment); - IPoint intersect(ILine line); - IPoint getStartPoint(); + IShape intersect(IRay ray); + IShape intersect(ISegment segment); + IShape intersect(ILine line); - double distance(IRay ray); - double distance(IPoint point); - double distance(ILine line); + IPoint getStartPoint(); IVector getDirection(); diff --git a/src/java/IDefinition/ISegment.java b/src/java/IDefinition/ISegment.java index b0a7da8..93bda08 100644 --- a/src/java/IDefinition/ISegment.java +++ b/src/java/IDefinition/ISegment.java @@ -2,19 +2,15 @@ import java.util.List; -public interface ISegment { +public interface ISegment extends IShape{ - IPoint intersect(ISegment segment); - IPoint intersect(ILine line); - IPoint intersect(IRay ray); + IShape intersect(ISegment segment); + IShape intersect(ILine line); + IShape intersect(IRay ray); IPoint[] getEnds(); double getLength(); - double distance(ISegment segment); - double distance(ILine line); - double distance(IRay ray); - IVector getDirection(); boolean pass(IPoint point); diff --git a/src/java/IDefinition/IShape.java b/src/java/IDefinition/IShape.java new file mode 100644 index 0000000..220eb7c --- /dev/null +++ b/src/java/IDefinition/IShape.java @@ -0,0 +1,5 @@ +package IDefinition; + +public interface IShape { + double distance (IShape shape); +} diff --git a/src/java/TwoD/definition/Line2D.java b/src/java/TwoD/definition/Line2D.java index 6b986e9..6f7835f 100644 --- a/src/java/TwoD/definition/Line2D.java +++ b/src/java/TwoD/definition/Line2D.java @@ -1,6 +1,7 @@ package TwoD.definition; import IDefinition.*; +import org.jetbrains.annotations.NotNull; import javax.sound.sampled.Line; @@ -8,12 +9,14 @@ public class Line2D implements ILine { private final Vector2D direction; private final Point2D passPoint; - public Line2D(Vector2D direction, Point2D passPoint) { + public Line2D(@NotNull Vector2D direction, @NotNull Point2D passPoint) { + if(direction.isZeroVector()) + throw new IllegalArgumentException("Try to initialise from a zero vector."); this.direction = direction; this.passPoint = passPoint; } - public Line2D(Point2D p1, Point2D p2){ + public Line2D(@NotNull Point2D p1, @NotNull Point2D p2){ if(p1.equals(p2)) throw new IllegalArgumentException("Try to initialise a ray from two same points."); this.direction = new Vector2D(p1,p2); @@ -43,38 +46,13 @@ public boolean pass(IPoint point) { } @Override - public double distance(ILine line) { - if(this.intersect(line)!=null) - return 0; - else{ - Vector2D v = new Vector2D(this.passPoint, ((Line2D)line).getOnePassPoint()); - Vector2D p = this.direction.computePerpendicular(); - - return Math.abs(v.dotProduct(p.multiply(1/p.modular()))); - } - } - - @Override - public double distance(IPoint point) { - if(point instanceof Point2D){ - Vector2D v = new Vector2D((Point2D) point,passPoint); - Vector2D p = this.direction.computePerpendicular(); - return Math.abs(v.dotProduct(p.multiply(1/p.modular()))); - } - else throw new IllegalArgumentException("Dimension not match."); - } - - @Override - public double distance(IRay ray) { - //todo: finish - return 0; - } - - @Override - public Point2D intersect(ILine line) { + public IShape intersect(ILine line){ if(line instanceof Line2D){ if(((Line2D) line).direction.parallel(direction)){ - return null; + if(!this.equals(line)) + return null; + else + return this; } else{ double v_x1 = this.direction.getX(); @@ -83,12 +61,12 @@ public Point2D intersect(ILine line) { double v_x2 = ((Line2D) line).direction.getX(); double v_y2 = ((Line2D) line).direction.getY(); - double a1 = v_x1*passPoint.getY() - v_y1*passPoint.getX(); - double a2 = v_x2*((Line2D)line).passPoint.getY() - v_y2*((Line2D)line).passPoint.getX(); + double a1 = v_y1*passPoint.getX() - v_x1*passPoint.getY(); + double a2 = v_y2*((Line2D)line).passPoint.getX() - v_x2*((Line2D)line).passPoint.getY(); double d0 = v_x1*v_y2 - v_y1*v_x2; double dx = a2*v_x1 - a1*v_x2; - double dy = v_y1*a2 + v_y2*a1; + double dy = v_y1*a2 - v_y2*a1; double x = dx/d0; double y = dy/d0; @@ -100,7 +78,7 @@ public Point2D intersect(ILine line) { } @Override - public Point2D intersect(IRay ray) { + public IShape intersect(IRay ray) { if(ray instanceof Ray2D){ if(ray.intersect(new Ray2D(passPoint,direction))!=null || ray.intersect(new Ray2D(passPoint, direction.multiply(-1)))!=null){ @@ -116,11 +94,14 @@ public Point2D intersect(IRay ray) { @Override - public Point2D intersect(ISegment segment) { + public IShape intersect(ISegment segment) { if(segment instanceof Segment2D){ - if(segment.intersect(new Ray2D(passPoint,direction))!=null || - segment.intersect(new Ray2D(passPoint, direction.multiply(-1)))!=null){ - return this.intersect( + boolean intersect = segment.intersect(new Ray2D(passPoint,direction))!=null || + segment.intersect(new Ray2D(passPoint, direction.multiply(-1)))!=null; + if(intersect){ + if(this.pass(((Segment2D) segment).getP1())&&this.pass(((Segment2D) segment).getP2())) + return (Segment2D)segment; + else return this.intersect( new Line2D( (Vector2D) segment.getDirection(), (Point2D) segment.getEnds()[0])); @@ -149,4 +130,42 @@ public boolean equals(Object obj) { } else return false; } + + @Override + public String toString(){ + return String.format("Line2D %f x %+f y = %f", + direction.getY(), + direction.getX(), + direction.getX()*passPoint.getY()-direction.getY()*passPoint.getX()); + } + + @Override + public double distance(IShape shape) { + if(shape instanceof Line2D line){ + if(this.intersect(line)!=null) + return 0; + else{ + Vector2D v = new Vector2D(this.passPoint, line.getOnePassPoint()); + Vector2D p = this.direction.computePerpendicular(); + + return Math.abs(v.dotProduct(p.multiply(1/p.modular()))); + } + } + else if (shape instanceof Ray2D){ + + //todo: finish + return 0; + } + else if(shape instanceof Point2D point){ + Vector2D v = new Vector2D(point,passPoint); + Vector2D p = this.direction.computePerpendicular(); + return Math.abs(v.dotProduct(p.multiply(1/p.modular()))); + } + + else if(shape instanceof Segment2D){ + return 0; + //todo: finish + } + else throw new IllegalArgumentException("Dimension not match or shape not supported."); + } } diff --git a/src/java/TwoD/definition/Point2D.java b/src/java/TwoD/definition/Point2D.java index d47b6b0..c815946 100644 --- a/src/java/TwoD/definition/Point2D.java +++ b/src/java/TwoD/definition/Point2D.java @@ -1,6 +1,7 @@ package TwoD.definition; import IDefinition.IPoint; +import IDefinition.IShape; import java.util.List; @@ -22,13 +23,13 @@ public double getY() { } @Override - public List getValues() { + public List toList() { return List.of(x,y); } @Override - public double distance(IPoint p) { - if(p instanceof Point2D point) { + public double distance(IShape shape) { + if(shape instanceof Point2D point) { return Math.sqrt(Math.pow(point.getX()-x,2)+Math.pow(point.getY()-y,2)); } else throw new IllegalArgumentException("Dimension not match."); diff --git a/src/java/TwoD/definition/Ray2D.java b/src/java/TwoD/definition/Ray2D.java index 7e80bee..c9558f3 100644 --- a/src/java/TwoD/definition/Ray2D.java +++ b/src/java/TwoD/definition/Ray2D.java @@ -2,12 +2,16 @@ import IDefinition.*; +import javax.naming.OperationNotSupportedException; + public class Ray2D implements IRay { private final Point2D startPoint; private final Vector2D direction; public Ray2D(Point2D startPoint, Vector2D direction) { + if(direction.equals(new Vector2D(0,0))) + throw new IllegalArgumentException("Zero vector cannot be a direction."); this.startPoint = startPoint; this.direction = direction; } @@ -29,6 +33,11 @@ public boolean parallel(IRay ray) { return this.direction.parallel(ray.getDirection()); } + + public boolean sameDirection(IRay ray) throws OperationNotSupportedException { + return parallel(ray) && this.direction.uniform().equals(ray.getDirection().uniform()); + } + @Override public boolean pass(IPoint point) { if(point instanceof Point2D){ @@ -38,7 +47,7 @@ public boolean pass(IPoint point) { } @Override - public Point2D intersect(IRay ray) { + public IShape intersect(IRay ray){ if(ray instanceof Ray2D){ boolean intersect = this.pass(((Ray2D) ray).getStartPoint()) @@ -49,15 +58,36 @@ public Point2D intersect(IRay ray) { && ((Ray2D)ray).getDirection().crossProductMod(new Vector2D(((Ray2D) ray).startPoint, this.startPoint))* ((Ray2D)ray).getDirection().crossProductMod(this.direction)<0 ); - if(intersect) - return new Line2D(direction,startPoint).intersect(new Line2D(((Ray2D) ray).direction, ((Ray2D) ray).getStartPoint())); + if(intersect) { + boolean sameDirection = false; + try{ + if (this.sameDirection(ray)) sameDirection = true; + } catch (OperationNotSupportedException e) { + e.printStackTrace(); + } + if(this.direction.parallel(((Ray2D) ray).direction) && sameDirection){ + Point2D interP = + this.pass(((Ray2D) ray).startPoint)?((Ray2D) ray).startPoint:this.startPoint; + return new Ray2D(interP, this.direction); + } + else if(this.direction.parallel(((Ray2D) ray).direction) && !sameDirection && + this.startPoint.equals(((Ray2D) ray).startPoint)){ + return this.startPoint; + } + else if(this.direction.parallel(((Ray2D) ray).direction) && !sameDirection && + !this.startPoint.equals(((Ray2D) ray).startPoint)){ + return new Segment2D(this.startPoint, ((Ray2D) ray).startPoint ); + } + else + return new Line2D(direction, startPoint).intersect(new Line2D(((Ray2D) ray).direction, ((Ray2D) ray).getStartPoint())); + } else return null; } else throw new IllegalArgumentException("Dimension not match."); } @Override - public Point2D intersect(ISegment segment) { + public IShape intersect(ISegment segment){ if(segment instanceof Segment2D){ boolean intersect = this.pass(((Segment2D)segment).getP1()) @@ -68,42 +98,76 @@ public Point2D intersect(ISegment segment) { this.direction.crossProductMod(new Vector2D(startPoint, ((Segment2D) segment).getP2()))<0 && ((Segment2D)segment).getDirection().crossProductMod(new Vector2D(((Segment2D) segment).getP1(), startPoint))* ((Segment2D)segment).getDirection().crossProductMod(direction)<0 - ); - if(intersect) - return new Line2D(direction,startPoint).intersect(new Line2D(((Segment2D) segment).getP1(), ((Segment2D) segment).getP2())); + + ); + + if(intersect){ + if(this.pass(((Segment2D)segment).getP1())&&this.pass(((Segment2D) segment).getP2())) { + return segment; + } + else if(this.direction.parallel(segment.getDirection())){ + Point2D interP = + this.pass(((Segment2D)segment).getP1())?((Segment2D)segment).getP1(): ((Segment2D) segment).getP2(); + if(interP.equals(this.startPoint)) { + return interP; + } + else { + return new Segment2D(interP, this.startPoint); + } + } + else { + return new Line2D(direction, startPoint). + intersect(new Line2D(((Segment2D) segment).getP1(), ((Segment2D) segment).getP2())); + } + } else return null; } else throw new IllegalArgumentException("Dimension not match."); } @Override - public Point2D intersect(ILine line) { + public IShape intersect(ILine line) { if(line instanceof Line2D){ return ((Line2D) line).intersect(this); } else throw new IllegalArgumentException("Dimension not match."); } + @Override - public double distance(IRay ray) { - //todo: finish - return 0; + public Vector2D getDirection() { + return direction; } @Override - public double distance(IPoint point) { - //todo: finish - return 0; + public String toString() { + return "Ray2D from" + startPoint.toString() + "-> Direction:" + direction.toString(); } @Override - public double distance(ILine line) { - //todo: finish + public double distance(IShape shape) { return 0; } @Override - public Vector2D getDirection() { - return direction; + public boolean equals(Object obj){ + if(this==obj) + return true; + if(obj instanceof Ray2D) + return this.startPoint.equals(((Ray2D) obj).startPoint)&& + this.direction.parallel(((Ray2D) obj).direction)&& + this.direction.getX()*((Ray2D) obj).direction.getX()>=0&& + this.direction.getY()*((Ray2D) obj).direction.getY()>=0; + else return false; + } + + @Override + public int hashCode(){ + try { + return this.startPoint.hashCode() + this.direction.uniform().hashCode(); + } catch (OperationNotSupportedException e) { + e.printStackTrace(); + return this.startPoint.hashCode(); + } } } diff --git a/src/java/TwoD/definition/Segment2D.java b/src/java/TwoD/definition/Segment2D.java index 82f5809..1ee5ff3 100644 --- a/src/java/TwoD/definition/Segment2D.java +++ b/src/java/TwoD/definition/Segment2D.java @@ -16,36 +16,61 @@ public Segment2D(Point2D p1, Point2D p2) { @Override - public Point2D intersect(ISegment segment) { + public IShape intersect(ISegment segment) { if(segment instanceof Segment2D){ - boolean intersect = - segment.pass(p1) +// boolean intersect = +// segment.pass(p1) +// ||segment.pass(p2) +// ||this.pass(((Segment2D) segment).getP1()) +// ||this.pass(((Segment2D) segment).getP2()) +// ||( +// v.crossProductMod(new Vector2D(p1, ((Segment2D) segment).getP1()))* +// v.crossProductMod(new Vector2D(p1, ((Segment2D) segment).getP2()))<0 +// && ((Segment2D)segment).getDirection().crossProductMod(new Vector2D(((Segment2D) segment).getP1(), p1))* +// ((Segment2D)segment).getDirection().crossProductMod(new Vector2D(((Segment2D) segment).getP1(), p2))<0 +// ); + boolean onEnd = segment.pass(p1) ||segment.pass(p2) ||this.pass(((Segment2D) segment).getP1()) - ||this.pass(((Segment2D) segment).getP2()) - ||( - v.crossProductMod(new Vector2D(p1, ((Segment2D) segment).getP1()))* - v.crossProductMod(new Vector2D(p1, ((Segment2D) segment).getP2()))<0 - && ((Segment2D)segment).getDirection().crossProductMod(new Vector2D(((Segment2D) segment).getP1(), p1))* - ((Segment2D)segment).getDirection().crossProductMod(new Vector2D(((Segment2D) segment).getP1(), p2))<0 - ); + ||this.pass(((Segment2D) segment).getP2()); + boolean intersect = + v.crossProductMod(new Vector2D(p1, ((Segment2D) segment).getP1()))* + v.crossProductMod(new Vector2D(p1, ((Segment2D) segment).getP2()))<0 + && ((Segment2D)segment).getDirection().crossProductMod(new Vector2D(((Segment2D) segment).getP1(), p1))* + ((Segment2D)segment).getDirection().crossProductMod(new Vector2D(((Segment2D) segment).getP1(), p2))<0; if (intersect) return new Line2D(p1,p2).intersect(new Line2D(((Segment2D) segment).getP1(), ((Segment2D) segment).getP2())); + else if(onEnd && v.parallel(((Segment2D) segment).v)){ + if(this.pass(((Segment2D) segment).p1)&&this.pass(((Segment2D) segment).p2)){ + return segment; + } + else if (((Segment2D)segment).pass(p1)&&((Segment2D)segment).pass(p2)){ + return this; + } + else{ + Point2D interPoint1 = this.pass(((Segment2D) segment).getP1()) ? + ((Segment2D) segment).getP1() : + ((Segment2D) segment).getP2(); + Point2D interPoint2 = ((Segment2D) segment).pass(p1) ? p1 : p2; + return new Segment2D(interPoint1,interPoint2); + } + + } else return null; } else throw new IllegalArgumentException("Dimension not match."); } @Override - public Point2D intersect(ILine line) { + public IShape intersect(ILine line) { if(line instanceof Line2D){ - return ((Line2D) line).intersect(this); + return line.intersect(this); } else throw new IllegalArgumentException("Dimension not match."); } @Override - public Point2D intersect(IRay ray) { + public IShape intersect(IRay ray) { if (ray instanceof Ray2D){ return ((Ray2D)ray).intersect(this); } @@ -62,24 +87,7 @@ public double getLength() { return new Vector2D(p1,p2).modular(); } - @Override - public double distance(ISegment segment) { - if(segment instanceof Segment2D){ - //todo: finish - return 0; - } - else throw new IllegalArgumentException("Dimension not match."); - } - - @Override - public double distance(ILine line) { - return 0; - } - @Override - public double distance(IRay ray) { - return 0; - } @Override public Vector2D getDirection() { @@ -103,4 +111,31 @@ public Point2D getP1() { public Point2D getP2() { return p2; } + + @Override + public String toString() { + return "Segment2D from " + p1 + " to " + p2; + } + + @Override + public double distance(IShape shape) { + return 0; + } + + @Override + public boolean equals(Object o){ + if(this==o) + return true; + if(o instanceof Segment2D){ + return p1.equals(((Segment2D) o).p1)&&p2.equals(((Segment2D) o).p2)|| + p1.equals(((Segment2D) o).p2)&&p2.equals(((Segment2D) o).p1); + } + else + return false; + } + + @Override + public int hashCode(){ + return p1.hashCode() + p2.hashCode(); + } } diff --git a/src/java/TwoD/definition/Vector2D.java b/src/java/TwoD/definition/Vector2D.java index 4173f05..d30fb0e 100644 --- a/src/java/TwoD/definition/Vector2D.java +++ b/src/java/TwoD/definition/Vector2D.java @@ -104,4 +104,8 @@ public Vector2D computePerpendicular(){ public double crossProductMod(Vector2D v){ return this.x*v.y - this.y*v.x; } + + public boolean isZeroVector(){ + return this.modular()==0; + } } diff --git a/src/test/java/BasicElementInitialisationTest.java b/src/test/java/BasicElementInitialisationTest.java new file mode 100644 index 0000000..b2424af --- /dev/null +++ b/src/test/java/BasicElementInitialisationTest.java @@ -0,0 +1,98 @@ +import TwoD.definition.*; +import com.google.common.util.concurrent.RateLimiter; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +import javax.sound.sampled.Line; + +import static com.google.common.truth.Truth.assertThat; +@RunWith(JUnit4.class) +public class BasicElementInitialisationTest { + @Test + public void Point2DCreate(){ + //arrange + Point2D p = new Point2D(0,0); + //act + String s = p.toString(); + //assert + assertThat(s).startsWith("Point2D"); + assertThat(p.getX()).isEqualTo(0); + assertThat(p.getY()).isEqualTo(0); + assertThat(p.toList()).containsExactly(0.0,0.0); + } + + @Test + public void Ray2DCreateStartAndDirection(){ + //arrange + Point2D start = new Point2D(0,0); + Vector2D v = new Vector2D(1,0); + //act + Ray2D r = new Ray2D(start,v); + //assert + assertThat(r.toString()).startsWith("Ray2D"); + System.out.println(r); + assertThat(r.getDirection()).isEqualTo(v); + assertThat(r.getStartPoint()).isEqualTo(start); + } + + @Test + public void Ray2DCreateFromTwoPoints(){ + //arrange + Point2D start = new Point2D(0,0); + Point2D pass = new Point2D(1,0); + //act + Ray2D r = new Ray2D(start,pass); + //assert + assertThat(r.toString()).startsWith("Ray2D"); + System.out.println(r); + assertThat(r.getDirection().parallel(new Vector2D(1,0))).isTrue(); + assertThat(r.getStartPoint()).isEqualTo(start); + } + + @Test + public void SegmentCreate(){ + //arrange + Point2D p1 = new Point2D(0,0); + Point2D p2 = new Point2D(0,1); + //act + Segment2D seg = new Segment2D(p1,p2); + //assert + assertThat(seg.toString()).startsWith("Segment2D"); + System.out.println(seg); + assertThat(seg.getP1()).isEqualTo(p1); + assertThat(seg.getP2()).isEqualTo(p2); + assertThat(seg.getDirection()).isEqualTo(new Vector2D(p1,p2)); + } + + @Test + public void LineCreateFromTwoPoints(){ + //arrange + Point2D p1 = new Point2D(0,0); + Point2D p2 = new Point2D(0,1); + //act + Line2D line = new Line2D(p1,p2); + //assert + assertThat(line.toString()).startsWith("Line2D"); + System.out.println(line); + assertThat(line.getDirection().parallel(new Vector2D(p1,p2))).isTrue(); + assertThat(line.pass(line.getOnePassPoint())).isTrue(); + assertThat(line.pass(p1)).isTrue(); + assertThat(line.pass(p2)).isTrue(); + } + + @Test + public void LineCreateFromDirectionAndPoint(){ + //arrange + Point2D p = new Point2D(0,0); + Vector2D v = new Vector2D(0,1); + //act + Line2D line = new Line2D(v,p); + //assert + assertThat(line.toString()).startsWith("Line2D"); + System.out.println(line); + assertThat(line.getDirection().parallel(v)).isTrue(); + assertThat(line.pass(p)).isTrue(); + assertThat(line.pass(line.getOnePassPoint())).isTrue(); + } +} diff --git a/src/test/java/LineLineIntersectionTest.java b/src/test/java/LineLineIntersectionTest.java new file mode 100644 index 0000000..7e8a2c4 --- /dev/null +++ b/src/test/java/LineLineIntersectionTest.java @@ -0,0 +1,89 @@ +import IDefinition.IShape; +import TwoD.definition.Line2D; +import TwoD.definition.Point2D; +import TwoD.definition.Vector2D; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; +import static com.google.common.truth.Truth.assertThat; + +@RunWith(JUnit4.class) +public class LineLineIntersectionTest { + @Test + public void SameLineIntersection(){ + //arrange + Line2D l1 = new Line2D(new Vector2D(1,0), new Point2D(0,0)); + Line2D l2 = new Line2D(new Vector2D(1,0), new Point2D(1,0)); + //act + IShape result = l1.intersect(l2); + //assert + assertThat(result).isEqualTo(l1); + } + + @Test + public void parallelLineIntersect(){ + //arrange + Line2D l1 = new Line2D(new Vector2D(1,0), new Point2D(0,0)); + Line2D l2 = new Line2D(new Vector2D(1,0), new Point2D(1,1)); + //act + IShape result = l1.intersect(l2); + //assert + assertThat(result).isNull(); + } + + @Test + public void normalIntersection(){ + //arrange + Line2D l1 = new Line2D(new Vector2D(1,1), new Point2D(0,0)); + Line2D l2 = new Line2D(new Vector2D(1,-1), new Point2D(0,2)); + //act + IShape result = l1.intersect(l2); + //assert + assertThat(result).isEqualTo(new Point2D(1,1)); + } + + @Test + public void normalIntersection2(){ + //arrange + Line2D l1 = new Line2D(new Vector2D(1,1), new Point2D(1,1)); + Line2D l2 = new Line2D(new Vector2D(1,-1), new Point2D(0,2)); + //act + IShape result = l1.intersect(l2); + //assert + assertThat(result).isEqualTo(new Point2D(1,1)); + } + + @Test + public void normalIntersection3(){ + //arrange + Line2D l1 = new Line2D(new Vector2D(1,2), new Point2D(1,0)); + Line2D l2 = new Line2D(new Vector2D(2,1), new Point2D(0,1)); + //act + IShape result = l1.intersect(l2); + //assert + assertThat(result).isEqualTo(new Point2D(2,2)); + } + + @Test + public void normalIntersection4(){ + //arrange + Line2D l1 = new Line2D(new Vector2D(1,0), new Point2D(1,0)); + Line2D l2 = new Line2D(new Vector2D(0,1), new Point2D(0,1)); + //act + IShape result = l1.intersect(l2); + //assert + assertThat(result).isEqualTo(new Point2D(0,0)); + } + + @Test + public void normalIntersection5(){ + //arrange + Line2D l1 = new Line2D(new Vector2D(1,1), new Point2D(1,1)); + Line2D l2 = new Line2D(new Vector2D(0,1), new Point2D(5,0)); + //act + IShape result = l1.intersect(l2); + //assert + assertThat(result).isEqualTo(new Point2D(5,5)); + } +} diff --git a/src/test/java/RaysIntersection.java b/src/test/java/RaysIntersection.java new file mode 100644 index 0000000..3ad11b5 --- /dev/null +++ b/src/test/java/RaysIntersection.java @@ -0,0 +1,91 @@ +import IDefinition.ISegment; +import IDefinition.IShape; +import TwoD.definition.Point2D; +import TwoD.definition.Ray2D; + +import TwoD.definition.Segment2D; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +import static com.google.common.truth.Truth.assertThat; + +@RunWith(JUnit4.class) +public class RaysIntersection { + @Test + public void SameRayIntersect(){ + //arrange + Ray2D r1 = new Ray2D(new Point2D(0,0), new Point2D(1,0)); + Ray2D r2 = new Ray2D(new Point2D(0,0), new Point2D(2,0)); + //act + IShape result = r1.intersect(r2); + //assert + assertThat(result).isEqualTo(r1); + } + + @Test + public void parallelNotSameDirectionReturnStartPoint(){ + //arrange + Ray2D r1 = new Ray2D(new Point2D(0,0), new Point2D(1,0)); + Ray2D r2 = new Ray2D(new Point2D(0,0), new Point2D(-2,0)); + //act + IShape result = r1.intersect(r2); + //assert + assertThat(result).isEqualTo(new Point2D(0,0)); + } + + @Test + public void parallelNotSameDirectionReturnSegment(){ + //arrange + Ray2D r1 = new Ray2D(new Point2D(0,0), new Point2D(1,0)); + Ray2D r2 = new Ray2D(new Point2D(1,0), new Point2D(-2,0)); + //act + IShape result = r1.intersect(r2); + //assert + assertThat(result).isEqualTo(new Segment2D(new Point2D(0,0), new Point2D(1,0))); + } + + @Test + public void parallelNotIntersect(){ + //arrange + Ray2D r1 = new Ray2D(new Point2D(0,0), new Point2D(1,0)); + Ray2D r2 = new Ray2D(new Point2D(0,1), new Point2D(-2,1)); + //act + IShape result = r1.intersect(r2); + //assert + assertThat(result).isNull(); + } + + @Test + public void NotParallelNotIntersect(){ + //arrange + Ray2D r1 = new Ray2D(new Point2D(0,0), new Point2D(1,-1)); + Ray2D r2 = new Ray2D(new Point2D(0,1), new Point2D(1,1)); + //act + IShape result = r1.intersect(r2); + //assert + assertThat(result).isNull(); + } + + @Test + public void NormalIntersect1(){ + //arrange + Ray2D r1 = new Ray2D(new Point2D(0,0), new Point2D(2,2)); + Ray2D r2 = new Ray2D(new Point2D(0,2), new Point2D(2,0)); + //act + IShape result = r1.intersect(r2); + //assert + assertThat(result).isEqualTo(new Point2D(1,1)); + } + + @Test + public void NormalIntersect2(){ + //arrange + Ray2D r1 = new Ray2D(new Point2D(0,0), new Point2D(2,2)); + Ray2D r2 = new Ray2D(new Point2D(0,0), new Point2D(2,0)); + //act + IShape result = r1.intersect(r2); + //assert + assertThat(result).isEqualTo(new Point2D(0,0)); + } +} diff --git a/src/test/java/SegmentRayIntersectionTest.java b/src/test/java/SegmentRayIntersectionTest.java new file mode 100644 index 0000000..de37f0d --- /dev/null +++ b/src/test/java/SegmentRayIntersectionTest.java @@ -0,0 +1,92 @@ +import IDefinition.IShape; +import TwoD.definition.Point2D; +import TwoD.definition.Ray2D; +import TwoD.definition.Segment2D; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +import static com.google.common.truth.Truth.assertThat; +@RunWith(JUnit4.class) +public class SegmentRayIntersectionTest { + @Test + public void parallelNotIntersect(){ + //arrange + Ray2D ray = new Ray2D(new Point2D(0,0), new Point2D(1,0)); + Segment2D seg = new Segment2D(new Point2D(0,1), new Point2D(1,1)); + //act + IShape result1 = ray.intersect(seg); + IShape result2 = seg.intersect(ray); + + //assert + assertThat(result1).isNull(); + assertThat(result2).isNull(); + } + + @Test + public void NotParallelNotIntersect(){ + //arrange + Ray2D ray = new Ray2D(new Point2D(0,0), new Point2D(1,0)); + Segment2D seg = new Segment2D(new Point2D(0,1), new Point2D(1,2)); + //act + IShape result1 = ray.intersect(seg); + IShape result2 = seg.intersect(ray); + //assert + assertThat(result1).isNull(); + assertThat(result2).isNull(); + } + + @Test + public void intersectOnEnd(){ + + Ray2D ray = new Ray2D(new Point2D(0,0), new Point2D(1,0)); + Segment2D seg = new Segment2D(new Point2D(0,0), new Point2D(1,2)); + //act + IShape result1 = ray.intersect(seg); + IShape result2 = seg.intersect(ray); + //assert + assertThat(result1).isEqualTo(new Point2D(0,0)); + assertThat(result2).isEqualTo(new Point2D(0,0)); + } + + @Test + public void intersectNormal(){ + //arrange + Ray2D ray = new Ray2D(new Point2D(0,0), new Point2D(1,1)); + Segment2D seg = new Segment2D(new Point2D(0,0.5), new Point2D(2,1.5)); + //act + IShape result1 = ray.intersect(seg); + IShape result2 = seg.intersect(ray); + //assert + assertThat(result1).isEqualTo(new Point2D(1,1)); + assertThat(result2).isEqualTo(new Point2D(1,1)); + } + + @Test + public void rayContainSegment(){ + //arrange + Ray2D ray = new Ray2D(new Point2D(0,0), new Point2D(1,0)); + Segment2D seg = new Segment2D(new Point2D(0,0), new Point2D(1,0)); + //act + IShape result1 = ray.intersect(seg); + IShape result2 = seg.intersect(ray); + //assert + assertThat(result1).isEqualTo(new Segment2D(new Point2D(0,0),new Point2D(1,0))); + assertThat(result2).isEqualTo(new Segment2D(new Point2D(0,0),new Point2D(1,0))); + } + + @Test + public void rayContainPartOfTheSegment(){ + //arrange + Ray2D ray = new Ray2D(new Point2D(0,0), new Point2D(1,0)); + Segment2D seg = new Segment2D(new Point2D(-1,0), new Point2D(1,0)); + //act + IShape result1 = ray.intersect(seg); + IShape result2 = seg.intersect(ray); + //assert + assertThat(result1).isEqualTo(new Segment2D(new Point2D(0,0),new Point2D(1,0))); + assertThat(result2).isEqualTo(new Segment2D(new Point2D(0,0),new Point2D(1,0))); + } + +} diff --git a/src/test/java/SegmentsIntersectTest.java b/src/test/java/SegmentsIntersectTest.java new file mode 100644 index 0000000..8d0b7a2 --- /dev/null +++ b/src/test/java/SegmentsIntersectTest.java @@ -0,0 +1,90 @@ +import IDefinition.IShape; +import TwoD.definition.Point2D; +import TwoD.definition.Segment2D; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +import static com.google.common.truth.Truth.assertThat; + +@RunWith(JUnit4.class) +public class SegmentsIntersectTest { + @Test + public void SegmentNotIntersect() { + //arrange + Segment2D s1 = new Segment2D(new Point2D(0, 0), new Point2D(1, 0)); + Segment2D s2 = new Segment2D(new Point2D(1, 1), new Point2D(2, 1)); + //act + IShape result = s1.intersect(s2); + //assert + assertThat(result).isNull(); + } + + @Test + public void SegmentIntersectOnEnd() { + //arrange + Segment2D s1 = new Segment2D(new Point2D(0, 0), new Point2D(2, 0)); + Segment2D s2 = new Segment2D(new Point2D(1, 0), new Point2D(1, 1)); + //act + IShape result = s1.intersect(s2); + //assert + assertThat(result).isEqualTo(new Point2D(1, 0)); + } + + @Test + public void SegmentIntersectOnTheOtherEnd() { + //arrange + Segment2D s2 = new Segment2D(new Point2D(0, 0), new Point2D(2, 0)); + Segment2D s1 = new Segment2D(new Point2D(1, 0), new Point2D(1, 1)); + //act + IShape result = s1.intersect(s2); + //assert + assertThat(result).isEqualTo(new Point2D(1, 0)); + } + + @Test + public void SegmentIntersectNormal1() { + //arrange + Segment2D s2 = new Segment2D(new Point2D(0, 0), new Point2D(2, 0)); + Segment2D s1 = new Segment2D(new Point2D(1, -1), new Point2D(1, 1)); + //act + IShape result = s1.intersect(s2); + //assert + assertThat(result).isEqualTo(new Point2D(1, 0)); + } + + @Test + public void SegmentIntersectNormal2() { + //arrange + Segment2D s2 = new Segment2D(new Point2D(0, 0), new Point2D(2, 0)); + Segment2D s1 = new Segment2D(new Point2D(0, -2), new Point2D(2, 2)); + //act + IShape result = s1.intersect(s2); + //assert + assertThat(result).isEqualTo(new Point2D(1, 0)); + } + + @Test + public void SegmentContainIntersect() { + //arrange + Segment2D s2 = new Segment2D(new Point2D(0, 0), new Point2D(2, 0)); + Segment2D s1 = new Segment2D(new Point2D(0, 0), new Point2D(1, 0)); + //act + IShape result = s1.intersect(s2); + //assert + assertThat(result).isEqualTo(new Segment2D(new Point2D(0, 0), new Point2D(1, 0))); + } + + @Test + public void SegmentOnTheSameLineIntersect() { + //arrange + Segment2D s2 = new Segment2D(new Point2D(-1, 0), new Point2D(2, 0)); + Segment2D s1 = new Segment2D(new Point2D(0, 0), new Point2D(1, 0)); + //act + IShape result = s1.intersect(s2); + //assert + assertThat(result).isEqualTo(new Segment2D(new Point2D(0, 0), new Point2D(1, 0))); + + } +}