Advertisement
Guest User

Untitled

a guest
Jul 2nd, 2015
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.63 KB | None | 0 0
  1. bool PointIsInGeometry(PointCollection points, MapPoint point)
  2. {
  3. int i;
  4. int j = points.Count - 1;
  5. bool output = false;
  6.  
  7. for (i = 0; i < points.Count; i++)
  8. {
  9. if (points[i].X < point.X && points[j].X >= point.X || points[j].X < point.X && points[i].X >= point.X)
  10. {
  11. if (points[i].Y + (point.X - points[i].X) / (points[j].X - points[i].X) * (points[j].Y - points[i].Y) < point.Y)
  12. {
  13. output = !output;
  14. }
  15. }
  16. j = i;
  17. }
  18. return output;
  19. }
  20.  
  21. Area: Return the area of a polygon (0 for an empty polygon).
  22. BoundingBox: Return the bounding box (extent) of a polygon.
  23. Width: Return the width of a rectangle.
  24. Height: Return the height of a rectangle.
  25. Left: Split a rectangle into two halves and return the left half.
  26. Right: ... returning the right half.
  27. Top: ... returning the top half.
  28. Bottom: ... returning the bottom half.
  29. Clip: Clip a polygon to a rectangle.
  30. RandomPoint: Return a random point in a rectangle.
  31. Search: Search a sorted list for a target value. Return the index
  32. of the last element less than the target.
  33. In: Test whether a point is inside a polygon.
  34.  
  35. Procedure SimpleRandomSample(P:Polygon, N:Integer) {
  36. U = Sorted list of N independent uniform values between 0 and 1
  37. Return SRS(P, BoundingBox(P), U)
  38. }
  39.  
  40. Procedure SRS(P:Polygon, B:Rectangle, U:List) {
  41. N = Length(U)
  42. If (N == 0) {Return empty list}
  43. aP = Area(P)
  44. If (aP <= 0) {
  45. Error("Cannot sample degenerate polygons.")
  46. Return empty list
  47. }
  48. t = 2
  49. If (aP*t < Area(B)) {
  50. # Cut P into pieces
  51. If (Width(B) > Height(B)) {
  52. B1 = Left(B); B2 = Right(B)
  53. } Else {
  54. B1 = Bottom(B); B2 = Top(B)
  55. }
  56. P1 = Clip(P, B1); P2 = Clip(P, B2)
  57. K = Search(U, Area(P1) / aP)
  58. V = Concatenate( SRS(P1, B1, U[1::K]), SRS(P2, B2, U[K+1::N]) )
  59. } Else {
  60. # Sample P
  61. V = empty list
  62. maxIter = t*N + 5*Sqrt(t*N)
  63. While(Length(V) < N and maxIter > 0) {
  64. Decrement maxIter
  65. Q = RandomPoint(B)
  66. If (Q In P) {Append Q to V}
  67. }
  68. If (Length(V) < N) {
  69. Error("Too many iterations.")
  70. }
  71. }
  72. Return V
  73. }
  74.  
  75. import random
  76. from shapely import Polygon, Point
  77.  
  78. def get_random_point_in_polygon(poly):
  79. (minx, miny, maxx, maxy) = poly.bounds
  80. while True:
  81. p = Point(random.uniform(minx, maxx), random.uniform(miny, maxy))
  82. if poly.contains(p):
  83. return p
  84.  
  85. p = Polygon([(0, 0), (0, 2), (1, 1), (2, 2), (2, 0), (1, 1), (0, 0)])
  86. point_in_poly = get_random_point_in_polygon(mypoly)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement