Class: AutoBrowse::MouseMover::Vector

Inherits:
Object
  • Object
show all
Includes:
MathUtils
Defined in:
lib/auto_browse/mouse.rb

Overview

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from MathUtils

#clamp, #confine, #coordinate_floor0, #fitts, #identity_floor0, #random_number_range

Constructor Details

#initialize(x, y) ⇒ Vector

Returns a new instance of Vector.



56
57
58
# File 'lib/auto_browse/mouse.rb', line 56

def initialize(x, y)
  @x, @y = x.to_f, y.to_f
end

Instance Attribute Details

#xObject

Returns the value of attribute x.



54
55
56
# File 'lib/auto_browse/mouse.rb', line 54

def x
  @x
end

#yObject

Returns the value of attribute y.



54
55
56
# File 'lib/auto_browse/mouse.rb', line 54

def y
  @y
end

Instance Method Details

#*(multiplier) ⇒ Object



72
73
74
# File 'lib/auto_browse/mouse.rb', line 72

def *(multiplier)
  Vector.new(x * multiplier, y * multiplier)
end

#+(other) ⇒ Object



64
65
66
# File 'lib/auto_browse/mouse.rb', line 64

def +(other)
  Vector.new(x + other.x, y + other.y)
end

#-(other) ⇒ Object



68
69
70
# File 'lib/auto_browse/mouse.rb', line 68

def -(other)
  Vector.new(x - other.x, y - other.y)
end

#/(divisor) ⇒ Object



76
77
78
# File 'lib/auto_browse/mouse.rb', line 76

def /(divisor)
  Vector.new(x / divisor, y / divisor)
end

#bezier_curve(finish) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/auto_browse/mouse.rb', line 130

def bezier_curve(finish)
  min = 10
  max = 90

  start = self
  deviation = start.direction(finish).magnitude * 0.2
  spread = confine(deviation, min, max)
  anchors = start.generate_bezier_anchors(finish, spread)
  
  control_point_vectors = [start] + anchors + [finish]
  control_points = control_point_vectors.map(&:to_a)
  BezierCurve.new(*control_points)
end

#bezier_curve_through_points(finish, intermediate_points = []) ⇒ Object



144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/auto_browse/mouse.rb', line 144

def bezier_curve_through_points(finish, intermediate_points = [])
  min = 10
  max = 90

  start = self
  deviation = start.direction(finish).magnitude * 0.2
  spread = confine(deviation, min, max)
  anchors = start.generate_bezier_anchors(finish, spread)
  
  control_point_vectors = [start] + anchors + [finish]
  control_points = control_point_vectors.map(&:to_a)
  BezierCurve.new(*control_points)
end

#direction(other) ⇒ Object



80
81
82
# File 'lib/auto_browse/mouse.rb', line 80

def direction(other)
  other - self
end

#generate_bezier_anchors(other, spread) ⇒ Object



113
114
115
116
117
118
119
120
121
# File 'lib/auto_browse/mouse.rb', line 113

def generate_bezier_anchors(other, spread)
  side = rand.round == 1 ? 1 : -1
  calc = ->() do
    rand_mid, normal_vector = self.random_normal_line(other, spread)
    choice = normal_vector * side
    rand_mid.random_vector_on_line(rand_mid + choice)
  end
  [calc.(), calc.()].sort {|a, b| a.x - b.x }
end

#magnitudeObject



88
89
90
# File 'lib/auto_browse/mouse.rb', line 88

def magnitude
  Math.sqrt(x ** 2 + y ** 2)
end

#overshoot(radius) ⇒ Object



123
124
125
126
127
128
# File 'lib/auto_browse/mouse.rb', line 123

def overshoot(radius)
  a = rand * 2 * Math::PI
  rad = radius * Math.sqrt(rand)
  vector = Vector.new(rad * Math.cos(a), rad * Math.sin(a))
  self + vector
end

#perpendicularObject



84
85
86
# File 'lib/auto_browse/mouse.rb', line 84

def perpendicular
  Vector.new(y, -1 * x)
end

#random_normal_line(other, magnitude) ⇒ Object



107
108
109
110
111
# File 'lib/auto_browse/mouse.rb', line 107

def random_normal_line(other, magnitude)
  rand_mid = random_vector_on_line(other)
  normal_vector = direction(rand_mid).perpendicular.set_magnitude(magnitude)
  [rand_mid, normal_vector]
end

#random_vector_on_line(other) ⇒ Object

returns a randomly chosen vector that points to some point on the vector from self to other.



101
102
103
104
105
# File 'lib/auto_browse/mouse.rb', line 101

def random_vector_on_line(other)
  vec = direction(other)
  multiplier = rand
  self + (vec * multiplier)
end

#set_magnitude(magnitude) ⇒ Object



96
97
98
# File 'lib/auto_browse/mouse.rb', line 96

def set_magnitude(magnitude)
  unit * magnitude
end

#to_aObject



60
61
62
# File 'lib/auto_browse/mouse.rb', line 60

def to_a
  [x, y]
end

#unitObject



92
93
94
# File 'lib/auto_browse/mouse.rb', line 92

def unit
  self / magnitude
end