Leaflet Polygon with 6 or more latlon points - leaflet

Helo! I have 8 points and I need to draw a polygon, but if I pass the points randomly, the lines cross as shown in the image below. I needed to make a closed polygon without crossing lines.
var latlngs
var polygon = L.polygon(latlngs, {color: 'red'}).addTo(map);

Leaflet has no idea what the "intended" order of points is. While you may have in your mind that you want a relatively convex shape crafted from x number of points, you need to be the one to order them properly so that the shape you intend is drawn as you intend it.
If you are facing a problem where you have an unsorted array of points, and you need to draw the most convex shape around those points that you can, this is a classic convex hull problem. TurfJS has a convex function you can use for that. There's also a concave hull function there if that's what you need.

Related

Turf bbox taking in account rotation

I have a set of polygons and I want to draw the smallest possible polygon around them (it has to be a rectangular shape, all inner angles 90 degrees).
To achieve this I used the bboxPolygon function from turf.js and it works okay, but it does not take in account rotation of the polygons.
For example take a look at the image bellow, the purple line is the bboxPolygon result but there is too much space at the sides, what I ultimately try to achieve is the corners should be near the topmost points of polygon 101 and bottom most of 202. Is there some other method to use to achieve this? I tried envelope but I still get the same results...
By definition, a bbox is always aligned north/south/east/west.
In this particular case, a convex hull is probably what you want. Turf supports that with the convex method.

Extrapolation delaunay triangulation

Shown Figure (1) is a typical Delaunay triangulation (blue) and it has a boundary line (black rectangle).
Each vertex in the Delaunay triangulation has a height value. So I can calculate the height inside convex hull. I am figuring out a method to calculate the height up to the boundary line (some sort of extrapolation).
There are two things associated with this task
Triangulate up to the boundary point
Figuring out the height at newly created triangle vertices
Anybody come across this issue?
Figure 1:
I'd project the convex hull points of the triangulation to the visible box segments and then insert the 4 box corners and the projected points into the triangulation.
There is no unique correct way to assign heights to the new points. One easy and stable method would be to assign to each new point the height of the closest visible convex hull vertex. Be careful with extrapolation: Triangles of the convex hull tend to have unstable slopes, see the large triangles in front of the below terrain image. Their projection to the xy plane has almost 0 area but due to the height difference they are large and almost 90 degrees to the xy plane.
I've had some luck with the following approach:
Find the segment on the convex hull that is closet to the extrapolation point
If I can drop a perpendicular onto the segment, interpolate between the two vertices of the segment.
If I can not construct the perpendicular, just use the closest vertex
This approach results in a continuous surface, but does not provide 1st derivative continuity.
You can find some code that might be helpful at TriangularFacetInterpolator.java. Look for the interpolateWithExteriorSupport method.

Convex Hull with a predefined number of vertices

I am working on image segmentation and I thought the convex hull can provide me with a simple solution to my problem. Currently I have polygons with for sides (see image below). Due to image processing issues, the shape does not have clean straight sides and hence when I use the standard convex hull (in Matlab) I may get more than the four main corners to define it.
My goal is to force the convex hull algorithm to find the best 4 vertices that will enclose my polygons (i.e. 4 best enclosing vertices per polygon). Is this possible? An example code will be appreciated.
Thanks
The problem of the minimum area bounding polygon is briefly mentioned in "Geometric applications of a matrix-searching algorithm" (see Applications section). It is not simple and is probably not the way for you.
For an easier (but approximate) answer to your question, you can consider the four cardinal directions and find the farthest points in these, which define a quadrilateral. (Also consider the four intermediate directions, which are more appropriate for an axis-aligned rectangle.)
If you insist having an enclosing quadrilateral, you can translate the four edges to the farthest points in the respective perpendicular directions, and find the pairwise intersections.
If you insist having a rectangle, compute the convex hull and find the minimum area or minimum perimeter bounding rectangle by the Rotating Calipers method. https://geidav.wordpress.com/tag/rotating-calipers/

How to find ALL points that lie on the outline of a polygon's convex hull (Matlab)

sorry if this question might seem simple but I couldn't figure it out.
Imagine two arbitrary Rectangles that are randomly overlaped so that their outlines intersect at only two locations. Now you cut the area of overlap out of Rectangle 1.
This "bitten Rectangle 1" has now the following points (vertices): (1) All points of Rectangle 1 that lie outside of Rectangle 2, (2) All points of Rectangle 2 that lie inside Rectangle 1 and (3) the two intersection points.
The problem know is the following: How can I get the order of the new points so that the functions plot(...) or fill(...) would draw the right "Bitten Rectangle 1"?
What I did so far:
I determined the convex hull of all the points that lie outside of Rectangle 2 + the intersection points. Then one has to add the new points that lie inside rectangle 1 (due to overlap with Rectangle 2) between the indices of the first and second intersection point also in the right order.
The problem with this convex hull approach is that it only works if the intersection points lie on different lines of Rectangle 1, because then they are part of the convex hull.
If they lie on the same line they are no longer treated as part of the convex hull.
What I'd need is a method to get the order of all possible points that lie on the convex hull and not only the outer most ones.
Hope anybody can help me...
Thank you in advance,
Patrick
In Matlab, there is an amazing function called polybool that let you do any set operation on polygons: http://www.mathworks.com/help/toolbox/map/ref/polybool.html
All you have to do is to define four arrays describing the rectangles (rect1x, rect1y, rect2x and rect2y) and to call [resultx resulty] = polybool('subtraction', rect1x, rect1y, rect2x, rect2y). The resulting arrays will be describing the "Bitten Rectangle 1".

Fast way to convert array of points into triangle strip?

I have an array of CGPoints (basic struct with two floats: x and y). I want to use OpenGL ES to draw a textured curve using these points. I can do this fine with just two points, but it gets harder when I need to make a line from several points.
Currently I draw a line horizontally, calculate its angle from the points given, and then rotate it. I don't think doing this for all lines in a curve is a good idea. There's probably a faster way.
I'm thinking that I can "enlarge" or "constrict" all the points at once to make a curve with some sort of width.
I'm not positive what you want to accomplish, but consider this:
Based on a ordered list of points, you can draw a polyline using those points. If you want to have a polyline with a 2D texture on it, you can draw a series of quadrilaterals (using two triangles each, of course). You can generate these quadrilaterals using an idea similar to catmul-rom spline generation.
Consider a series of points p[i-1], p[i], p[i+1]. Now, for each i, you can find two points each an epsilon distance away from p[i] along the line perpendicular to the line connecting p[i-1] and p[i+1]. You can determine the two points generated for the endpoints in various ways, like using the perpendicular to the line from p[0] to p[1].
I'm not sure if this will be faster than your method, but you should be caching the results. If you are planning on doing this every frame, another type of solution to your problem may be needed.