vanetsim.map
Class MapHelper

java.lang.Object
  extended by vanetsim.map.MapHelper

public final class MapHelper
extends java.lang.Object

This class holds some geometric functions needed for various calculations on the map. It's just a helper class to make the map class smaller and as this class has no variables, all functions are declared static.


Constructor Summary
MapHelper()
           
 
Method Summary
static void calculateBridges(Street bridgeStreet, Street otherStreet)
          Checks for intersections on the maps and creates bridges if necessary (only for display purposes).
static double calculateDistancePointToStreet(Street street, int x, int y, boolean sqrt, int[] result)
          Calculate the distance between a point and a street.
static void calculateResizedLine(int[] startPoint, int[] endPoint, double correction, boolean correctStart, boolean correctEnd)
          Recalculates start and end points of a line so that the line is shorter or longer than before.
static int findLineCircleIntersections(int circleX, int circleY, int circleRadius, int x1, int y1, int x2, int y2, boolean onlyOnSegment, int[] result)
          Finds an intersection between a line and a circle.
static Node findNearestNode(int x, int y, int maxDistance, long[] distance)
          Returns the nearest node to a given point.
static boolean findNearestPointOnStreet(Street street, int x, int y, int[] result)
          Calculates the point ON a street which is nearest to a given point (for snapping or such things).
static Street findNearestStreet(int x, int y, int maxDistance, double[] distance, int[] nearestPoint)
          Returns the nearest street to a given point.
static Vehicle findNearestVehicle(int x, int y, int maxDistance, long[] distance)
          Returns the nearest vehicle to a given point.
static boolean findSegmentIntersection(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4, double[] result)
          Finds an intersection between two segments.
static boolean getXYParallelRight(int x1, int y1, int x2, int y2, int distance, double[] result)
          Gets the x and y coordinate difference of a parallel line on the right side (seen from first point to second point).
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

MapHelper

public MapHelper()
Method Detail

calculateDistancePointToStreet

public static double calculateDistancePointToStreet(Street street,
                                                    int x,
                                                    int y,
                                                    boolean sqrt,
                                                    int[] result)
Calculate the distance between a point and a street.

Parameters:
street - the street given
x - x coordinate of the point
y - y coordinate of the point
sqrt - if set to true, the correct distance is returned; if set to false the square of the distance is returned (little bit faster as it saves a call to Math.sqrt(); however, use only if you can handle this!)
result - an array holding the point on the street so that it can be returned. result[0] holds the x coordinate, result[1] the y coordinate. Make sure the array has the correct size (2 elements)!
Returns:
the distance as a double. If nothing was found, Double.MAX_VALUE is returned.

findNearestPointOnStreet

public static boolean findNearestPointOnStreet(Street street,
                                               int x,
                                               int y,
                                               int[] result)
Calculates the point ON a street which is nearest to a given point (for snapping or such things). This code was inspired by Paul Bourke's homepage, especially the Delphi sourcecode (link last visited on 15.08.2008). See there for the mathematical background of this calculation!

Parameters:
street - the street
x - the x coordinate of the point
y - the y coordinate of the point
result - an array for the result. result[0] holds the x coordinate, result[1] the y coordinate. Make sure the array has the correct size (2 elements), otherwise you will not get a result!
Returns:
true if calculation was successful, else false

findNearestStreet

public static Street findNearestStreet(int x,
                                       int y,
                                       int maxDistance,
                                       double[] distance,
                                       int[] nearestPoint)
Returns the nearest street to a given point. First all regions are calculated which are within maxDistance. Then, ALL streets in these regions are checked if they are within this maxDistance and the best one is returned (if any exists).

Parameters:
x - the x coordinate of the given point
y - the x coordinate of the given point
maxDistance - the maximum distance; use Integer.MAX_VALUE if you just want to get any nearest street but note that this costs a lot of performance because ALL regions and ALL streets are checked!
distance - an array used to return the distance between the nearest point and the point given. This should be a double[1] array!
nearestPoint - an array used to return the x-coordinate (nearestpoint[0]) and y-coordinate (nearestpoint[1]) on the street.
Returns:
the nearest street or null if none was found or an error occured

findNearestVehicle

public static Vehicle findNearestVehicle(int x,
                                         int y,
                                         int maxDistance,
                                         long[] distance)
Returns the nearest vehicle to a given point. First all regions are calculated which are within maxDistance. Then, ALL vehicles in these regions are checked if they are within this maxDistance and the best one is returned (if any exists). Note that only vehicles which are currently active will be returned!

Parameters:
x - the x coordinate of the given point
y - the x coordinate of the given point
maxDistance - the maximum distance; use Integer.MAX_VALUE if you just want to get any nearest vehicle but note that this costs a lot of performance because ALL regions and ALL vehicles are checked!
distance - an array used to return the distance between the nearest point and the point given. This should be a long[1] array!
Returns:
the nearest street or null if none was found or an error occured

