1
0
mirror of https://github.com/davpapp/PowerMiner synced 2024-12-22 07:58:48 -05:00

CursorMovement is passing all tests

This commit is contained in:
davpapp 2018-01-30 10:04:20 -05:00
parent b604c8a70d
commit 1f5dc2c7a5
6 changed files with 69 additions and 25 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -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();
}

View File

@ -9,6 +9,7 @@ public class CursorPath {
private ArrayList<CursorPoint> pathCursorPoints;
private int pathNumPoints;
private int pathDistance;
private double pathTheta;
private int pathTimespanMilliseconds;
public CursorPath(ArrayList<CursorPoint> 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);

View File

@ -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))));
}
}