diff --git a/core-java-modules/core-java-lang-math-3/src/main/java/com/baeldung/math/rotatevertex/VertexRotation.java b/core-java-modules/core-java-lang-math-3/src/main/java/com/baeldung/math/rotatevertex/VertexRotation.java new file mode 100644 index 0000000000..b8ec6a9065 --- /dev/null +++ b/core-java-modules/core-java-lang-math-3/src/main/java/com/baeldung/math/rotatevertex/VertexRotation.java @@ -0,0 +1,26 @@ +package com.baeldung.math.rotatevertex; + +import java.awt.geom.AffineTransform; +import java.awt.geom.Point2D; + +public class VertexRotation { + public static Point2D.Double usingOriginAsRotationPoint(Point2D.Double vertex, Point2D.Double rotationPoint, double angle) { + double translatedToOriginX = vertex.x - rotationPoint.x; + double translatedToOriginY = vertex.y - rotationPoint.y; + + double rotatedX = translatedToOriginX * Math.cos(angle) - translatedToOriginY * Math.sin(angle); + double rotatedY = translatedToOriginX * Math.sin(angle) + translatedToOriginY * Math.cos(angle); + + double reverseTranslatedX = rotatedX + rotationPoint.x; + double reverseTranslatedY = rotatedY + rotationPoint.y; + + return new Point2D.Double(reverseTranslatedX, reverseTranslatedY); + } + + public static Point2D.Double usingAffineTransform(Point2D.Double vertex, Point2D.Double rotationPoint, double angle) { + AffineTransform affineTransform = AffineTransform.getRotateInstance(angle, rotationPoint.x, rotationPoint.y); + Point2D.Double rotatedVertex = new Point2D.Double(); + affineTransform.transform(vertex, rotatedVertex); + return rotatedVertex; + } +} diff --git a/core-java-modules/core-java-lang-math-3/src/test/java/com/baeldung/math/rotatevertex/VertexRotationUnitTest.java b/core-java-modules/core-java-lang-math-3/src/test/java/com/baeldung/math/rotatevertex/VertexRotationUnitTest.java new file mode 100644 index 0000000000..50b1aaf196 --- /dev/null +++ b/core-java-modules/core-java-lang-math-3/src/test/java/com/baeldung/math/rotatevertex/VertexRotationUnitTest.java @@ -0,0 +1,31 @@ +package com.baeldung.math.rotatevertex; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.awt.geom.Point2D; + +import org.junit.jupiter.api.Test; + +public class VertexRotationUnitTest { + @Test + void givenRotationPoint_whenUseOrigin_thenRotateVertex() { + Point2D.Double vertex = new Point2D.Double(2.0, 2.0); + Point2D.Double rotationPoint = new Point2D.Double(0.0, 1.0); + double angle = Math.toRadians(45.0); + Point2D.Double rotatedVertex = VertexRotation.usingOriginAsRotationPoint(vertex, rotationPoint, angle); + + assertEquals(0.707, rotatedVertex.getX(), 0.001); + assertEquals(3.121, rotatedVertex.getY(), 0.001); + } + + @Test + void givenRotationPoint_whenUseAffineTransform_thenRotateVertex() { + Point2D.Double vertex = new Point2D.Double(2.0, 2.0); + Point2D.Double rotationPoint = new Point2D.Double(0.0, 1.0); + double angle = Math.toRadians(45.0); + Point2D.Double rotatedVertex = VertexRotation.usingAffineTransform(vertex, rotationPoint, angle); + + assertEquals(0.707, rotatedVertex.getX(), 0.001); + assertEquals(3.121, rotatedVertex.getY(), 0.001); + } +}