findNearestNode

public static Node findNearestNode(int x,
                                   int y,
                                   int maxDistance,
                                   long[] distance)
Returns the nearest node to a given point. First all regions are calculated which are within maxDistance. Then, ALL nodes in these regions are checked if they are within this maxDistance and the best one is returned (if any exists).

Parameters:
x - the x coordinate of the given point
y - the x coordinate of the given point
maxDistance - the maximum distance; use Integer.MAX_VALUE if you just want to get any nearest vehicle but note that this costs a lot of performance because ALL regions and ALL nodes are checked!
distance - an array used to return the distance between the nearest point and the point given. This should be a long[1] array!
Returns:
the nearest node or null if none was found or an error occured

findSegmentIntersection

public static boolean findSegmentIntersection(int x1,
                                              int y1,
                                              int x2,
                                              int y2,
                                              int x3,
                                              int y3,
                                              int x4,
                                              int y4,
                                              double[] result)
Finds an intersection between two segments.
Code basically from CODE & FORM blog from Marius Watz (link last visited: 23.09.2008).

Parameters:
x1 - the x coordinate of the first point of the first segment
y1 - the y coordinate of the first point of the first segment
x2 - the x coordinate of the second point of the first segment
y2 - the y coordinate of the second point of the first segment
x3 - the x coordinate of the first point of the second segment
y3 - the y coordinate of the first point of the second segment
x4 - the x coordinate of the second point of the second segment
y4 - the y coordinate of the second point of the second segment
result - an array to return the intersection point. The x coordinate will be in result[0], the y coordinate in result[1].
Returns:
true if lines intersect, false if lines don't intersect or if you didn't supply a valid result array.

findLineCircleIntersections

public static int findLineCircleIntersections(int circleX,
                                              int circleY,
                                              int circleRadius,
                                              int x1,
                                              int y1,
                                              int x2,
                                              int y2,
                                              boolean onlyOnSegment,
                                              int[] result)
Finds an intersection between a line and a circle. Code basically from VB Helper: Find the points where a line intersects a circle in Visual Basic .NET (link last visited: 31.10.2008).

Parameters:
circleX - The x coordinate of the middle of the circle
circleY - The y coordinate of the middle of the circle
circleRadius - The radius of the circle
x1 - The x coordinate of the start point of the line
y1 - The y coordinate of the start point of the line
x2 - The x coordinate of the end point of the line
y2 - The y coordinate of the end point of the line
onlyOnSegment - If true, only intersection points are found which are on the line segment, else intersections on the extension of the line segment are also found!
result - An array to return the result. Should be an int[4]. The x coordinate of the first point will be in result[0], the y coordinate in result[1]. The x coordinate of the second point will be in result[2], the y coordinate in result[3].
Returns:
The amount of intersections found (0-2).

getXYParallelRight

public static boolean getXYParallelRight(int x1,
                                         int y1,
                                         int x2,
                                         int y2,
                                         int distance,
                                         double[] result)
Gets the x and y coordinate difference of a parallel line on the right side (seen from first point to second point).

Parameters:
x1 - the x coordinate of the first point
y1 - the y coordinate of the first point
x2 - the x coordinate of the second point
y2 - the y coordinate of the second point
distance - the distance
result - an array to return the coordinate differences of the parallel. The x coordinate difference will be in result[0], the y coordinate difference in result[1].
Returns:
true if calculation was successful, else false

calculateBridges

public static void calculateBridges(Street bridgeStreet,
                                    Street otherStreet)
Checks for intersections on the maps and creates bridges if necessary (only for display purposes). The bridge is added to the lowerspeedStreet. This function takes quite a long time to process if the map is large.

Parameters:
bridgeStreet - the street which will be above otherStreet if both intersect
otherStreet - the other street

calculateResizedLine

public static void calculateResizedLine(int[] startPoint,
                                        int[] endPoint,
                                        double correction,
                                        boolean correctStart,
                                        boolean correctEnd)
Recalculates start and end points of a line so that the line is shorter or longer than before.

Parameters:
startPoint - the start point. x coordinate is expected in startPoint[0], y in startPoint[1]. Will be used to return the result.
endPoint - the end point. x coordinate is expected in endPoint[0], y in endPoint[1]. Will be used to return the result.
correction - the amount of length correction. Use a positive value to makes the line shorter, a negative to make it longer. If correctStart and correctEnd are both true, the total correction is double of this value, if both are false no correction is done.
correctStart - if the startPoint shall be corrected.
correctEnd - if the endPoint shall be corrected.