From 3a57c296d78a828c3b10aee7bc3bc56998a7a5fb Mon Sep 17 00:00:00 2001 From: divyesh000 Date: Wed, 31 Jul 2024 19:00:56 +0400 Subject: [PATCH 1/3] write tests for ShapeWrapper --- .../github/creme332/model/ShapeWrapper.java | 3 + .../tests/model/ShapeWrapperTest.java | 121 ++++++++++++++++++ 2 files changed, 124 insertions(+) create mode 100644 src/test/java/com/github/creme332/tests/model/ShapeWrapperTest.java diff --git a/src/main/java/com/github/creme332/model/ShapeWrapper.java b/src/main/java/com/github/creme332/model/ShapeWrapper.java index bbb8e484..964debae 100644 --- a/src/main/java/com/github/creme332/model/ShapeWrapper.java +++ b/src/main/java/com/github/creme332/model/ShapeWrapper.java @@ -214,6 +214,9 @@ public void rotate(double radAngle, Point2D pivot) { * @return True if point is inside shape or on its border */ public boolean isPointOnShape(Point2D point) { + if (shape == null) { + return false; + } return shape.contains(point) || isPointOnShapeBorder(shape, point); } diff --git a/src/test/java/com/github/creme332/tests/model/ShapeWrapperTest.java b/src/test/java/com/github/creme332/tests/model/ShapeWrapperTest.java new file mode 100644 index 00000000..a72ea87d --- /dev/null +++ b/src/test/java/com/github/creme332/tests/model/ShapeWrapperTest.java @@ -0,0 +1,121 @@ +package com.github.creme332.tests.model; + +import org.junit.Before; +import org.junit.Test; +import static org.junit.Assert.*; + +import java.awt.Color; +import java.awt.Polygon; +import java.awt.geom.Point2D; +import java.util.Arrays; +import java.util.List; + +import com.github.creme332.model.ShapeWrapper; +import com.github.creme332.model.LineType; + +public class ShapeWrapperTest { + + private ShapeWrapper originalShapeWrapper; + private Polygon polygon; + + @Before + public void setUp() { + polygon = new Polygon(new int[] { 0, 0, 1 }, new int[] { 0, 1, 1 }, 3); + originalShapeWrapper = new ShapeWrapper(Color.BLUE, LineType.DASHED, 2); + originalShapeWrapper.setShape(polygon); + originalShapeWrapper.getPlottedPoints().add(new Point2D.Double(0, 0)); + originalShapeWrapper.getPlottedPoints().add(new Point2D.Double(0, 1)); + originalShapeWrapper.getPlottedPoints().add(new Point2D.Double(1, 1)); + } + + @Test + public void testCopyConstructor() { + ShapeWrapper copiedShapeWrapper = new ShapeWrapper(originalShapeWrapper); + + // Check if the primitive attributes are copied correctly + assertEquals(originalShapeWrapper.getLineColor(), copiedShapeWrapper.getLineColor()); + assertEquals(originalShapeWrapper.getLineType(), copiedShapeWrapper.getLineType()); + assertEquals(originalShapeWrapper.getLineThickness(), copiedShapeWrapper.getLineThickness()); + assertEquals(originalShapeWrapper.isFillable(), copiedShapeWrapper.isFillable()); + + // Check if the shape is copied correctly + assertTrue(copiedShapeWrapper.getShape() instanceof Polygon); + Polygon copiedPolygon = (Polygon) copiedShapeWrapper.getShape(); + assertArrayEquals(polygon.xpoints, copiedPolygon.xpoints); + assertArrayEquals(polygon.ypoints, copiedPolygon.ypoints); + assertEquals(polygon.npoints, copiedPolygon.npoints); + + // Check if the plotted points are copied correctly + assertEquals(originalShapeWrapper.getPlottedPoints().size(), copiedShapeWrapper.getPlottedPoints().size()); + for (int i = 0; i < originalShapeWrapper.getPlottedPoints().size(); i++) { + Point2D originalPoint = originalShapeWrapper.getPlottedPoints().get(i); + Point2D copiedPoint = copiedShapeWrapper.getPlottedPoints().get(i); + assertEquals(originalPoint.getX(), copiedPoint.getX(), 0.001); + assertEquals(originalPoint.getY(), copiedPoint.getY(), 0.001); + } + } + + @Test + public void testTranslate() { + Point2D translationVector = new Point2D.Double(2, 2); + originalShapeWrapper.translate(translationVector); + + Polygon translatedPolygon = (Polygon) originalShapeWrapper.getShape(); + assertArrayEquals(new int[] { 2, 2, 3 }, translatedPolygon.xpoints); + assertArrayEquals(new int[] { 2, 3, 3 }, translatedPolygon.ypoints); + + assertEquals(new Point2D.Double(2, 2), originalShapeWrapper.getPlottedPoints().get(0)); + assertEquals(new Point2D.Double(2, 3), originalShapeWrapper.getPlottedPoints().get(1)); + assertEquals(new Point2D.Double(3, 3), originalShapeWrapper.getPlottedPoints().get(2)); + } + + @Test + public void testRotate() { + // Define a square shape + Polygon square = new Polygon(new int[] { 0, 1, 1, 0 }, new int[] { 0, 0, 1, 1 }, 4); + ShapeWrapper shapeWrapper = new ShapeWrapper(Color.BLACK, LineType.SOLID, 1); + shapeWrapper.setShape(square); + shapeWrapper.getPlottedPoints().addAll(Arrays.asList( + new Point2D.Double(0, 0), + new Point2D.Double(1, 0), + new Point2D.Double(1, 1), + new Point2D.Double(0, 1))); + + Point2D pivot = new Point2D.Double(0.5, 0.5); + shapeWrapper.rotate(Math.PI / 2, pivot); // Rotate 90 degrees + + List expectedPoints = Arrays.asList( + new Point2D.Double(1, 0), + new Point2D.Double(1, 1), + new Point2D.Double(0, 1), + new Point2D.Double(0, 0)); + + for (int i = 0; i < shapeWrapper.getPlottedPoints().size(); i++) { + Point2D actual = shapeWrapper.getPlottedPoints().get(i); + Point2D expected = expectedPoints.get(i); + assertEquals(expected.getX(), actual.getX(), 0.0001); + assertEquals(expected.getY(), actual.getY(), 0.0001); + } + } + + @Test + public void testIsPointOnShape() { + // Define a simple square shape + Polygon square = new Polygon(new int[] { 0, 1, 1, 0 }, new int[] { 0, 0, 1, 1 }, 4); + ShapeWrapper shapeWrapper = new ShapeWrapper(Color.BLACK, LineType.SOLID, 1); + shapeWrapper.setShape(square); + + // Inside the shape + assertTrue(shapeWrapper.isPointOnShape(new Point2D.Double(0.5, 0.5))); + + // Outside the shape + assertFalse(shapeWrapper.isPointOnShape(new Point2D.Double(1.5, 1.5))); + + // On the border of the shape + assertTrue(shapeWrapper.isPointOnShape(new Point2D.Double(1, 0))); + assertTrue(shapeWrapper.isPointOnShape(new Point2D.Double(0, 0))); + assertTrue(shapeWrapper.isPointOnShape(new Point2D.Double(1, 1))); + assertTrue(shapeWrapper.isPointOnShape(new Point2D.Double(0, 1))); + } + +} From a4cf931541fe64e2db5d53229f81ac0e5c6b8f59 Mon Sep 17 00:00:00 2001 From: creme332 <65414576+creme332@users.noreply.github.com> Date: Fri, 2 Aug 2024 17:27:46 +0400 Subject: [PATCH 2/3] reduce tolerance in isPointOnShapeBorder --- src/main/java/com/github/creme332/model/ShapeWrapper.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/github/creme332/model/ShapeWrapper.java b/src/main/java/com/github/creme332/model/ShapeWrapper.java index 964debae..666fc6c4 100644 --- a/src/main/java/com/github/creme332/model/ShapeWrapper.java +++ b/src/main/java/com/github/creme332/model/ShapeWrapper.java @@ -211,7 +211,8 @@ public void rotate(double radAngle, Point2D pivot) { /** * * @param point - * @return True if point is inside shape or on its border + * @return True if point is inside shape or on its border within a specified + * tolerance. */ public boolean isPointOnShape(Point2D point) { if (shape == null) { @@ -231,7 +232,7 @@ public boolean isPointOnShape(Point2D point) { * @return true if the point is on the shape's border, false otherwise */ public static boolean isPointOnShapeBorder(Shape shape, Point2D point) { - final double TOLERANCE = 3.0; + final double TOLERANCE = 1.0; if (shape == null) { return false; From 1f45383f8cb3fa01eb5865454e81af4f8f071926 Mon Sep 17 00:00:00 2001 From: creme332 <65414576+creme332@users.noreply.github.com> Date: Fri, 2 Aug 2024 17:28:02 +0400 Subject: [PATCH 3/3] update tests to cater for tolerance --- .../com/github/creme332/tests/model/ShapeWrapperTest.java | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/test/java/com/github/creme332/tests/model/ShapeWrapperTest.java b/src/test/java/com/github/creme332/tests/model/ShapeWrapperTest.java index a72ea87d..3cef9d8c 100644 --- a/src/test/java/com/github/creme332/tests/model/ShapeWrapperTest.java +++ b/src/test/java/com/github/creme332/tests/model/ShapeWrapperTest.java @@ -108,8 +108,11 @@ public void testIsPointOnShape() { // Inside the shape assertTrue(shapeWrapper.isPointOnShape(new Point2D.Double(0.5, 0.5))); - // Outside the shape - assertFalse(shapeWrapper.isPointOnShape(new Point2D.Double(1.5, 1.5))); + // Outside the shape but within tolerance region + assertTrue(shapeWrapper.isPointOnShape(new Point2D.Double(1.5, 1.5))); + + // Outside the shape but not within tolerance region + assertFalse(shapeWrapper.isPointOnShape(new Point2D.Double(3, 3))); // On the border of the shape assertTrue(shapeWrapper.isPointOnShape(new Point2D.Double(1, 0)));