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:
parent
b604c8a70d
commit
1f5dc2c7a5
BIN
bin/Cursor.class
BIN
bin/Cursor.class
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -57,29 +57,40 @@ public class Cursor {
|
|||||||
public void moveCursorToCoordinates(Point goalPoint) throws InterruptedException {
|
public void moveCursorToCoordinates(Point goalPoint) throws InterruptedException {
|
||||||
Point startingCursorPoint = getCurrentCursorPoint();
|
Point startingCursorPoint = getCurrentCursorPoint();
|
||||||
int distanceToMoveCursor = calculateDistanceBetweenPoints(startingCursorPoint, goalPoint);
|
int distanceToMoveCursor = calculateDistanceBetweenPoints(startingCursorPoint, goalPoint);
|
||||||
double thetaDirectionToMoveCursor = calculateThetaBetweenPoints(startingCursorPoint, goalPoint);
|
|
||||||
|
|
||||||
CursorPath cursorPathToFollow = chooseCursorPathToFollowBasedOnDistance(distanceToMoveCursor);
|
CursorPath cursorPathToFollow = chooseCursorPathToFollowBasedOnDistance(distanceToMoveCursor);
|
||||||
System.out.println("Starting from " + startingCursorPoint.x + ", " + startingCursorPoint.y);
|
System.out.println("Starting from " + startingCursorPoint.x + ", " + startingCursorPoint.y);
|
||||||
System.out.println("Moving to " + goalPoint.x + ", " + goalPoint.y);
|
System.out.println("Moving to " + goalPoint.x + ", " + goalPoint.y);
|
||||||
System.out.println("Moving in " + thetaDirectionToMoveCursor / Math.PI * 180 + " degree direction.");
|
System.out.println("Distance to move: " + distanceToMoveCursor);
|
||||||
followCursorPath(startingCursorPoint, thetaDirectionToMoveCursor, cursorPathToFollow);
|
//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 {
|
private void followCursorPath(Point startingCursorPoint, double thetaDirectionToMoveCursor, CursorPath cursorPathToFollow) throws InterruptedException {
|
||||||
for (CursorPoint translationPoint : cursorPathToFollow.getCursorPathPoints()) {
|
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));
|
robotMouseMove(calculatePoint(startingCursorPoint, thetaDirectionToMoveCursor, translationPoint));
|
||||||
Thread.sleep(50);
|
Thread.sleep(50);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private Point calculatePoint(Point startingCursorPoint, double thetaDirectionToMoveCursor, CursorPoint translationPoint) {
|
private Point calculatePoint(Point startingCursorPoint, double theta, CursorPoint translationPoint) {
|
||||||
int x = (int) (startingCursorPoint.x + Math.cos(thetaDirectionToMoveCursor) * translationPoint.x);
|
int x = (int) (startingCursorPoint.x + Math.cos(theta) * translationPoint.x - Math.sin(theta) * translationPoint.y);
|
||||||
int y = (int) (startingCursorPoint.y + Math.sin(thetaDirectionToMoveCursor) * translationPoint.y);
|
int y = (int) (startingCursorPoint.y + Math.sin(theta) * translationPoint.x + Math.cos(theta) * translationPoint.y);
|
||||||
return new Point(x, y);
|
return new Point(x, y);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void robotMouseMove(Point pointToMoveCursorTo) {
|
public void robotMouseMove(Point pointToMoveCursorTo) {
|
||||||
robot.mouseMove(pointToMoveCursorTo.x, pointToMoveCursorTo.y);
|
robot.mouseMove(pointToMoveCursorTo.x, pointToMoveCursorTo.y);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -93,10 +104,10 @@ public class Cursor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public double calculateThetaBetweenPoints(Point a, Point b) {
|
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();
|
return MouseInfo.getPointerInfo().getLocation();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -9,6 +9,7 @@ public class CursorPath {
|
|||||||
private ArrayList<CursorPoint> pathCursorPoints;
|
private ArrayList<CursorPoint> pathCursorPoints;
|
||||||
private int pathNumPoints;
|
private int pathNumPoints;
|
||||||
private int pathDistance;
|
private int pathDistance;
|
||||||
|
private double pathTheta;
|
||||||
private int pathTimespanMilliseconds;
|
private int pathTimespanMilliseconds;
|
||||||
|
|
||||||
public CursorPath(ArrayList<CursorPoint> cursorPoints)
|
public CursorPath(ArrayList<CursorPoint> cursorPoints)
|
||||||
@ -16,6 +17,7 @@ public class CursorPath {
|
|||||||
this.pathCursorPoints = copyCursorPointsWithOffset(cursorPoints);
|
this.pathCursorPoints = copyCursorPointsWithOffset(cursorPoints);
|
||||||
this.pathNumPoints = cursorPoints.size();
|
this.pathNumPoints = cursorPoints.size();
|
||||||
this.pathDistance = calculateCursorPathDistance();
|
this.pathDistance = calculateCursorPathDistance();
|
||||||
|
this.pathTheta = calculateCursorPathTheta();
|
||||||
this.pathTimespanMilliseconds = calculateCursorPathTimespan();
|
this.pathTimespanMilliseconds = calculateCursorPathTimespan();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -38,6 +40,11 @@ public class CursorPath {
|
|||||||
return (int) calculateDistanceBetweenCursorPoints(getStartingCursorPoint(), getEndingCursorPoint());
|
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() {
|
private CursorPoint getStartingCursorPoint() {
|
||||||
return pathCursorPoints.get(0);
|
return pathCursorPoints.get(0);
|
||||||
}
|
}
|
||||||
@ -75,6 +82,10 @@ public class CursorPath {
|
|||||||
return pathDistance;
|
return pathDistance;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public double getCursorPathTheta() {
|
||||||
|
return pathTheta;
|
||||||
|
}
|
||||||
|
|
||||||
public void displayCursorPoints() {
|
public void displayCursorPoints() {
|
||||||
for (CursorPoint p : pathCursorPoints) {
|
for (CursorPoint p : pathCursorPoints) {
|
||||||
System.out.println("(" + p.x + ", " + p.y + "), " + p.time);
|
System.out.println("(" + p.x + ", " + p.y + "), " + p.time);
|
||||||
|
@ -8,33 +8,55 @@ import org.junit.jupiter.api.Test;
|
|||||||
class CursorTest {
|
class CursorTest {
|
||||||
|
|
||||||
Cursor cursor;
|
Cursor cursor;
|
||||||
double toleranceMin;
|
double cursorTolerance;
|
||||||
double toleranceMax;
|
|
||||||
|
|
||||||
void initialize() throws AWTException {
|
void initialize() throws AWTException {
|
||||||
cursor = new Cursor();
|
cursor = new Cursor();
|
||||||
toleranceMin = 0.9999;
|
cursorTolerance = 3;
|
||||||
toleranceMax = 1.0001;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testCalculateThetaBetweenPoints() throws AWTException {
|
void testMoveCursorToCoordinatesHelper() throws InterruptedException, AWTException {
|
||||||
initialize();
|
initialize();
|
||||||
Point a = new Point(0, 0);
|
Point a = new Point(0, 0);
|
||||||
Point b = new Point(10, 0);
|
Point b = new Point(150, 250);
|
||||||
Point c = new Point(0, 10);
|
Point c = new Point(375, 190);
|
||||||
Point d = new Point(-10, 0);
|
Point d = new Point(375, 600);
|
||||||
Point e = new Point(0, -10);
|
Point e = new Point(952, 603);
|
||||||
|
Point f = new Point(1025, 133);
|
||||||
assertInRange(cursor.calculateThetaBetweenPoints(a, b), 0.0);
|
Point g = new Point(543, 582);
|
||||||
assertInRange(cursor.calculateThetaBetweenPoints(a, c), Math.PI / 2);
|
testMoveCursorToCoordinates(a, b);
|
||||||
assertInRange(cursor.calculateThetaBetweenPoints(a, d), Math.PI);
|
testMoveCursorToCoordinates(b, c);
|
||||||
assertInRange(cursor.calculateThetaBetweenPoints(a, e), - Math.PI / 2);
|
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);
|
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))));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user