Class Distance
public class Distance
extends java.lang.Object
This is just a collection of simple distance-related utilities that don't really belong in any specific place.
Anything related to distance should go here - unless, of course, it pertains only to a specific class. I mean... I doubt that anybody's going to be studying the JavaDocs behind the "util" package on a pathfinding library, but you never know, right?
- Since:
- 0.1.0
- Author:
- Colin Robertson
-
Field Summary
Fields Modifier and Type Field Description static double
INCHES_TO_METERS
The factor by which an inch measurement should be multiplied to get the same measurement in meters.static double
METERS_TO_INCHES
The factor by which an inch measurement should be multiplied to get the same measurement in meters. -
Constructor Summary
Constructors Constructor Description Distance()
-
Method Summary
Modifier and Type Method Description static double
distanceX(Point a, Point b)
Get the distance between two points along a singular axis.static double
distanceY(Point a, Point b)
Get the distance between two points along a singular axis.static double
feetToMeters(double feet)
static double
getDistance(Point a, Point b)
Get the distance between two points.static double
inchesToMeters(double inches)
Convert a distance, given in inches, to a distance, given in meters.static Point
inDirection(HeadingPoint start, double length)
Create a new HeadingPoint a certain distance away from a direction.static Point
inDirection(Point start, double direction, double length)
Create a new HeadingPoint a certain distance away from a direction.static boolean
isNearPoint(Point a, Point b, double tolerance)
Determine whether or not point A is near point B with a given tolerance.static double
metersToFeet(double meters)
static double
metersToInches(double meters)
Convert a distance, given in meters, to a distance, given in inches.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Field Details
-
INCHES_TO_METERS
public static double INCHES_TO_METERSThe factor by which an inch measurement should be multiplied to get the same measurement in meters. -
METERS_TO_INCHES
public static double METERS_TO_INCHESThe factor by which an inch measurement should be multiplied to get the same measurement in meters.
-
-
Constructor Details
-
Distance
public Distance()
-
-
Method Details
-
getDistance
Get the distance between two points.This math is based on the distance formula, which you can Google if you'd like to verify that this is, in fact, real math. The distance formula is based upon the Pythagorean theory - simply put, it accounts for the difference in X and Y to form two "legs" and then calculates the hypotenuse of our theoretical triangle.
I seriously do doubt that there will ever be any issues with the functionality of this method - if there's a math issue, you should look elsewhere.
- Parameters:
a
- the first of the two points.b
- the second of the two points.- Returns:
- the distance between the two points.
-
distanceX
Get the distance between two points along a singular axis. This method does not make use of the distance formula, meaning only a single axis is factored into these calculations.- Parameters:
a
- the initial point.b
- the target point.- Returns:
- the distance between the two points. If the target point is smaller than the initial point, this value will be negative. Otherwise, it will be positive.
-
distanceY
Get the distance between two points along a singular axis. This method does not make use of the distance formula, meaning only a single axis is factored into these calculations.- Parameters:
a
- the initial point.b
- the target point.- Returns:
- the distance between the two points. If the target point is smaller than the initial point, this value will be negative. Otherwise, it will be positive.
-
inDirection
Create a new HeadingPoint a certain distance away from a direction.The returned point can be casted into a heading point if you'd so desire. Otherwise, it can just be left as a regular point.
The math for this class is a bit harder to find, however, a quick Google search for "draw line in direction" should bring up something relevant enough to get you started.
As with the
getDistance(Point, Point)
method, this code has already been verified to work entirely as intended. If you feel that you have a mathematical issue that's preventing your robot from working, you can rule this method out as a source.- Parameters:
start
- the start position and angle. Please note that this angle should and is always be notated in DEGREES, not RADIANS. If you have a radian measure that you'd like to convert to a degrees measure, you can use the Math.toDegrees() method and you should be all good.length
- the distance between the two points. This is known colloquially as the length of the line.- Returns:
- a new point, a set distance and direction away from the origin.
-
inDirection
Create a new HeadingPoint a certain distance away from a direction.The returned point can be casted into a heading point if you'd so desire. Otherwise, it can just be left as a regular point.
The math for this class is a bit harder to find, however, a quick Google search for "draw line in direction" should bring up something relevant enough to get you started.
As with the
getDistance(Point, Point)
method, this code has already been verified to work entirely as intended. If you feel that you have a mathematical issue that's preventing your robot from working, you can rule this method out as a source.This method is an overloaded variant of the other inDirection method contained in this file - just as a PSA.
- Parameters:
start
- the start position, expressed as a point.direction
- the angle at which the line should extend, represented as a measurement in degrees. If you have a radians measurement, you're going to need to convert it to a degrees one before continuing.length
- the distance between the two points. This is known colloquially as the length of the line.- Returns:
- a new point, a set distance and direction away from the origin.
-
isNearPoint
Determine whether or not point A is near point B with a given tolerance.If the tolerance value isn't greater than 0, this will always return false. Make sure the tolerance values you're inputting are, in fact, above zero, as distance is always measured as an absolute.
- Parameters:
a
- the first of the two points.b
- the second of the two points.tolerance
- the maximum allowable distance that's still "near".- Returns:
- whether or not the two points are within the tolerance.
-
inchesToMeters
public static double inchesToMeters(double inches)Convert a distance, given in inches, to a distance, given in meters.- Parameters:
inches
- the distance, measured in inches.- Returns:
- the distance, measured in meters.
-
metersToInches
public static double metersToInches(double meters)Convert a distance, given in meters, to a distance, given in inches.- Parameters:
meters
- the distance, measured in meters.- Returns:
- the distance, measured in inches.
-
feetToMeters
public static double feetToMeters(double feet) -
metersToFeet
public static double metersToFeet(double meters)
-