Polygon shape margin in Java - 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.

Related

Polygon Z Ordering

I am currently trying to write a 2D only 3D "renderer" that uses polygons only.
Before rendering, I order the polygons by calculating one Z value as following:
double z = (v1.getZ()+v2.getZ()+v3.getZ());
My polygons consist of each three vectors (X,Y,Z)
Then I sort the polygons so I then can use the painters algorithm approach:
int i = 0;
for (Poly polygon : polyZSort) {
if(polygon.getRealZ()>z) {
break;
}
i++;
}
polyZSort.add(i, polygon);
The rendering then is as simple as:
for (Poly poly : polyZSort) {
OVector v1 = poly.getV1();
v1 = form.doTransform(v1);
OVector v2 = poly.getV2();
v2 = form.doTransform(v2);
OVector v3 = poly.getV3();
v3 = form.doTransform(v3);
g2d.setColor(poly.getColor());
Polygon pl = new Polygon();
pl.addPoint(getX(v1.getX()),getY(v1.getY()));
pl.addPoint(getX(v2.getX()),getY(v2.getY()));
pl.addPoint(getX(v3.getX()),getY(v3.getY()));
g2d.fillPolygon(pl);
// black border line
g2d.setColor(Color.BLACK);
g2d.drawLine(
getX(v1.getX()),
getY(v1.getY()),
getX(v2.getX()),
getY(v2.getY())
);
g2d.drawLine(
getX(v2.getX()),
getY(v2.getY()),
getX(v3.getX()),
getY(v3.getY())
);
g2d.drawLine(
getX(v3.getX()),
getY(v3.getY()),
getX(v1.getX()),
getY(v1.getY())
);
}
This works, even with multiple cubes (but only most of the time):
However in certain positions, the ordering seems to be wrong:
Does anybody know where the problem may be?
Here a video of the "glitch" https://vimeo.com/552355610
You are missing important steps ... This is how it should be done:
input is list of 3D triangles, output is list of 3D triangles
The output list is empty at start
"2D" screen project each processed triangle and leave it with original z values
so its still 3D , but x,y are screen positions and z is "original" z value in camera coordinate system.
check each processed triangle after projection if it intersects any triangle already in output list.
If no intersection occurs then simply add this triangle to output list as is.
if it does intersect you need to re-triangulate all intersected and new triangle so no intersections occur. And add these new triangle(s) to output list (while removing the original intersecting triangles from output list).
Here exampel of 2 overlaping triangles:
as you can see it can create quite a lot of new triangles and usually new triangle intersects more than just one triangle so you need to do this recursively for each triangle of the overlapped parts or create a list of all intersecting triangles first and retriangulate them at once ...
after whole scene is processed Z sort the output list by mid point z value
this step you already got.
render the sorted output list using 2D triangle rendering
this step you already got.
As you can see you need a buffer of size depending on number of triangles in scene and overlaps. Also this method is very complicated (and not very good choice for rookie programmers) and usable only for very simple scenes. For more complex ones is much easier and faster to use Z buffer. However that one require along with the buffer itself also 3D rendering of triangle (so one more interpolation and per pixel condition). As you can see its O(1) instead of the O(n.log(n)) for z sort.

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.

Fastest way to check which rectangle is clicked in a list of rectangles

I have a rectangle Object with x, y, width and height. I have a list of these rectangles which are displayed on a screen. It is guaranteed that none of them overlap. Given a user's click position (x and y coordinates), I want to see which of these rectangles were clicked (since they do not overlap, there is a maximum of one rect that can be clicked).
I can obviously look through all of them and check for each one if the user clicked it but this is very slow because there are many on the screen. I can use some kind of comparison to keep the rectangles sorted when I insert a new one into the list. Is there some way to use something similar to binary search in order to decrease the time it takes to find which rect was clicked?
Note: the rectangles can be any size.
Thanks:)
Edit: To get an idea of what I am making visit koalastothemax.com
It highly depends upon your application and details we're not quite aware of yet for what the best solution would be. BUT, with as little as I know, I'd say you can make a 2D array that points to your rectangles. That 2D array would map directly to the pixels on the screen. So if you make the array 10x20, then the coordinate x divided by screen width times 10 (casted to int) will be the first index and y divided screen height times 20 would be your y index. With your x and y index, you can map directly to the rectangle that it points to. Some indexes might be empty and some might point to more than one rectangle if they're not perfectly laid out, but that seems the easiest way to me without knowing much about the application.
I have tackled a very similar problem in the past when developing a simulation. In my case the coordinates were doubles (so no integer indexing was possible) and there could be hundreds of millions of them that needed to be searched.
My solution was to create an Axis class to represent each axis as a sequence of ranges. The ranges were guaranteed to go from a minimum to a maximum and the class was smart enough to split itself into pieces when new ranges were added. Each range has a single generic object stored. The class used a binary search to find a range quickly.
So roughly the class looks like:
class Axis<T> {
public Axis(double min, double max, Supplier<T> creator);
public Stream<T> add(double from, double to);
public T get(double coord);
}
The add method needs to return a stream because the added range may cover several ranges.
To store rectanges:
Axis<Axis<Rectangle>> rectanges = new Axis<>(0.0, 100.0,
() -> new Axis<>(0.0, 100.0, Rectangle::new));
rectangles.add(x, x + w).forEach(r -> r.add(y, y + h).forEach(Rectangle::setPresent));
And to find a rectangle:
rectangles.get(x).get(y);
Note that there's always an object stored so you need a representation such as Rectangle.NULL for 'not present'. Or you could make it Optional<Rectangle> (though that indirection eats a lot of memory and processing for large numbers of rectangles).
I've just given the high level design here rather than any implementation details so let me know if you want more info on how to make it work. Getting the logic right on the range splits is not trivial. But I can guarantee that it's very fast even with very large numbers of rectangles.
The fastest way I can come up with is definitely not the most memory efficient. This works by exploiting the fact that an amortized hash table has constant lookup time. It will map every point that a rectangle has to that rectangle. This is only really effective if your are using integers. You might be able to get it to work with floats if you use a bit of rounding.
Make sure that the Point class has a hash code and equals function.
public class PointCheck
{
public Map<Point, Rect> pointMap;
public PointCheck()
{
pointMap = new HashMap<>();
}
/**
* Map all points that contain the rectangle
* to the rectangle.
*/
public void addRect(Rect rect)
{
for(int i = rect.x; i < rect.x + rect.width; ++i)
{
for(int j = rect.y; j < rect.y + rect.height; ++i)
{
pointMap.put(new Point(i, j), rect);
}
}
}
/**
* Returns the rectangle clicked, null
* if there is no rectangle.
*/
public Rect checkClick(Point click)
{
return pointMap.get(click);
}
}
Edit:
Just thought I should mention this: All of the rectangles held in the value of the hash map are references to the original rectangle, they are not clones.

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

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.

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