Generate random points in polygons - random

I have a high number of polygons in a shape file and after importing it into r I would now like to create 10 random points inside each polygon. Can anyone please recommend a package which will do this? I'm fairly new and slightly overwhelmed to/by r. Thanks in advance.

You need to implement a point-in-polygon test. This is done by drawing an horizontal line through the point and finding all polygon sides that cross it. If the number of line/side intersections located on the right of the point is odd, then the point is inside the polygon.
You can use this test by drawing random points in the bounding box of the polygon and stopping when 10 of them pass the insideness test.

Related

Check if a Circle fits through a maze in non-quantized 2d space

I'm a high school student and I went to a coding competition recently and got this problem that I had no idea how to solve:
Given a maze enclosed in a 100x100 area, determine if a circle with a given radius could fit through the maze given the locations of all the walls. Walls will be defined as lines connecting two points within the space, and you will be given start and destination points for the circle. The circle must start with its center at the start point and touch the destination point for it to successfully fit through the maze. There will be a maximum of 20 walls. The radius of the circle and the locations of the walls can be "arbitrarily" precise. ("arbitrarily" for this case just means within far limits - let's say, up to a max of 10 digits after the decimal).
Here is an example. If this were the input:
Radius = 2.8
Start = (5,5), Destination = (95,95)
Walls (a wall connects each pair of points):
(20,0) to (27.5,22.6)
(27.5,22.6) to (55.1,35.5)
(55.1,35.5) to (80.3,80,4)
(80.3,80,4) to (95,63.9)
(1.7,25.8) to (17.5,53.2)
(17.5,53.2) to (56.4,69)
(56.4,69) to (67.9,90.6)
(85.6,98.94512) to (87.3,92.5)
then this (made on desmos) is what the maze would look like (the blue circle is just to show how big the circle is):
I would know how to solve the problem if it were in a quantized grid, but the exact locations of the walls and the radius of the circle can be arbitrarily precise. I've thought about using the "right-hand rule" to find a path, but I don't know how to implement that in a non-quantized space (nor am I very familiar with the method).
How would I go about solving this? Can someone point me to an algorithm, a link, some pseudocode, or just an intuition that could help me get an understanding of how I might solve this? Any help is appreciated. Thanks!
Thickennig/moving walls by r in each side like in the other answer (+1 btw) sounds simple but to code it its not trivial to do. For more info see
draw outline for some connected lines
The direction of normal in 2D is easy if dx,dy is line direction then (-dy,dx) and (dy,-dx) are normals to it ...
However I would encourage to do slower but safe and easier approach by computing the closest distance to wall for each vertex of the maze and close paths that are closer than 2r ...
Something like this:
So:
for each vertex
check all lines that does not belong to vertex path
compute perpendicular and min distance d to line and its vertexes
use the smallest d the distance can be computed easily see:
helix central axis angle
just look there for Perpendicular distance of any point P to AB so:
d = min
(
perpendicular_distance(line,vertex),
|line_vertex1-vertex|,
|line_vertex2-vertex|
)
if d<2r close the path. For example by adding a line that joins the wall its too close to tested vertex
ideally by joining tested vertex and the found closest point. Do not forget in such case to split the opposing wall line to two by the closest point so your graph algorithms still work...
As you can see this is O(n^2) instead of O(n) like in the other answer but its foul proof... enlarging polygons is not and in matters of fact its one of the hardest things in 2D geometry to code (IIRC even still open problem)...
It's quite a task and not easy to code, but here's a way that works:
Let r be the radius of the circle. That means that the center of the circle can't get within r of any obstacle.
Move the walls of your maze area in by r on each side.
Replace every wall endpoint with a circle of radius r.
Replace every wall with a rectangle of width 2r.
Now you don't need to worry about the circle -- only its center point, which must remain within the new boundaries and outside of any of the circles or rectangles you made from the walls.
Now, there is a path from the start to the end if they are in the same enclosed area. To find that out...
Cut the scene horizontally at every intersection and vertical maximum or minimum to create strips, with each strip divided into regions by a line or circular arc that passes all the way through it. A region does not connect direction to the regions to its left and right, but may connect to zero or more regions in the strips above and below it. The connections between regions form a graph.
Starting at the region containing the start point, run a BFS or DFS on this graph to see if you can reach the region containing the end point.

Construct lines from multiple 2D points and measure the distance between those lines

Multiple points on a 2D plane are given. They represent a window frame of mostly rectangular form with some possible variations. The points which are part of each side are not guaranteed to form a perfect line. Each side of the window should be measured.
A rotating electronic device attached to a window measures the distance in all directions providing a 360 degree measurements. By using the rotation angle and the distance, a set of points are plotted on a 2D coordinate system. So far so good.
Now comes the harder part. The measured window frame could have some variations. The points should be converted to straight lines and the length of each line should be measured.
I imagine that the following steps are required:
Group the different points into straights lines. This means approximating each line “between” the points that form it.
Drawing those lines, getting rid of the separate points used to construct the lines.
Find the points where each two lines intersect.
Measure the distance between those points. However not all distances between all points are interesting. For example diagonals within a frame are irrelevant.
Any Java libraries dealing with geometry that could solve the problem are acceptable. I will write the solution in Kotlin/Java, but any algorithmic insights or code examples and ideas in any other languages or pseudo code are welcome.
Thank you in advance!
New Image
I would solve this in 2 stages:
Data cleaning: round the location (X, Y) of each point to its nearest multiple of N (vary N for varying degrees of precision)
Apply the gift-wrapping algorithm (also known as Jarvis March)
You now have only those points that are not co-linear, and the lines between them, and the order in which they need to be traversed to form the perimeter.
Iterate over the points in order, take point Px and P(x+1), and calculate the distance between them.

Algorithm to connect perimeter points with rectangle polygon

I am developing a web app and I need to find a way to draw an outline rectangular polygon connecting the given points to form a perimeter on a coordinate system.
I found this ordering shuffled points that can be joined to form a polygon (in python)
to be quite relevant to my problem, but it has the problem that, if any points surpasses the center of the polygon the algorithm does not work...
To make it more clear what I want I am attaching two pictures to show what I want to achieve with given points:
Correct way to join points
Wrong way to connect points
Is there an algorithm that given point coordinates creates the rectangular perimeter as I want it? Thanks

Voronoi plot, line crossing

I have the following problem. Initially I create 10 points, in a 2-D space, randomly distributed and then I use the Voronoi function to creat polygons. But I want my Voronoi polyhedra to obey a gaussian-normal distribution. So the area of each polygon should obey this rule. But I cannot do this since my polyhedra are not convex but have vertices and corners outside the plot, extending to infinity.
So what I want to do is to assign the crossing of the lines of the corresponding polygons with the borders of the plot. but how can I get the line intersections?
I know the point inside the plot , but I donnot know anything about the point outside the plot..
Thank you very much for your help!
Panos
You may better specify the terms of your problem
Why your vertices got to infinity? Are random points choosen all over 2d plan or inside a specified area?
Why you don't know nothing about other points?
You should probably calculate the intersections automatically. You would first need to detect the two lines which you would need to calculate. from there, you need will need two points on each line. (x1a, y1a), (x2a, y2a) and (x1b, y1b), (x2b, y2b)
from here, use the point-slope equation to find where these lines intersect:
if y-y1a=m(x-x1a) and m=(y2a-y1a)/(x2a-x1a)

Random but Regular Polygon Generator

I'm looking for a way to generate a set of random sided, but regular, polygons, inside a given rectangle or sector of a circle. To better explain, my given 2d space should have a random arrangement of regular polygons with various numbers of sides, so, e.g, if two hexagons are separated by a rectangle equal in length to their sides, the whole space cannot be filled with only more hexagons, some triangles may be required, etc.
I'm looking for one segment of a sort of kaleidoscope effect.
You are looking for tiling algorithms. See, for example, Penrose Tiler and Tilings and Tesselations pages for a start.
Another approach, I can think of:
First decide on how many objects you want. Say 'N'
Randomly Select 3 Points in your 2D Space.
Make use of 3 points to get a virtual triangle.
Now Select another point such a way that the point is outside the virtual triangle. Now form another virtual triangle by joining this point to 2 points from previous virtual triangle and then recurisevely form "N" virtual trianlges. Incase virtual triangles crossed, then you ignore the bigger trianlge and take triangles which had formed because of crossing points as new virtualtriangles
Now Generate a INSCRIBED Circle virtually to all virtual triangles which will never be able to intersect another virtual triangle since all virtual triangles are formed by without crossing any of the triangles as expalined above.
Make use of virtual circles to form any number of regular sides by dividing 360 degress to equal slices.
Now You can draw random regular polygons
I'm not sure I understand the requirements, but you can generate random regular polygons by randomly generating the following numbers:
radius (0 to whatever)
x and y of center (must be within radius of edges)
number of points (3 to whatever)
rotation (0 to 360)
To prevent overlapping, you can test each new polygon against each existing polygon and reject the new one if the distance between the centers is less than the sum of the radii.
Drawing the polygons is then a simple trig exercise.
See: "Heuristics for Generation of Random Polygons"
Generate N random points on a plane and extract the convex hull of the object (that is, if all polygons should be convex).
You can trivially reject before generating the convex hull if one of the points is inside another polygon. If it's not you still need to test the generated polygon against other polygons near it. (If you need to do this often, a spatial data structure is probably a something to look into).

Resources