diff --git a/bin/Cursor.class b/bin/Cursor.class index 372473b..845800f 100644 Binary files a/bin/Cursor.class and b/bin/Cursor.class differ diff --git a/bin/CursorPath.class b/bin/CursorPath.class index 17b5f80..54043a0 100644 Binary files a/bin/CursorPath.class and b/bin/CursorPath.class differ diff --git a/bin/CursorTest.class b/bin/CursorTest.class index 9336681..f4ebd8b 100644 Binary files a/bin/CursorTest.class and b/bin/CursorTest.class differ diff --git a/src/Cursor.java b/src/Cursor.java index 70e4b40..c3672ba 100644 --- a/src/Cursor.java +++ b/src/Cursor.java @@ -57,29 +57,40 @@ public class Cursor { public void moveCursorToCoordinates(Point goalPoint) throws InterruptedException { Point startingCursorPoint = getCurrentCursorPoint(); int distanceToMoveCursor = calculateDistanceBetweenPoints(startingCursorPoint, goalPoint); - double thetaDirectionToMoveCursor = calculateThetaBetweenPoints(startingCursorPoint, goalPoint); CursorPath cursorPathToFollow = chooseCursorPathToFollowBasedOnDistance(distanceToMoveCursor); System.out.println("Starting from " + startingCursorPoint.x + ", " + startingCursorPoint.y); System.out.println("Moving to " + goalPoint.x + ", " + goalPoint.y); - System.out.println("Moving in " + thetaDirectionToMoveCursor / Math.PI * 180 + " degree direction."); - followCursorPath(startingCursorPoint, thetaDirectionToMoveCursor, cursorPathToFollow); + System.out.println("Distance to move: " + distanceToMoveCursor); + //System.out.println("Moving in " + thetaDirectionToMoveCursor / Math.PI * 180 + " degree direction."); + + double theta = calculateThetaBetweenPoints(startingCursorPoint, goalPoint); + double cursorPathTheta = cursorPathToFollow.getCursorPathTheta(); + System.out.println("Theta " + theta + " in degrees " + (theta / Math.PI * 180) % 360); + System.out.println("CursorPathTheta " + cursorPathTheta + " in degrees " + (cursorPathTheta / Math.PI * 180) % 360); + System.out.println("Difference in thetas: " + (theta - cursorPathTheta) + " in degrees " + ((theta - cursorPathTheta) / Math.PI * 180) % 360); + + followCursorPath(startingCursorPoint, (theta - cursorPathTheta), cursorPathToFollow); } private void followCursorPath(Point startingCursorPoint, double thetaDirectionToMoveCursor, CursorPath cursorPathToFollow) throws InterruptedException { for (CursorPoint translationPoint : cursorPathToFollow.getCursorPathPoints()) { + System.out.println("\ndX:" + translationPoint.x + ", dY:" + translationPoint.y); + System.out.println("Translates to: "); + Point translatedPoint = calculatePoint(startingCursorPoint, thetaDirectionToMoveCursor, translationPoint); + System.out.println(translatedPoint.x + "," + translatedPoint.y); robotMouseMove(calculatePoint(startingCursorPoint, thetaDirectionToMoveCursor, translationPoint)); Thread.sleep(50); } } - private Point calculatePoint(Point startingCursorPoint, double thetaDirectionToMoveCursor, CursorPoint translationPoint) { - int x = (int) (startingCursorPoint.x + Math.cos(thetaDirectionToMoveCursor) * translationPoint.x); - int y = (int) (startingCursorPoint.y + Math.sin(thetaDirectionToMoveCursor) * translationPoint.y); + private Point calculatePoint(Point startingCursorPoint, double theta, CursorPoint translationPoint) { + int x = (int) (startingCursorPoint.x + Math.cos(theta) * translationPoint.x - Math.sin(theta) * translationPoint.y); + int y = (int) (startingCursorPoint.y + Math.sin(theta) * translationPoint.x + Math.cos(theta) * translationPoint.y); return new Point(x, y); } - private void robotMouseMove(Point pointToMoveCursorTo) { + public void robotMouseMove(Point pointToMoveCursorTo) { robot.mouseMove(pointToMoveCursorTo.x, pointToMoveCursorTo.y); } @@ -93,10 +104,10 @@ public class Cursor { } public double calculateThetaBetweenPoints(Point a, Point b) { - return Math.atan2((b.y - a.y), (b.x - a.x)); + return Math.atan2(1.0 * (b.y - a.y), 1.0 * (b.x - a.x)); } - private Point getCurrentCursorPoint() { + public Point getCurrentCursorPoint() { return MouseInfo.getPointerInfo().getLocation(); } diff --git a/src/CursorPath.java b/src/CursorPath.java index 81a88c1..3413925 100644 --- a/src/CursorPath.java +++ b/src/CursorPath.java @@ -9,6 +9,7 @@ public class CursorPath { private ArrayList pathCursorPoints; private int pathNumPoints; private int pathDistance; + private double pathTheta; private int pathTimespanMilliseconds; public CursorPath(ArrayList cursorPoints) @@ -16,6 +17,7 @@ public class CursorPath { this.pathCursorPoints = copyCursorPointsWithOffset(cursorPoints); this.pathNumPoints = cursorPoints.size(); this.pathDistance = calculateCursorPathDistance(); + this.pathTheta = calculateCursorPathTheta(); this.pathTimespanMilliseconds = calculateCursorPathTimespan(); } @@ -38,6 +40,11 @@ public class CursorPath { return (int) calculateDistanceBetweenCursorPoints(getStartingCursorPoint(), getEndingCursorPoint()); } + private double calculateCursorPathTheta() { + CursorPoint endingCursorPoint = getEndingCursorPoint(); + return Math.atan2(1.0 * endingCursorPoint.y, 1.0 * endingCursorPoint.x); + } + private CursorPoint getStartingCursorPoint() { return pathCursorPoints.get(0); } @@ -75,6 +82,10 @@ public class CursorPath { return pathDistance; } + public double getCursorPathTheta() { + return pathTheta; + } + public void displayCursorPoints() { for (CursorPoint p : pathCursorPoints) { System.out.println("(" + p.x + ", " + p.y + "), " + p.time); diff --git a/src/CursorTest.java b/src/CursorTest.java index 47b4454..b89ab85 100644 --- a/src/CursorTest.java +++ b/src/CursorTest.java @@ -8,33 +8,55 @@ import org.junit.jupiter.api.Test; class CursorTest { Cursor cursor; - double toleranceMin; - double toleranceMax; + double cursorTolerance; void initialize() throws AWTException { cursor = new Cursor(); - toleranceMin = 0.9999; - toleranceMax = 1.0001; + cursorTolerance = 3; } @Test - void testCalculateThetaBetweenPoints() throws AWTException { + void testMoveCursorToCoordinatesHelper() throws InterruptedException, AWTException { initialize(); Point a = new Point(0, 0); - Point b = new Point(10, 0); - Point c = new Point(0, 10); - Point d = new Point(-10, 0); - Point e = new Point(0, -10); - - assertInRange(cursor.calculateThetaBetweenPoints(a, b), 0.0); - assertInRange(cursor.calculateThetaBetweenPoints(a, c), Math.PI / 2); - assertInRange(cursor.calculateThetaBetweenPoints(a, d), Math.PI); - assertInRange(cursor.calculateThetaBetweenPoints(a, e), - Math.PI / 2); + Point b = new Point(150, 250); + Point c = new Point(375, 190); + Point d = new Point(375, 600); + Point e = new Point(952, 603); + Point f = new Point(1025, 133); + Point g = new Point(543, 582); + testMoveCursorToCoordinates(a, b); + testMoveCursorToCoordinates(b, c); + testMoveCursorToCoordinates(c, d); + testMoveCursorToCoordinates(d, e); + testMoveCursorToCoordinates(e, f); + testMoveCursorToCoordinates(f, g); + testMoveCursorToCoordinates(g, c); + testMoveCursorToCoordinates(c, f); + testMoveCursorToCoordinates(f, b); + testMoveCursorToCoordinates(b, a); } - void assertInRange(double valueToTest, double expectation) { + void testMoveCursorToCoordinates(Point a, Point b) throws InterruptedException { + cursor.robotMouseMove(a); + cursor.moveCursorToCoordinates(b); + Point point = cursor.getCurrentCursorPoint(); + verifyCursorIsInCorrectPlace(point, b); + } + + void verifyCursorIsInCorrectPlace(Point actualPoint, Point expectedPoint) { + assertInRangeByAbsoluteValue(actualPoint.x, expectedPoint.x, cursorTolerance); + assertInRangeByAbsoluteValue(actualPoint.y, expectedPoint.y, cursorTolerance); + } + + void assertInRangeByPercentage(double valueToTest, double expectation, double tolerancePercentage) { System.out.println(valueToTest + " expected: " + expectation); - assertTrue((valueToTest <= (expectation * toleranceMax)) && (valueToTest >= (expectation * toleranceMin))); + assertTrue((valueToTest <= (expectation * (1 + tolerancePercentage))) && (valueToTest >= (expectation * (1 - tolerancePercentage)))); + } + + void assertInRangeByAbsoluteValue(double valueToTest, double expectation, double toleranceAbsolute) { + System.out.println(valueToTest + " expected: " + expectation); + assertTrue((valueToTest <= (expectation + toleranceAbsolute)) && (valueToTest >= (expectation * (1 - toleranceAbsolute)))); } }