Skip to content

Instantly share code, notes, and snippets.

@asterwolf
Last active February 8, 2016 01:38
Show Gist options
  • Save asterwolf/0c3850df6d15e947dae1 to your computer and use it in GitHub Desktop.
Save asterwolf/0c3850df6d15e947dae1 to your computer and use it in GitHub Desktop.
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;
}
}
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);
}
}
}
}
@pathawks
Copy link

pathawks commented Feb 8, 2016

  • Do you mean robberYCoordinate < doorYCoordinate ?
  • It seems that each of these branches do the same thing. Is this intended?
  • The Distance object is keeping track of so much more than Distance, I feel like there must be a better name. Distance sounds like just one number. This object is keeping track of a robber's 2D position, as well as a door's 2D position.
  • Is there a reason everything in the Distance class starts with an indentation of 8 spaces, while everything else is indented 4 spaces?
  • Tabs and spaces

@asterwolf
Copy link
Author

  • Fixed the doorYCoordinate. Thanks!
  • They do the same things, the difference, is that if the coordinates of the robber is less than that of the door, then it will know when to stop when the coordinates of the robber is greater than or equal to the coordinates of the door. If the coordinates of the robber is greater than that of the door, than it will know when to stop when the coordinates of the robber is less than that of the door. 4 possible combination (-,-), (+,+), (-,+), (+,1). That is the section that I mentioned was funky and I'm sure I can figure out a better one later. :)
  • I'm not very creative with the naming processes. I wanted to just jump in and that is the reason for the object name.
  • No reason for it, I just happen to do that and didn't pay attention to it when I went back to fix indentation.
  • Thanks, I missed those. :~)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment