Refactored some code

This commit is contained in:
davpapp 2018-01-30 10:19:12 -05:00
parent 1f5dc2c7a5
commit 06ca105591
6 changed files with 17 additions and 28 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -57,36 +57,25 @@ public class Cursor {
public void moveCursorToCoordinates(Point goalPoint) throws InterruptedException {
Point startingCursorPoint = getCurrentCursorPoint();
int distanceToMoveCursor = calculateDistanceBetweenPoints(startingCursorPoint, goalPoint);
double angleToMoveCursor = 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("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);
double angleToTranslatePathBy = angleToMoveCursor - cursorPathToFollow.getCursorPathTheta();
followCursorPath(startingCursorPoint, angleToTranslatePathBy, 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 void followCursorPath(Point startingCursorPoint, double angleToTranslatePathBy, CursorPath cursorPathToFollow) throws InterruptedException {
for (CursorPoint untranslatedCursorPoint : cursorPathToFollow.getCursorPathPoints()) {
Point translatedPointToClick = translatePoint(startingCursorPoint, angleToTranslatePathBy, untranslatedCursorPoint);
robotMouseMove(translatedPointToClick);
int millisecondsToSleep = 50;
Thread.sleep(millisecondsToSleep);
}
}
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);
private Point translatePoint(Point startingCursorPoint, double angleToTranslateBy, CursorPoint untranslatedCursorPoint) {
int x = (int) (startingCursorPoint.x + Math.cos(angleToTranslateBy) * untranslatedCursorPoint.x - Math.sin(angleToTranslateBy) * untranslatedCursorPoint.y);
int y = (int) (startingCursorPoint.y + Math.sin(angleToTranslateBy) * untranslatedCursorPoint.x + Math.cos(angleToTranslateBy) * untranslatedCursorPoint.y);
return new Point(x, y);
}

View File

@ -25,13 +25,15 @@ public class CursorPath {
ArrayList<CursorPoint> cursorPointsCopy = new ArrayList<CursorPoint>(cursorPoints.size());
CursorPoint startingCursorPoint = cursorPoints.get(0);
for (CursorPoint cursorPoint : cursorPoints) {
CursorPoint offsetCursorPoint = new CursorPoint(cursorPoint.x - startingCursorPoint.x,
cursorPoint.y - startingCursorPoint.y,cursorPoint.time - startingCursorPoint.time);
cursorPointsCopy.add(offsetCursorPoint);
cursorPointsCopy.add(getOffsetCursorPoint(cursorPoint, startingCursorPoint));
}
return cursorPointsCopy;
}
private CursorPoint getOffsetCursorPoint(CursorPoint cursorPoint, CursorPoint offsetPoint) {
return new CursorPoint(cursorPoint.x - offsetPoint.x, cursorPoint.y - offsetPoint.y,cursorPoint.time - offsetPoint.time);
}
private int calculateCursorPathTimespan() {
return getEndingCursorPoint().time - getStartingCursorPoint().time;
}

View File

@ -50,12 +50,10 @@ class CursorTest {
}
void assertInRangeByPercentage(double valueToTest, double expectation, double tolerancePercentage) {
System.out.println(valueToTest + " expected: " + expectation);
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))));
}