Matching similar routes in Google Maps. Can it be done? - java

I have encountered a slight problem.
Is there a way to match routes in Google Maps so that as long as the 2 routes moves along the same path, they will be matched.
For example, Andrew is going from Point A to Point B, and James is going from Point A1 to Point B1.
Although their destinations are different, but because the route from Point A to Point B passes through Point A1 and B1, Google Maps matches these 2 routes together.
Can this be done? If so, how?

i am not 100 percent sure about but think about this,
Google keeps driving or walking directions(polylines) as encoded strings. there are some js codes to decode these and you will see this string has many lines start and end point according to a pattern to save string length.
like this
"}wjiGtdpcNrAlBJZ"
they will be an exact start coordinate and coordinate differences of other points of a direction.
like these coordinates;
-0.00001, 43.64175
-79.38652, 43.64133
-79.38707000000001, 43.641270000000006
-79.38721000000001, 43.641270000000006
so if you encode two different direction and turn these points to exact coordinates you can compare them by checking start and end coordinates are same. because "-0.00001, 43.64175" part or another ones will be in the other way's polyline too if they intersects.
i think you can know if a to b and c to d intersects on same street or particular distance.

Related

Collision between a moving circle and a stationary circle

So what I'm trying to make is a function where I input the location of a moving circle and its target location, the location of a stationary circle and both of their radiuses, and I want to return the point at which they intersected if they did and the target location if they didn't.
The starting position of both circles, their radiuses and the end position of the moving circle are all known.
In this example I want to in a single frame move circle A from point Start to point End, if A collides with B I want to return the point where they touch closest to Start. In between Start and End there is a vector that can be calculated through End-Start which I'll call V and use in my attempt at solving this.
I will to refer the point Start as S and the position of B as P, the radius of A as Ra and the radius of B as Rb as seen in this image: variables.
So this is how far I've got:
When the two circles are just about touching the distance between them should be their radiuses combined like in this image: radiuses combined.
Therefore Ra+Rb = length of P-C which becomes (Ra+Rb)² = (P.x-C.x)² + (P.y-C.y)² according to Pythagoras (I squared both sides to remove the square root)
C can be described as the point S plus the vector V scaled by some constant t, C = S + tV so for example the point half way between Start and End could be described as S + 0.5V.
So the equation would then become (Ra+Rb)² = (P.x-(S.x+tV.x))² + (P.y-(S.y+tV.y))²
I have not gotten further than that since I cant isolate t which I need to find C
Any help is greatly appreciated! Sorry if I made any mistakes, its my first time posting.
(If anyone has code in Java for this that would be amazing)
You would probably have received a better answer for your question over at math.stackexchange.com, since this really seems to be a question about the maths related to your program.
But anyhow, this problem can be solved in a few steps:
1. Projection of a point onto a line:
Let Q be a projected point on V. Is the distance P-Q larger than the sum of Ra and Rb? If so, there is no collision, else proceed:
2. Pythagoras:
You now know the distance P-Q, and as you noted yourself, the circles will intersect at a distance Ra+Rb - if they collide. So, now if we find the distance Q-C, we can find where C is, since we already know where Q is from the projection onto V.
So, what is the distance Q-C: Sqrt((B-Q)^2 - (B-C)^2)
3. Find C by translating Q by distance Q-C
Now, you just need to make sure that you translate Q in the right direction: toward S.
As for the coding part of your problem, there was never a question asked, so there's nothing to respond to...
[Edit: fixed #3 Translate->Find]

Comparing a set of coordinates (strokes?) for a percentage difference

I have 2 paths A and B.
For example:
A = {(1,1), (2,2), (3,3)}
B = {(2,2), (3,3), (5,5), (6,7)}
Each pair of elements is a line, starting at the element before it.
The beginning element is the start of the stroke, and the ending element is the end of the stroke.
I want to compare A and B for similarity, and in the end get a percentage accuracy.
I have done some research already and found references to Hausdorff Distance and Frechet Distance but I cannot figure out how to get these to do what I want them to do.
Thank you!
(The language I am looking to code this in, is Java if any libraries exist for this problem, that would be greatly appreciated)

Implementing Adaboost for multiple dimensions in Java

I'm working on AdaBoost implementation in Java.
It should have work for "double" coordinates on 2D 3D or 10D.
All I found for Java is for a binary data (0,1) and not for multi-dimensional space.
I'm currently looking for a way to represent the dimensions and to initialize the classifiers for boosting.
I'm looking for suggestions on how to represent the multidimensional space in Java, and how to initialize the classifiers to begin with.
The data is something in between [-15,+15]. And the target values are 1 or 2.
To use a boosted decision tree on spatial data, the typical approach is to try to find a "partition point" on some axis that minimizes the residual information in the two subtrees. To do this, you find some value along some axis (say, the x axis) and then split the data points into two groups - one group of points whose x coordinate is below that split point, and one group of points whose x coordinate is above that split point. That way, you convert the real-valued spatial data into 0/1 data - the 0 values are the ones below the split point, and the 1 values are the ones above the split point. The algorithm is thus identical to AdaBoost, except that when choosing the axis to split on, you also have to consider potential splitting points.
How about using JBoost, I think it's got what you're looking for.
Why don't you use a double[] array for each object? That is the common way of representing feature vectors in Java.

determine whether line is entirely within path2d shape (in java)

I'm looking for the easiest/fastest (computationally) way to determine whether a shape, more specifically a GeneralPath object, contains any given line segment (of type Line2D.Double).
GeneralPath has methods to determine whether a point or a rectangle is contained, but not a line (that I can find). The line can be at a slant, so I can't just model a really thin rectangle. One point in the line will definitely be inside the shape, but I need to check the rest of the line segment. So potentially, I could also check to see if the line intersects any of the boundary edges, but I'm not sure how that would look just given the shape and the line.
Does your GeneralPath contain straight line segments or quadratic or bezier segments? This matters a lot. The algorithm for the straight line is the most straightforward:
Iterate over all points in the path. If two consecutive points are on opposite sides of the line, you have a potential crossing. Then you need to check if the line segment end point is inside or outside the shape relative to the potential crossing, which you get by solving for the intersection of two points (the line and the line formed by the two consecutive points) and seeing if the result is contained in your line segment.
Unfortunately, the curved paths can have two consecutive points with a parenthesis shape between them ")", which your line can pass through while still keeping the iterable points on the same side. If you can get the format with two end points and a single (double) control point(s) that form a bounding triangle (quadrilateral), you can get easy simple solutions (since the curve is guaranteed to fit inside the triangle/quad formed by the three/four points, as long as the line doesn't intersect the triangle/quad, you are good). Unfortunately, this has an ugly part as well--if the line does intersect the triangle/quad, you aren't guaranteed anything and have to check closer. As a double unfortunate, I don't know a technique other than normalizing the coordinate system and solving for the zeroes. And that's something I'd look up in a book that I can't seem to find (or wait until another nice SO poster comes along).
... actually, since the curvature properties of the shapes are invariant under rotation, for the closer inspection part you can just rotate the curve points (whether 3 or 4) to be axis aligned. Then do your skinny rectangle trick. That's probably not the cleanest, but it's the most obvious trick.
Come to think of it, why not do the rotation of all the points in the first place? The whole intersection problem is rotation invariant. That would save a lot of code. Just axis-align the line, apply the transformation to the shape, and do your cheeky rectangle trick.
Unless I am missing something, why can't you check if the the path contains x1,y1 and x2,y2 and AND the two as follows:
generalPath.contains(line.getX1(),line.getY1()) &&
generalPath.contains(line.getX2(),line.getY2())

2D waypoint pathfinding: combinations of WPs to go from curLocation to targetLocation

Please take a moment to understand my situation. If it is not comprehendable, please tell me in a comment.
I have an ArrayList of Waypoints. These waypoints are not in any order. A waypoint has the following properties:
{int type, float z, float y, float x, float rotation}
This applies to a 3 dimensional world, but since my pathfinding should not care about height (and thus treat the world as a 2 dimensional one), the y value is ignored. Rotation is not of importance for this question.
In this 2 dimensional world, the x represents the x axis and the z represents the y axis.
If x increases, the object in the world moves east. If x decreases, the object in the world moves west.
If z increases, the object in the world moves north. If z decreases, the object in the world moves south.
Thus, these "new" waypoints can be simplified to: waypoint = {float x, float y}.
Now, these waypoints represent the X-axis (x) and Y-axis (z) locations of an object. Moreover, there is a current location: curLocation = {float x, float y} and a target location: tarLocation = {float x, float y}.
This is what I want to get:
All combinations of waypoints (aka: paths or routes) that will lead from curLocation to tarLocation under the following strict conditions:
The distance inbetween each waypoint may not be bigger than (float) maxInbetweenDistance. This includes the initial distance from curLocation to the first waypoint and the distance from the last waypoint to tarLocation. If no such combination of waypoints is possible, null should be returned.
When multiple waypoints are found within maxInbetweenDistance from a waypoint that lead towards the target waypoint, the closest waypoint should be chosen (even better would be if an alternative waypoint that is slightly further away would result in a new path with a longer distance that is also returned).
The order of returned waypoint combinations (paths) should be from shortest route (minimum distance) to longest route (maximum distance)
Finally, please consider these points:
This is the only thing I need to do AI/pathfinding wise, which is why I do not wish to use a full blown pathfinding or AI framework. I believe one function should be able to handle the above.
If returning all possible combinations of waypoints causes too much overhead, it'd also be fine if one can specify a maximum amount of combinations (but still ordered from closest to furthest). Eg. the 5 closest paths.
How would I achieve this? Any feedback is appreciated.
I think your solution is to start with Dijkstra's Algorithm to find the shortest path first. You can consider your waypoints to be a connected graph where nodes are connected if they are close enough in the xy plane then apply Dijkstra (there are many example code listings online).
Now you have the shortest path through your graph from start to finish, which will be composed of N edges of the graph.
You would next need to create N new graphs, each just like the first, but with one segment of your shortest route un-connected. Find the shortest routes from start to finish on these modified graphs. Now you have N+1 routes which you can sort by length.
Repeat this until you have found enough paths for your needs, or there are no unranked paths left.
I haven't found a name for this technique, but it is described as a modification to Dijkstra here.
If your waypoints possess connectivity, you should take a look at Dijkstra's shortest path algorithm. The first couple of google hits even lists an implementation in Java. (I can't tell if connectivity is known from the post, but it does contain the "graph-algorithm" tag, so I'll assume so). As the name suggests, this method give you a shortest path between the two nodes.
Your constraints are challenging, as is the need for all possible combinations of paths under those constraints. Again - assuming connectivity exists - your node adjacency matrix can enforce your maxInbetweenDistance rule. Likewise, you can use this matrix in obtaining the "next best" solutions. Once the optimal path is known, you can mark that path (or elements of it) as unavailable, then re-run Dijkstra's algorithm. By repeating this process, you can obtain a set of increasingly sub-optimal paths.
As a matter of convention: in most computational geometry problems, Z is the height, and the horizontal plane is formed by the XY axes.
Well the easiest to implement would probably be creating an ArrayList of paths, which would be in turn an ArrayList of waypoints, that contains ALL possible paths, then using a recursive function to return whether each path is Valid or not given the starting and finishing point values, and the max distance, and if a path is not valid remove it from the list. The next step would be going through each of the paths that is left and ordering them from shortest total distance to shortest. This would be the brute force method of getting what you want, so the least efficient one possible. When I get home tonight I will repost if some one already hasn't with a more efficient method for doing this in java.
Edit: if the brute force method is too much, the list of waypoints will have to be sorted some how, the best way is probably to sort them initially based on distance from the starting point.

Categories

Resources