Guest User

Untitled

a guest
Jul 15th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.69 KB | None | 0 0
  1. class Point
  2. attr_accessor :x, :y
  3. def initialize(x, y)
  4. @x = x
  5. @y = y
  6. end
  7. end
  8.  
  9. # >0 left
  10. # =0 on line
  11. # < 0 right
  12. def direction(line_start_point, line_end_point, point)
  13. (line_end_point.x - line_start_point.x) * (point.y - line_start_point.y) - (point.x - line_start_point.x) * (line_end_point.y - line_start_point.y)
  14. end
  15.  
  16. # Edge Crossing Rules
  17. # 1. an upward edge includes its starting endpoint, and excludes its final endpoint;
  18. # 2. a downward edge excludes its starting endpoint, and includes its final endpoint;
  19. # 3. horizontal edges are excluded
  20. # 4. the edge­ray intersection point must be strictly right of the point P.
  21.  
  22. def cn_poly(point, polygon_points, edge_count)
  23. cross_number_count = 0
  24. (edge_count).times do |i|
  25. if (polygon_points[i].y <= point.y && polygon_points[i + 1].y > point.y) || # upward edge
  26. (polygon_points[i].y > point.y && polygon_points[i + 1].y <= point.y) # downward edge
  27. vt = (point.y - polygon_points[i].y).to_f / (polygon_points[i + 1].y - polygon_points[i].y)
  28. if point.x < polygon_points[i].x + vt * (polygon_points[i + 1].x - polygon_points[i].x)
  29. cross_number_count += 1
  30. end
  31. end
  32. end
  33. puts "crossing number counter: #{cross_number_count}, point in polygon? #{cross_number_count.odd?}"
  34. cross_number_count.odd?
  35. end
  36.  
  37. def wn_pn_poly(point , polygon_points, edge_count)
  38. winding_number_count = 0
  39.  
  40. edge_count.times do |i|
  41. if polygon_points[i].y <= point.y && polygon_points[i + 1].y > point.y && direction(polygon_points[i], polygon_points[i+1], point) > 0
  42. winding_number_count += 1
  43. elsif polygon_points[i].y > point.y && polygon_points[i + 1].y <= point.y && direction(polygon_points[i], polygon_points[i+1], point) < 0
  44. winding_number_count -= 1
  45. end
  46. end
  47. puts "winding number counter: #{winding_number_count}, point in polygon? #{winding_number_count != 0}"
  48. winding_number_count != 0
  49. end
  50.  
  51. polygon1 = [
  52. Point.new(1, 0),
  53. Point.new(2, 1),
  54. Point.new(4, 2),
  55. Point.new(5, 0),
  56. Point.new(4, -2),
  57. Point.new(3, 1),
  58. Point.new(2, -2),
  59. Point.new(1, 0)
  60. ]
  61.  
  62. polygon2 = [
  63. Point.new(2, 0),
  64. Point.new(2, 2),
  65. Point.new(0, 2),
  66. Point.new(0, 0),
  67. Point.new(2, 0)
  68. ]
  69.  
  70. polygon3 = [
  71. Point.new(0,0),
  72. Point.new(7,0),
  73. Point.new(7,4),
  74. Point.new(1,4),
  75. Point.new(1,3),
  76. Point.new(5,3),
  77. Point.new(5,2),
  78. Point.new(4,2),
  79. Point.new(4,5),
  80. Point.new(0,5),
  81. Point.new(0,0),
  82. ]
  83.  
  84. polygon4 = [
  85. Point.new(0,0),
  86. Point.new(7,0),
  87. Point.new(7,4),
  88. Point.new(4,4),
  89. Point.new(4,5),
  90. Point.new(0,5),
  91. Point.new(0,0),
  92. ]
  93.  
  94. test_point1 = Point.new(2, 3.5)
  95. test_point2 = Point.new(4.5, 2.5)
  96.  
  97. wn_pn_poly(test_point1, polygon3, 10)
  98. cn_poly(test_point1, polygon3, 10)
  99.  
  100. # wn_pn_poly(test_point, polygon4, 6)
  101. # cn_poly(test_point, polygon4, 6)
Add Comment
Please, Sign In to add comment