How can I get the intersection of a line and a polygon? - java

So I have a list of points here.
private List<Point> points;
and the points class just has x,y,z values but only x,y are used in this case. So I store all the polygon points in there
and then I have this method here.
public Point getIntersection(Crossable aLine) {
}
And aLine is just two points so it has 4 values total for x1,x2,y1,y2 etc.
I want to get the intersection of the Line and the polygon, I have no understanding of how to get the intersection since there are a lot of variables involved and special cases. Any ideas?

Consider every two adjacent points of the polygon as a line and check to see if it intersects with your line. You need a loop to go through them all.

Related

Sphere-Sphere Intersection, choosing right theta

I am working on a C++ problem where I'm trying to make a utility function that takes as input two line segments starting points in 3d space [(x,y,z) and radius r]. If the segments can be oriented such that they end at the same point, the function should return true and print out that point. If there are multiple orientations that would produce a common endpoint, the function should choose the one that is furthest in the direction indicated by hint_direction.
The function receives these values:
bool func(
point3d position_0, // origin of first line segment.
float length_0, // length of first line segment.
point3d position_1, // origin of second line segment.
float length_1, // length of second line segment.
vector3d hint_direction, // in the event there are multiple solutions, return the one furthest in this direction.
point3d *out_common_end_position) // if result is true, point where both line segments can be oriented to end. otherwise uninitialized.
I have been following some guides online which lay out how to do this such as this: https://gamedev.stackexchange.com/questions/75756/sphere-sphere-intersection-and-circle-sphere-intersection.
I was able to successfully get all the intersecting points, but I cannot figure out how to get a point that is furthest in the hint direction.
I thought I might be able to use the intersection point between circle and hint_direction and get a angle of that point, but I cannot figure out how to do so.
As Spektre correctly pointed out I missed the 3D portion of your question, so the 4 options are the following:
no intersection (or one sphere completely lies within the other)
a single point (spheres touch from inside or outside)
a normal intersection forming a circle
both spheres overlap completely, i.e. they have the same origin and the same radius
Since the normal intersection would form a circle you'd want to project the direction hint onto that circle and calculate the intersection between the circle and the projected vector to get the farthest intersection point.

How to check if a location is near a line between to points and coordinates on that line?

I have a factory with the three plants A,B and C. Each of that plant contains machines from 1 to 9 in a row.
For each plant I know the location 1 and 9. None of the locations between them.
Now the user can walk through the factory and save a location for each machine.
Question: How can I achieve that there is a line between the two edges 1 and 9? I would like to do that in Java Vectors.
So the user could pass his current location and the program saves the nearest location that line.
The aim is to get to know all the locations of the machines.
Dont really get the question clear. But think this might be of help
Create a Point Object with x cordinate is for plants and y cordinates for machines
Point location = new Point(1,1);//Means it is in Plant 1 on Machine 1
create a method to get where user is and store it
void setLocation(int plant, int location){
location = new Point(plant, location);
}
Point getLocation(){
return location;
}
You could make a 2d array of points, with the plants being the rows and machines being the columns, Point[plants][machines]. And then go through the array and give each Point a definite location.Then create a method called Search. In this search method, two parameters would be passed in, an x and y coordinate (the coordinates of your user).The search method would then iterate through the 2d array of points, and do the math to find the closest point to those passed in coordinates. Should be as simple as using the distance formula. Then you can just change the coordinates of the user to match that of the closest point found.

Polygon shape margin in Java

I have a simple array of geo points. This array represents a non crossing polygon. I have an algorithm that can calculate if a geo point is inside the polygon or not.
This is how my class looks like:
public class Polygon
{
List<LatLng> vertexes;
}
However sometimes in order to work the report I need to have a tolerance so I need to make a margin around the original polygon. That means the new polygon must have the same shape but to be bigger by r (value).
Does anyone have an idea how this can be implemented in Java having in mind that my original polygon class has a list of LatLng points.

Join two lines in one line (in Java)

I have an array of Lines and I am using it to draw vectors in my map.
I want to replace two superposed Lines (or have short distance between them) with one Line. Can you give an algorithm to do that ?
Here is a picture that helps you to understand this problem:
The Input Lines :
After executing the algorithm, I would like to to get the output represented in the following picture:
PS: A Line is an ArrayList of points.
Merge any pair of vertices that are within some fixed distance of each other (set their position to be equal).
Find the nearest point on each line to each vertex. If it's close enough, then split the line on that point, and merge the points.
Remove duplicate lines that have the exact same start and end points.
For example, if you have a line defined by points A and B, and another line with point C (diagram on left above). The point D can be found using a shortest distance from point to line function. If D is too far away from C then ignore it, otherwise split the line AB into two lines AD and DB, and move all points in position C to position D, to get the diagram on the right.
This question is similar to the question "Do two ranges intersect, and if not then what is the distance between them?" The answer depends slightly on whether you already know which range is smallest already, and whether the points in the ranges are ordered correctly (that is, whether the lines have the same direction).
So a preliminary algo approach will be like this :
if (a.start < b.start) {
first = a;
second = b;
} else {
first = b;
second = a;
}
Let us find the distance now :
distance = max(0, second.start - first.end);
Now you have must have a range of values for shortest distance , depending on which you are going to super impose the lines into 1 . Lets say you have kept them in an array called :
arrayRange[];
Now if ,
for(int 1=0;i<arrayRange.length;i++)
{
if(distance is one of the elements of the arrayRange)
then,
callFunctionSuperImposeLines(distance,a,intersectionPoint,a.end,b,b.end);
}

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())

Categories

Resources