Skip to content

Instantly share code, notes, and snippets.

@asterwolf
Created January 16, 2016 08:31
Show Gist options
  • Save asterwolf/1df0272ba8868b3d803c to your computer and use it in GitHub Desktop.
Save asterwolf/1df0272ba8868b3d803c to your computer and use it in GitHub Desktop.
import java.util.Scanner;
public class DistanceCalculations {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
double bigTriangleWidth;
double bigTriangleHeight;
double bigTriangleAngle;
double bigTriangleHypotenus;
double smallTriangleHypotenus;
double smallTriangleWidth;
double smallTriangleHeight;
double newRobbersX;
double newRobbersY;
System.out.println("Enters in the doors's X coordinate followed " +
"by his Y coordinate");
double doorXCoordinates = input.nextDouble();
double doorYCoordinates = input.nextDouble();
System.out.println("Enters in the robber's X coordinate followed " +
"by his Y coordinate");
double robberXCoordinates = input.nextDouble();
double robberYCoordinates = input.nextDouble();
/*
System.out.println("Enters in the Guard's X coordinate followed " +
"by his Y coordinate");
double gaurdXCoordinates = input.nextDouble();
double gaurdYCoordinates = input.nextDouble();
*/
bigTriangleWidth = Math.abs(doorXCoordinates - robberXCoordinates);
bigTriangleHeight = Math.abs(doorYCoordinates - robberYCoordinates);
bigTriangleAngle = Math.toDegrees(Math.atan(bigTriangleWidth / bigTriangleHeight));
bigTriangleHypotenus = Math.sqrt(Math.pow(bigTriangleWidth,2) + Math.pow(bigTriangleHeight,2));
System.out.printf("The height is %f, the width is %f," +
" the angle is %f, the hypotenuse is %f\n", bigTriangleHeight,
bigTriangleWidth,bigTriangleAngle, bigTriangleHypotenus);
System.out.println("Enter the robber's speed followed by the " +
"the guard's speed");
double robberSpeed = input.nextDouble();
double guardSpeed = input.nextDouble();
smallTriangleHypotenus = robberSpeed;
smallTriangleWidth = Math.sin(Math.toRadians(bigTriangleAngle)) * smallTriangleHypotenus;
smallTriangleHeight = Math.cos(Math.toRadians(bigTriangleAngle)) * smallTriangleHypotenus;
System.out.printf("The small triangle's width is %f, " +
" the small triangle's height is %f\n", smallTriangleWidth, smallTriangleHeight);
if(robberXCoordinates < 0)
newRobbersX = (Math.abs(robberXCoordinates) - smallTriangleWidth) * -1;
else
newRobbersX = robberXCoordinates - smallTriangleWidth;
if(robberYCoordinates < 0)
newRobbersY = (Math.abs(robberYCoordinates) - smallTriangleHeight) * -1;
else
newRobbersY = robberYCoordinates - smallTriangleHeight;
System.out.printf("The robber's new location after moving %.1f " +
"feet towards the fixed location of (%.2f,%.2f) from the location" +
" of (%.2f,%.2f), is now (%.2f,%.2f).\n", robberSpeed, doorXCoordinates, doorYCoordinates, robberXCoordinates, robberYCoordinates, newRobbersX, newRobbersY);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment