Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- bool PointIsInGeometry(PointCollection points, MapPoint point)
- {
- int i;
- int j = points.Count - 1;
- bool output = false;
- for (i = 0; i < points.Count; i++)
- {
- if (points[i].X < point.X && points[j].X >= point.X || points[j].X < point.X && points[i].X >= point.X)
- {
- if (points[i].Y + (point.X - points[i].X) / (points[j].X - points[i].X) * (points[j].Y - points[i].Y) < point.Y)
- {
- output = !output;
- }
- }
- j = i;
- }
- return output;
- }
- Area: Return the area of a polygon (0 for an empty polygon).
- BoundingBox: Return the bounding box (extent) of a polygon.
- Width: Return the width of a rectangle.
- Height: Return the height of a rectangle.
- Left: Split a rectangle into two halves and return the left half.
- Right: ... returning the right half.
- Top: ... returning the top half.
- Bottom: ... returning the bottom half.
- Clip: Clip a polygon to a rectangle.
- RandomPoint: Return a random point in a rectangle.
- Search: Search a sorted list for a target value. Return the index
- of the last element less than the target.
- In: Test whether a point is inside a polygon.
- Procedure SimpleRandomSample(P:Polygon, N:Integer) {
- U = Sorted list of N independent uniform values between 0 and 1
- Return SRS(P, BoundingBox(P), U)
- }
- Procedure SRS(P:Polygon, B:Rectangle, U:List) {
- N = Length(U)
- If (N == 0) {Return empty list}
- aP = Area(P)
- If (aP <= 0) {
- Error("Cannot sample degenerate polygons.")
- Return empty list
- }
- t = 2
- If (aP*t < Area(B)) {
- # Cut P into pieces
- If (Width(B) > Height(B)) {
- B1 = Left(B); B2 = Right(B)
- } Else {
- B1 = Bottom(B); B2 = Top(B)
- }
- P1 = Clip(P, B1); P2 = Clip(P, B2)
- K = Search(U, Area(P1) / aP)
- V = Concatenate( SRS(P1, B1, U[1::K]), SRS(P2, B2, U[K+1::N]) )
- } Else {
- # Sample P
- V = empty list
- maxIter = t*N + 5*Sqrt(t*N)
- While(Length(V) < N and maxIter > 0) {
- Decrement maxIter
- Q = RandomPoint(B)
- If (Q In P) {Append Q to V}
- }
- If (Length(V) < N) {
- Error("Too many iterations.")
- }
- }
- Return V
- }
- import random
- from shapely import Polygon, Point
- def get_random_point_in_polygon(poly):
- (minx, miny, maxx, maxy) = poly.bounds
- while True:
- p = Point(random.uniform(minx, maxx), random.uniform(miny, maxy))
- if poly.contains(p):
- return p
- p = Polygon([(0, 0), (0, 2), (1, 1), (2, 2), (2, 0), (1, 1), (0, 0)])
- point_in_poly = get_random_point_in_polygon(mypoly)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement