Scaling working, but finding the closestPath needs to be fixed

This commit is contained in:
davpapp 2018-02-19 14:25:10 -05:00
parent 6912821b56
commit ee0efadb37
10 changed files with 99558 additions and 50 deletions

View File

@ -87,31 +87,31 @@ public class Cursor {
Thread.sleep(getRandomClickLength()); Thread.sleep(getRandomClickLength());
} }
public void moveAndLeftClickAtCoordinates(Point goalPoint) throws InterruptedException { public void moveAndLeftClickAtCoordinates(Point goalPoint) throws Exception {
moveCursorToCoordinates(goalPoint); moveCursorToCoordinates(goalPoint);
leftClickCursor(); leftClickCursor();
} }
public void moveAndRightClickAtCoordinates(Point goalPoint) throws InterruptedException { public void moveAndRightClickAtCoordinates(Point goalPoint) throws Exception {
moveCursorToCoordinates(goalPoint); moveCursorToCoordinates(goalPoint);
rightClickCursor(); rightClickCursor();
} }
public Point moveAndLeftClickAtCoordinatesWithRandomness(Point goalPoint, int xTolerance, int yTolerance) throws InterruptedException { public Point moveAndLeftClickAtCoordinatesWithRandomness(Point goalPoint, int xTolerance, int yTolerance) throws Exception {
Point randomizedGoalPoint = randomizePoint(goalPoint, xTolerance, yTolerance); Point randomizedGoalPoint = randomizePoint(goalPoint, xTolerance, yTolerance);
moveCursorToCoordinates(randomizedGoalPoint); moveCursorToCoordinates(randomizedGoalPoint);
leftClickCursor(); leftClickCursor();
return randomizedGoalPoint; // Return the point we moved to in case we need precise movement afterwards return randomizedGoalPoint; // Return the point we moved to in case we need precise movement afterwards
} }
public Point moveAndRightlickAtCoordinatesWithRandomness(Point goalPoint, int xTolerance, int yTolerance) throws InterruptedException { public Point moveAndRightlickAtCoordinatesWithRandomness(Point goalPoint, int xTolerance, int yTolerance) throws Exception {
Point randomizedGoalPoint = randomizePoint(goalPoint, xTolerance, yTolerance); Point randomizedGoalPoint = randomizePoint(goalPoint, xTolerance, yTolerance);
moveCursorToCoordinates(randomizedGoalPoint); moveCursorToCoordinates(randomizedGoalPoint);
rightClickCursor(); rightClickCursor();
return randomizedGoalPoint; // Return the point we moved to in case we need precise movement afterwards return randomizedGoalPoint; // Return the point we moved to in case we need precise movement afterwards
} }
public void moveCursorToCoordinates(Point goalPoint) throws InterruptedException { public void moveCursorToCoordinates(Point goalPoint) throws Exception {
Point startingPoint = getCurrentCursorPoint(); Point startingPoint = getCurrentCursorPoint();
int distanceToMoveCursor = getDistanceBetweenPoints(startingPoint, goalPoint); int distanceToMoveCursor = getDistanceBetweenPoints(startingPoint, goalPoint);
double angleToRotateCursorPathTo = getThetaBetweenPoints(startingPoint, goalPoint); double angleToRotateCursorPathTo = getThetaBetweenPoints(startingPoint, goalPoint);
@ -147,33 +147,56 @@ public class Cursor {
robot.mouseMove(pointToMoveCursorTo.x, pointToMoveCursorTo.y); robot.mouseMove(pointToMoveCursorTo.x, pointToMoveCursorTo.y);
} }
private CursorPath chooseCursorPathToFollowBasedOnDistance(int distanceToMoveCursor) { private CursorPath chooseCursorPathToFollowBasedOnDistance(int distanceToMoveCursor) throws Exception {
int newDistanceToMoveCursor = findNearestPathLengthThatExists(distanceToMoveCursor); int newDistanceToMoveCursor = findNearestPathLengthThatExists(distanceToMoveCursor);
double scaleToFactorBy = getScaleToFactorBy(newDistanceToMoveCursor, distanceToMoveCursor); double scaleToFactorBy = getScaleToFactorBy(newDistanceToMoveCursor, distanceToMoveCursor);
System.out.println("new distance to follow cursor is: " + newDistanceToMoveCursor); System.out.println("new distance to follow cursor is: " + newDistanceToMoveCursor + " from scaling by " + scaleToFactorBy);
ArrayList<CursorPath> cursorPathsWithSameDistance = cursorPathsByDistance.get(newDistanceToMoveCursor); ArrayList<CursorPath> cursorPathsWithSameDistance = cursorPathsByDistance.get(newDistanceToMoveCursor);
int indexOfRandomPathToFollow = random.nextInt(cursorPathsWithSameDistance.size());
//CursorPath scaledCursorPath = cursorPathsWithSameDistance.get(indexOfRandomPathToFollow).getScaledCopyOfCursorPath(scaleToFactorBy); CursorPath scaledCursorPath = cursorPathsWithSameDistance.get(random.nextInt(cursorPathsWithSameDistance.size())).getScaledCopyOfCursorPath(scaleToFactorBy);
//System.out.println("Chose the following path: ");
//scaledCursorPath.displayCursorPoints(); return scaledCursorPath;
return cursorPathsByDistance.get(newDistanceToMoveCursor).get(indexOfRandomPathToFollow);//scaledCursorPath; //return cursorPathsByDistance.get(newDistanceToMoveCursor).get(indexOfRandomPathToFollow);//scaledCursorPath;
} }
private int findNearestPathLengthThatExists(int distanceToMoveCursor) { public int findNearestPathLengthThatExists(int distanceToMoveCursor) throws Exception {
int offset = 1; int offset = 0;
boolean reachedMinimumLimit = false;
boolean reachedMaximumLimit = false;
while (cursorPathsByDistance.get(distanceToMoveCursor + offset).size() == 0) { while (cursorPathsByDistance.get(distanceToMoveCursor + offset).size() == 0) {
if (offset > 0) { // Go up
offset = -(offset + 1); if (distanceToMoveCursor + Math.abs(offset) + 1 >= NUMBER_OF_DISTANCES) {
reachedMaximumLimit = true;
}
if (distanceToMoveCursor - Math.abs(offset) - 1 < 0) {
reachedMinimumLimit = true;
}
if (reachedMaximumLimit && reachedMinimumLimit) {
throw new Exception("No paths exist.");
}
if (offset < 0) {
if (!reachedMaximumLimit) {
offset = -offset + 1;
}
else {
offset--;
}
} }
else { else {
offset = -offset + 1; if (!reachedMinimumLimit) {
offset = -offset - 1;
}
else {
offset++;
}
} }
} }
return distanceToMoveCursor + offset; return distanceToMoveCursor + offset;
} }
private double getScaleToFactorBy(int newDistanceToMoveCursor, int distanceToMoveCursor) { private double getScaleToFactorBy(int newDistanceToMoveCursor, int distanceToMoveCursor) {
return (1.0 * newDistanceToMoveCursor / distanceToMoveCursor); return (1.0 * distanceToMoveCursor / newDistanceToMoveCursor);
} }
@ -187,13 +210,13 @@ public class Cursor {
} }
public void displayCursorPaths() { public void displayCursorPaths() {
for (int i = 0; i < 1000; i++) { for (int i = 0; i < NUMBER_OF_DISTANCES; i++) {
System.out.println("There are " + cursorPathsByDistance.get(i).size() + " paths of size " + i); System.out.println("There are " + cursorPathsByDistance.get(i).size() + " paths of size " + i);
} }
System.out.println("--------------"); System.out.println("--------------");
for (int i = 0; i < cursorPathsByDistance.get(1).size(); i++) { /*for (int i = 0; i < cursorPathsByDistance.get(1).size(); i++) {
cursorPathsByDistance.get(1).get(i).displayCursorPoints(); cursorPathsByDistance.get(1).get(i).displayCursorPoints();
} }*/
//cursorPathsByDistance.get(0).get(0).displayCursorPoints(); //cursorPathsByDistance.get(0).get(0).displayCursorPoints();
} }
} }

