Skip to content

Instantly share code, notes, and snippets.

@asterwolf
Last active February 8, 2016 01:38

Revisions

  1. asterwolf revised this gist Feb 8, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion DistanceMain.java
    Original file line number Diff line number Diff line change
    @@ -39,7 +39,7 @@ public static void main(String[] args){
    double speed = input.nextDouble();

    if(XIsSmaller && YIsSmaller){
    //System.out.println("true");
    //System.out.println("true");
    while(bigTriangle.getRobbersX() < doorXCoordinate && bigTriangle.getRobbersY() < doorYCoordinate){
    bigTriangle.setWidth(doorXCoordinate);
    bigTriangle.setLength(doorYCoordinate);
  2. asterwolf created this gist Feb 8, 2016.
    141 changes: 141 additions & 0 deletions Distance.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,141 @@
    public class Distance{
    public double bigTriangleWidth;
    public double bigTriangleLength;
    public double bigTriangleAngle;
    public double robbersXCoordinate;
    public double robbersYCoordinate;

    /*
    * Constructor sets the robber's X and Y coordinates from input.
    * @ param - input representing the robbers X and Y coordinates.
    */
    public Distance(double robbersXCoordinate, double robbersYCoordinate){
    this.robbersXCoordinate = robbersXCoordinate;
    this.robbersYCoordinate = robbersYCoordinate;
    }

    /*
    * Setter, setting the width of the big triangle by taking the distance of
    * the robber's X coordinate and door's X coordinate.
    * @ param - doors X coordinate (destination).
    */
    public void setWidth(double doorXCoordinate){
    bigTriangleWidth = Math.abs(doorXCoordinate - robbersXCoordinate);
    }

    /*
    * Setter, setting the Length of the big triangle by taking the distance of
    * the robber's Y coordinate and door's Y coordinate.
    * @ param - doors Y coordinate (destination).
    */
    public void setLength(double doorYCoordinate){
    bigTriangleLength = Math.abs(doorYCoordinate - robbersYCoordinate);
    }

    /*
    * Setter, solving the Angle of the big triangle from the robber's coordinate.
    */
    public void setTheta(){
    bigTriangleAngle = Math.toDegrees(Math.atan(bigTriangleWidth / bigTriangleLength));
    }

    /*
    * Setter, setting the hypotenuse of the small triangle representing
    * how fast the robber is moving toward the door coordinates.
    * @ param - input of the robber's speed representing small triangle's
    * hypotenuse.
    */
    public void hypotenuse(double speed){
    double hypotenuse = speed;
    smallWidth(hypotenuse);
    smallLength(hypotenuse);
    }

    /*
    * Setter, setting length of the small triangle.
    * @ param - using the theta angle and hypotenuse to solve for the adjacent
    * side of the small triangle.
    */
    public void smallLength(double hypotenuse){
    double smallTriangleLength = Math.cos(Math.toRadians(bigTriangleAngle)) * hypotenuse;
    setRobbersYCoordinate(smallTriangleLength);
    }

    /*
    * Setter, setting width of the small triangle.
    * @ param - using the theta angle and hypotenuse to solve for the opposite
    * side of the small triangle.
    */
    public void smallWidth(double hypotenuse){
    double smallTriangleWidth = Math.sin(Math.toRadians(bigTriangleAngle)) * hypotenuse;
    setRobbersXCoordinate(smallTriangleWidth);
    }

    /*
    * Setter, recalculating the robber's new X coordinate by subtracting the small
    * triangle's width by the big triangle's width.
    * @ param - small triangle's width. sent from the method hypotenuse.
    */
    public void setRobbersXCoordinate(double smallTriangleWidth){
    if(robbersXCoordinate < 0)
    robbersXCoordinate = (Math.abs(robbersXCoordinate) - smallTriangleWidth) * -1;
    else
    robbersXCoordinate = robbersXCoordinate - smallTriangleWidth;

    System.out.printf("(%.2f,",robbersXCoordinate);
    }

    /*
    * Setter, recalculating the robber's new Y coordinate by subtracting the small
    * triangle's length by the big triangle's length.
    * @ param - small triangle's length. sent from the method hypotenuse.
    */
    public void setRobbersYCoordinate(double smallTriangleLength){
    if(robbersYCoordinate < 0)
    robbersYCoordinate = (Math.abs(robbersYCoordinate) - smallTriangleLength) * -1;
    else
    robbersYCoordinate = robbersYCoordinate - smallTriangleLength;

    System.out.printf("%.2f)\n",robbersYCoordinate);
    }

    /*
    * Getter, return's robbersYCoordinate.
    * @ return robbersYCoordinate.
    */
    public double getRobbersY(){
    return robbersYCoordinate;
    }

    /*
    * Getter, return's robbersXCoordinate.
    * @ return robbersXCoordinate.
    */
    public double getRobbersX(){
    return robbersXCoordinate;
    }

    /*
    * Getter, return's width of big Triangle.
    * @ return bigTriangleWidth
    */
    public double getWidth(){
    return bigTriangleWidth;
    }

    /*
    * Getter, return's Length of big Triangle.
    * @ return bigTriangleLength
    */
    public double getLength(){
    return bigTriangleLength;
    }

    /*
    * Getter, return's the angle.
    * @ return bigTriangleAngle
    */
    public double getTheta(){
    return bigTriangleAngle;
    }
    }
    98 changes: 98 additions & 0 deletions DistanceMain.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,98 @@
    import java.util.Scanner;
    public class DistanceMain {
    public static void main(String[] args){
    Scanner input = new Scanner(System.in);
    boolean XIsSmaller = false;
    boolean YIsSmaller = false;

    System.out.println("Enters in the doors's X coordinate followed " +
    "by his Y coordinate");
    double doorXCoordinate = input.nextDouble();
    double doorYCoordinate = input.nextDouble();

    System.out.println("Enters in the robber's X coordinate followed " +
    "by his Y coordinate");
    double robberXCoordinate = input.nextDouble();
    double robberYCoordinate = input.nextDouble();

    if(robberXCoordinate < doorXCoordinate){
    XIsSmaller = true;
    //System.out.println("true");
    }

    if(robberYCoordinate < doorXCoordinate){
    YIsSmaller = true;
    //System.out.println("true");
    }

    Distance bigTriangle = new Distance(robberXCoordinate, robberYCoordinate);

    /* bigTriangle.setWidth(doorXCoordinate, robberXCoordinate);
    bigTriangle.setLength(doorYCoordinate, robberYCoordinate);
    bigTriangle.setTheta();
    System.out.printf("The Length is %f, the width is %f," +
    " the angle is %f\n", bigTriangle.getLength(),
    bigTriangle.getWidth(),bigTriangle.getTheta());
    */
    System.out.println("Enter in the robber's speed");
    double speed = input.nextDouble();

    if(XIsSmaller && YIsSmaller){
    //System.out.println("true");
    while(bigTriangle.getRobbersX() < doorXCoordinate && bigTriangle.getRobbersY() < doorYCoordinate){
    bigTriangle.setWidth(doorXCoordinate);
    bigTriangle.setLength(doorYCoordinate);
    bigTriangle.setTheta();

    System.out.printf("The Width is %f, the Length is %f," +
    " the angle is %f\n", bigTriangle.getWidth(),
    bigTriangle.getLength(),bigTriangle.getTheta());
    bigTriangle.hypotenuse(speed);
    }
    }
    else if(!XIsSmaller && !YIsSmaller){
    //System.out.println("true");
    while(bigTriangle.getRobbersX() > doorXCoordinate && bigTriangle.getRobbersY() > doorYCoordinate){
    bigTriangle.setWidth(doorXCoordinate);
    bigTriangle.setLength(doorYCoordinate);
    bigTriangle.setTheta();

    System.out.printf("The Width is %f, the Length is %f," +
    " the angle is %f\n", bigTriangle.getWidth(),
    bigTriangle.getLength(),bigTriangle.getTheta());
    bigTriangle.hypotenuse(speed);
    }
    }

    else if(!XIsSmaller && YIsSmaller){
    //System.out.println("true");
    while(bigTriangle.getRobbersX() > doorXCoordinate && bigTriangle.getRobbersY() < doorYCoordinate){
    bigTriangle.setWidth(doorXCoordinate);
    bigTriangle.setLength(doorYCoordinate);
    bigTriangle.setTheta();

    System.out.printf("The Width is %f, the Length is %f," +
    " the angle is %f\n", bigTriangle.getWidth(),
    bigTriangle.getLength(),bigTriangle.getTheta());

    bigTriangle.hypotenuse(speed);
    }
    }

    else if(XIsSmaller && !YIsSmaller){
    //System.out.println("true");
    while(bigTriangle.getRobbersX() < doorXCoordinate && bigTriangle.getRobbersY() > doorYCoordinate){
    bigTriangle.setWidth(doorXCoordinate);
    bigTriangle.setLength(doorYCoordinate);
    bigTriangle.setTheta();

    System.out.printf("The Width is %f, the Length is %f," +
    " the angle is %f\n", bigTriangle.getWidth(),
    bigTriangle.getLength(),bigTriangle.getTheta());

    bigTriangle.hypotenuse(speed);
    }
    }
    }
    }