Class: ProxBox

Inherits:
Object
  • Object
show all
Defined in:
lib/proxbox.rb,
lib/proxbox/version.rb

Constant Summary collapse

VERSION =

:nodoc:

"0.0.2"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ ProxBox

Returns a new instance of ProxBox.



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/proxbox.rb', line 33

def initialize( params = {} )
  @pbox = params.fetch(:pbox, nil)
  if !!@pbox
    raise "Invalid list of layout details!" unless @pbox.all? { |grid, obj|
      grid.all? { |layout|
        layout.is_a?(Fixnum)
      } && grid.length == 4
    }
    @pbox = pop_relationships(@pbox)
  end
end

Instance Attribute Details

#pboxObject

Returns the value of attribute pbox.



31
32
33
# File 'lib/proxbox.rb', line 31

def pbox
  @pbox
end

Instance Method Details

#closest_by_index(array_points, value = 0) ⇒ Object



120
121
122
123
124
# File 'lib/proxbox.rb', line 120

def closest_by_index(array_points, value = 0)
  array_points.index (
    closest_by_value(array_points, value)
  )
end

#closest_by_value(array_points, value = 0) ⇒ Object



116
117
118
# File 'lib/proxbox.rb', line 116

def closest_by_value(array_points, value = 0)
  array_points.min_by { |x| (x.to_f - value).abs }
end

#euclidean_distance(a, b) ⇒ Object



59
60
61
62
63
# File 'lib/proxbox.rb', line 59

def euclidean_distance(a,b)
  raise "Array's required!" unless a.is_a?(Array) && b.is_a?(Array)
  raise "Array's must contain Integers!" unless [a,b].all? {|h| h.all? {|i| i.is_a?(Fixnum)} }
  Math.sqrt(a.zip(b).map { |x| (x[1] - x[0])**2 }.reduce(:+))
end

#four_points(x, y, width, height) ⇒ Object



112
113
114
# File 'lib/proxbox.rb', line 112

def four_points(x,y,width,height)
  Array( [x,y,x+width,y+height] )
end

#overall_relationship(a, b) ⇒ Object



126
127
128
129
130
131
132
# File 'lib/proxbox.rb', line 126

def overall_relationship(a,b)
  hash = two_dimension_compare(a,b)
  closest_by_value(
    [  closest_by_value( hash[ :vertical   ] ),
       closest_by_value( hash[ :horizontal ] )  ]
  )
end

#pop_relationships(everything) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/proxbox.rb', line 45

def pop_relationships(everything)
  raise "Incorrect Array length(s)!  Each must be 2 in length!" unless everything.all? {|i| i.length == 2}
  everything.each_with_index { |item, index|
    everything.each_with_index { |sectem, secdex|
      unless index == secdex
        everything[index][2] = Array(everything[index][2]) << overall_relationship(item[0],sectem[0])
      else
        everything[index][2] = Array(everything[index][2]) << nil
      end
    }
  }
  everything
end

#strongest_relationship(arr) ⇒ Object



108
109
110
# File 'lib/proxbox.rb', line 108

def strongest_relationship(arr)
  closest_by_value(arr)
end

#two_dimension_compare(a, b) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/proxbox.rb', line 65

def two_dimension_compare(a,b)
  tdep = two_dimension_edge_points(a,b)
  {
    # :horizontal => [1<->2, 2<->1, 3<->4, 4<->3]
    :horizontal => [
      euclidean_distance( *tdep[:horizontal][0] ),
      euclidean_distance( *tdep[:horizontal][1] ),
      euclidean_distance( *tdep[:horizontal][2] ),
      euclidean_distance( *tdep[:horizontal][3] )
    ],

    # :vertical   => [1<->3, 2<->4, 3<->1, 4<->2]
    :vertical => [
      euclidean_distance( *tdep[:vertical][0] ),
      euclidean_distance( *tdep[:vertical][1] ),
      euclidean_distance( *tdep[:vertical][2] ),
      euclidean_distance( *tdep[:vertical][3] )
    ]
  }
end

#two_dimension_edge_points(a, b) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/proxbox.rb', line 86

def two_dimension_edge_points(a,b)
  a = Array four_points(*a)
  b = Array four_points(*b)
  {
    # :horizontal => [1<->2, 2<->1, 3<->4, 4<->3]
    :horizontal => [
      [ a[0..1], [b[2],b[1]] ],
      [ [a[2],a[1]], b[0..1] ],
      [ [a[0],a[3]], b[2..3] ],
      [ a[2..3], [b[0],b[3]] ]
    ],

    # :vertical   => [1<->3, 2<->4, 3<->1, 4<->2]
    :vertical => [
      [ a[0..1], [b[0],b[3]] ],
      [ [a[2],a[1]], b[2..3] ],
      [ [a[0],a[3]], b[0..1] ],
      [ a[2..3], [b[2],b[1]] ]
    ]
  }
end