View File

@ -34,7 +34,6 @@ public class CursorPath {
return normalizedDelayCursorPoints; return normalizedDelayCursorPoints;
} }
// util
private ArrayList<CursorPoint> getTranslatedListOfCursorPoints(ArrayList<CursorPoint> cursorPoints, CursorPoint cursorPointToTranslateBy) { private ArrayList<CursorPoint> getTranslatedListOfCursorPoints(ArrayList<CursorPoint> cursorPoints, CursorPoint cursorPointToTranslateBy) {
ArrayList<CursorPoint> translatedCursorPath = new ArrayList<CursorPoint>(); ArrayList<CursorPoint> translatedCursorPath = new ArrayList<CursorPoint>();
for (CursorPoint cursorPoint : cursorPoints) { for (CursorPoint cursorPoint : cursorPoints) {
@ -43,7 +42,6 @@ public class CursorPath {
return translatedCursorPath; return translatedCursorPath;
} }
//util
private ArrayList<CursorPoint> getNormalizedDelayListOfCursorPoints(ArrayList<CursorPoint> cursorPoints) { private ArrayList<CursorPoint> getNormalizedDelayListOfCursorPoints(ArrayList<CursorPoint> cursorPoints) {
ArrayList<CursorPoint> normalizedDelayCursorPoints = new ArrayList<CursorPoint>(); ArrayList<CursorPoint> normalizedDelayCursorPoints = new ArrayList<CursorPoint>();
for (int i = 0; i < cursorPoints.size() - 1; i++) { for (int i = 0; i < cursorPoints.size() - 1; i++) {
@ -55,7 +53,6 @@ public class CursorPath {
return normalizedDelayCursorPoints; return normalizedDelayCursorPoints;
} }
//util
public CursorPath getScaledCopyOfCursorPath(double factorToScaleBy) { public CursorPath getScaledCopyOfCursorPath(double factorToScaleBy) {
ArrayList<CursorPoint> scaledCursorPath = new ArrayList<CursorPoint>(); ArrayList<CursorPoint> scaledCursorPath = new ArrayList<CursorPoint>();
for (CursorPoint cursorPoint : this.cursorPoints) { for (CursorPoint cursorPoint : this.cursorPoints) {
@ -64,11 +61,9 @@ public class CursorPath {
return new CursorPath(scaledCursorPath, true); return new CursorPath(scaledCursorPath, true);
} }
//util
public CursorPath getRotatedCopyOfCursorPath(double angleToRotateTo) { public CursorPath getRotatedCopyOfCursorPath(double angleToRotateTo) {
ArrayList<CursorPoint> rotatedCursorPath = new ArrayList<CursorPoint>(); ArrayList<CursorPoint> rotatedCursorPath = new ArrayList<CursorPoint>();
double angleToRotateBy = this.theta - angleToRotateTo; double angleToRotateBy = this.theta - angleToRotateTo;
System.out.println("Rotating points by: " + angleToRotateBy);
for (CursorPoint cursorPoint : this.cursorPoints) { for (CursorPoint cursorPoint : this.cursorPoints) {
rotatedCursorPath.add(cursorPoint.getCursorPointRotatedBy(angleToRotateBy)); rotatedCursorPath.add(cursorPoint.getCursorPointRotatedBy(angleToRotateBy));
} }
@ -110,7 +105,7 @@ public class CursorPath {
} }
private boolean isCursorPathDistanceReasonable() { private boolean isCursorPathDistanceReasonable() {
return (this.distance > 0 && this.distance < 1000); return (this.distance > 0 && this.distance < 2200);
} }
private boolean isCursorPathNumPointsReasonable() { private boolean isCursorPathNumPointsReasonable() {

View File

@ -14,7 +14,7 @@ class CursorPathTest {
initializeCursorPath(); initializeCursorPath();
for (CursorPath cursorPath : cursorPaths) { for (CursorPath cursorPath : cursorPaths) {
cursorPath.displayCursorPoints(); //cursorPath.displayCursorPoints();
testCursorPathStartsAtOrigin(cursorPath); testCursorPathStartsAtOrigin(cursorPath);
testEndingCursorPointInCursorPathHasZeroDelay(cursorPath); testEndingCursorPointInCursorPathHasZeroDelay(cursorPath);
@ -23,9 +23,23 @@ class CursorPathTest {
testCursorPathRotation(cursorPath, Math.PI); testCursorPathRotation(cursorPath, Math.PI);
testCursorPathRotation(cursorPath, 7 / 4 * Math.PI); testCursorPathRotation(cursorPath, 7 / 4 * Math.PI);
//testCursorPathRotation(cursorPath, 10.32184);
//testCursorPathRotation(cursorPath, 0.01372);
//testCursorPathRotation(cursorPath, -0.0001238);
//testCursorPathRotation(cursorPath, 0);
testCursorPathScaling(cursorPath, 1.15); testCursorPathScaling(cursorPath, 1.15);
testCursorPathScaling(cursorPath, 0.48324);
testCursorPathScaling(cursorPath, 0.9999);
testCursorPathScaling(cursorPath, 1.8431838);
testCursorPathScaling(cursorPath, 2.10004);
testCursorPathScaling(cursorPath, 1.15);
testCursorPathScaling(cursorPath, 1.001010101);
testCursorPathScaling(cursorPath, 1.523521);
testCursorPathScaling(cursorPath, 1.12366);
testCursorPathScaling(cursorPath, 0.974324);
testCursorPathScaling(cursorPath, 0.72134);
testCopyTime(cursorPath); //testDelays(cursorPath);
} }
} }
@ -66,15 +80,18 @@ class CursorPathTest {
void testCursorPathRotation(CursorPath cursorPath, double angleToRotateTo) { void testCursorPathRotation(CursorPath cursorPath, double angleToRotateTo) {
CursorPath rotatedCursorPath = cursorPath.getRotatedCopyOfCursorPath(angleToRotateTo); CursorPath rotatedCursorPath = cursorPath.getRotatedCopyOfCursorPath(angleToRotateTo);
assertEquals(angleToRotateTo, rotatedCursorPath.getCursorPathTheta()); assertEquals(angleToRotateTo % Math.PI, ((rotatedCursorPath.getCursorPathTheta() % Math.PI) + Math.PI) % Math.PI, 0.01);
ArrayList<CursorPoint> cursorPoints = cursorPath.getCursorPathPoints(); ArrayList<CursorPoint> cursorPoints = cursorPath.getCursorPathPoints();
ArrayList<CursorPoint> rotatedCursorPoints = rotatedCursorPath.getCursorPathPoints(); ArrayList<CursorPoint> rotatedCursorPoints = rotatedCursorPath.getCursorPathPoints();
assertEquals(cursorPoints.size(), rotatedCursorPoints.size()); assertEquals(cursorPoints.size(), rotatedCursorPoints.size());
assertEquals(cursorPath.getStartingCursorPoint().x, rotatedCursorPath.getStartingCursorPoint().x);
assertEquals(cursorPath.getStartingCursorPoint().y, rotatedCursorPath.getStartingCursorPoint().y);
} }
void testCopyTime(CursorPath cursorPath) { void testDelays(CursorPath cursorPath) {
CursorPath rotatedCursorPath = cursorPath.getRotatedCopyOfCursorPath(2.3 / 9.0 * Math.PI); CursorPath rotatedCursorPath = cursorPath.getRotatedCopyOfCursorPath(2.3 / 9.0 * Math.PI);
ArrayList<CursorPoint> cursorPoints = cursorPath.getCursorPathPoints(); ArrayList<CursorPoint> cursorPoints = cursorPath.getCursorPathPoints();
ArrayList<CursorPoint> rotatedCursorPoints = rotatedCursorPath.getCursorPathPoints(); ArrayList<CursorPoint> rotatedCursorPoints = rotatedCursorPath.getCursorPathPoints();
@ -90,9 +107,9 @@ class CursorPathTest {
ArrayList<CursorPoint> cursorPoints = cursorPath.getCursorPathPoints(); ArrayList<CursorPoint> cursorPoints = cursorPath.getCursorPathPoints();
ArrayList<CursorPoint> scaledCursorPoints = scaledCursorPath.getCursorPathPoints(); ArrayList<CursorPoint> scaledCursorPoints = scaledCursorPath.getCursorPathPoints();
assertEquals(cursorPoints.size(), scaledCursorPoints.size()); assertEquals(cursorPoints.size(), scaledCursorPoints.size());
}
assertEquals(cursorPath.getStartingCursorPoint().x, scaledCursorPath.getStartingCursorPoint().x);
void assertInRangeByTolerance(double expected, double actual, double tolerance) { assertEquals(cursorPath.getStartingCursorPoint().y, scaledCursorPath.getStartingCursorPoint().y);
assertTrue((actual <= (expected + tolerance)) && (actual >= (expected - tolerance))); assertEquals(cursorPath.getCursorPathDistance() * factorToScaleBy, scaledCursorPath.getCursorPathDistance(), 3);
} }
} }

View File

@ -16,15 +16,23 @@ class CursorTest {
} }
@Test @Test
void testMoveCursorToCoordinatesHelper() throws InterruptedException, AWTException { void testMoveCursorToCoordinatesHelper() throws Exception {
initialize(); initialize();
cursor.displayCursorPaths(); //cursor.displayCursorPaths();
testThetaBetweenPoints(); testThetaBetweenPoints();
testFindNearestPathLengthThatExists();
testMoveCursorToCoordinates(); testMoveCursorToCoordinates();
//testRightClickCursor(); //testRightClickCursor();
} }
void testFindNearestPathLengthThatExists() throws Exception {
for (int i = 0; i < 2203; i++) {
int closestLength = cursor.findNearestPathLengthThatExists(i);
System.out.println("Closest path to length " + i + " is " + closestLength);
}
}
void testThetaBetweenPoints() { void testThetaBetweenPoints() {
Point a = new Point(0, 0); Point a = new Point(0, 0);
Point b = new Point(10, 0); Point b = new Point(10, 0);
@ -36,7 +44,7 @@ class CursorTest {
assertEquals(degreesToRadians(45), cursor.getThetaBetweenPoints(b, d)); assertEquals(degreesToRadians(45), cursor.getThetaBetweenPoints(b, d));
} }
void testMoveCursorToCoordinates() throws InterruptedException { void testMoveCursorToCoordinates() throws Exception {
Point a = new Point(0, 0); Point a = new Point(0, 0);
Point b = new Point(150, 250); Point b = new Point(150, 250);
Point c = new Point(375, 190); Point c = new Point(375, 190);
@ -44,6 +52,18 @@ class CursorTest {
Point e = new Point(952, 603); Point e = new Point(952, 603);
Point f = new Point(1025, 133); Point f = new Point(1025, 133);
Point g = new Point(543, 582); Point g = new Point(543, 582);
Point h = new Point(1643, 582);
Point i = new Point(0, 582);
Point j = new Point(954, 123);
Point k = new Point(954, 960);
Point l = new Point(384, 654);
Point m = new Point(84, 43);
Point n = new Point(84, 43);
Point o = new Point(12, 842);
Point p = new Point(1600, 514);
Point q = new Point(1599, 513);
Point r = new Point(1600, 515);
Point s = new Point(1602, 513);
testMoveCursorToCoordinates(a, b); testMoveCursorToCoordinates(a, b);
testMoveCursorToCoordinates(b, c); testMoveCursorToCoordinates(b, c);
testMoveCursorToCoordinates(c, d); testMoveCursorToCoordinates(c, d);
@ -55,6 +75,19 @@ class CursorTest {
testMoveCursorToCoordinates(f, b); testMoveCursorToCoordinates(f, b);
testMoveCursorToCoordinates(b, a); testMoveCursorToCoordinates(b, a);
testMoveCursorToCoordinates(a, g); testMoveCursorToCoordinates(a, g);
testMoveCursorToCoordinates(g, h);
testMoveCursorToCoordinates(h, i);
testMoveCursorToCoordinates(i, j);
testMoveCursorToCoordinates(j, k);
testMoveCursorToCoordinates(k, l);
testMoveCursorToCoordinates(l, m);
testMoveCursorToCoordinates(m, n);
testMoveCursorToCoordinates(n, o);
testMoveCursorToCoordinates(o, p);
testMoveCursorToCoordinates(p, q);
testMoveCursorToCoordinates(q, r);
testMoveCursorToCoordinates(r, s);
testMoveCursorToCoordinates(s, a);
} }
/*void testRightClickCursor() throws InterruptedException { /*void testRightClickCursor() throws InterruptedException {
@ -77,27 +110,20 @@ class CursorTest {
// Way to verify that context menu is open? // Way to verify that context menu is open?
}*/ }*/
void testMoveCursorToCoordinates(Point a, Point b) throws InterruptedException { void testMoveCursorToCoordinates(Point a, Point b) throws Exception {
cursor.robotMouseMove(a); cursor.robotMouseMove(a);
cursor.moveCursorToCoordinates(b); cursor.moveCursorToCoordinates(b);
Point point = cursor.getCurrentCursorPoint(); Point point = cursor.getCurrentCursorPoint();
System.out.println("Cursor ended up on " + point.x + "," + point.y); //System.out.println("Cursor ended up on " + point.x + "," + point.y);
verifyCursorIsInCorrectPlace(point, b); verifyCursorIsInCorrectPlace(point, b);
Thread.sleep(500); Thread.sleep(250);
} }
void verifyCursorIsInCorrectPlace(Point actualPoint, Point expectedPoint) { void verifyCursorIsInCorrectPlace(Point actualPoint, Point expectedPoint) {
assertInRangeByAbsoluteValue(actualPoint.x, expectedPoint.x, cursorTolerance); assertEquals(actualPoint.x, expectedPoint.x, cursorTolerance);
assertInRangeByAbsoluteValue(actualPoint.y, expectedPoint.y, cursorTolerance); assertEquals(actualPoint.y, expectedPoint.y, cursorTolerance);
} }
void assertInRangeByPercentage(double valueToTest, double expectation, double tolerancePercentage) {
assertTrue((valueToTest <= (expectation * (1 + tolerancePercentage))) && (valueToTest >= (expectation * (1 - tolerancePercentage))));
}
void assertInRangeByAbsoluteValue(double valueToTest, double expectation, double toleranceAbsolute) {
assertTrue((valueToTest <= (expectation + toleranceAbsolute)) && (valueToTest >= (expectation * (1 - toleranceAbsolute))));
}
private double degreesToRadians(double degrees) { private double degreesToRadians(double degrees) {
return degrees * Math.PI / 180.0; return degrees * Math.PI / 180.0;

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

File diff suppressed because it is too large Load Diff