Module: D_Point

Included in:
Dinraal
Defined in:
app/lib/dinraal/point.rb

Instance Method Summary collapse

Instance Method Details

#point_at_distance_angle(options = {}) ⇒ Hash

Calculates a new point given a starting point, distance, and angle.

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • point (Hash) —

    point in DR hash notation.

  • distance (Float) —

    Distance between the given and generated point.

  • angle (Float) —

    Angle from given point to generated point in degrees.

Returns:

  • (Hash) —

    point in DR hash notation.



10
11
12
13
14
15
16
17
18
19
20
# File 'app/lib/dinraal/point.rb', line 10

def point_at_distance_angle(options = {})
  point = options[:point]
  distance = options[:distance]
  angle = options[:angle]

  new_point = {}

  new_point[:x] = (distance * Math.cos(angle * Math::PI / 180)) + point[:x]
  new_point[:y] = (distance * Math.sin(angle * Math::PI / 180)) + point[:y]
  new_point
end

#point_difference(point1:, point2:) ⇒ Array

Calculates the difference between two points

Parameters:

  • point1: (Array) —

    A point in array notation

  • point2: (Array) —

    A point in array notation

Returns:

  • (Array) —

    An array with the x difference as [0] and the y distance as [1]



28
29
30
# File 'app/lib/dinraal/point.rb', line 28

def point_difference(point1:, point2:)
  [point1.x - point2.x, point1.y - point2.y]
end

#point_distance(point1:, point2:) ⇒ Float

Calculates the distance between two points

Parameters:

  • point1: (Array) —

    A point in array notation

  • point2: (Array) —

    A point in array notation

Returns:

  • (Float)


38
39
40
41
42
# File 'app/lib/dinraal/point.rb', line 38

def point_distance(point1:, point2:)
  dx = point2.x - point1.x
  dy = point2.y - point1.y
  Math.sqrt((dx * dx) + (dy * dy))
end

#point_distance_squared(point1:, point2:) ⇒ Float

Calculates the distance squared between two points

Parameters:

  • point1: (Array) —

    A point in array notation

  • point2: (Array) —

    A point in array notation

Returns:

  • (Float)


50
51
52
53
54
# File 'app/lib/dinraal/point.rb', line 50

def point_distance_squared(point1:, point2:)
  dx = point2.x - point1.x
  dy = point2.y - point1.y
  (dx * dx) + (dy * dy)
end

#point_inside_triangle?(point:, triangle:) ⇒ Boolean

Determines if the given point is in the given triangle.

Parameters:

  • point (Hash) —

    point in DR hash notation.

  • triangle (Hash) —

    triangle in Dinraal hash notation.

Returns:

  • (Boolean) —

    true or false



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'app/lib/dinraal/point.rb', line 62

def point_inside_triangle?(point:, triangle:)
  args = $gtk.args

  point_x = point[:x]
  point_y = point[:y]
  x = triangle[:x]
  y = triangle[:y]
  x2 = triangle[:x2]
  y2 = triangle[:y2]
  x3 = triangle[:x3]
  y3 = triangle[:y3]

  triangle = [[x, y], [x2, y2], [x3, y3]]
  triangle = triangle.sort_by { |sort_point| sort_point[1] }
  triangle = triangle.reverse

  leg0 = [triangle[0], triangle[1]]
  leg0_slope = args.geometry.line_slope(leg0.flatten, replace_infinity: 1080)
  leg0_intercept = triangle[0][1] - (leg0_slope * triangle[0][0])

  return false unless point_y <= (leg0_slope * point_x) + leg0_intercept

  leg1 = [triangle[0], triangle[2]]
  leg1_slope = args.geometry.line_slope(leg1.flatten, replace_infinity: 1080)
  leg1_intercept = triangle[0][1] - (leg1_slope * triangle[0][0])

  return false unless point_y <= (leg1_slope * point_x) + leg1_intercept

  leg2 = [triangle[1], triangle[2]]
  leg2_slope = args.geometry.line_slope(leg2.flatten, replace_infinity: 1080)
  leg2_intercept = triangle[2][1] - (leg2_slope * triangle[2][0])

  return false unless point_y >= (leg2_slope * point_x) + leg2_intercept

  true
end

#two_point_eq(p0, p1) ⇒ Object

Returns [].

Parameters:

  • p0 (Array) —

    A point in array notation

  • p1 (Array) —

    A point in array notation

Returns:

  • []



105
106
107
# File 'app/lib/dinraal/point.rb', line 105

def two_point_eq(p0, p1)
  ->(x) { ((p1.y - p0.y) / (p1.x - p0.x) * (x - p0.x)) + p0.y }
end