Class: Capybara::Queries::SelectorQuery::Rectangle Private
Relationships & Source Files | |
Inherits: | Object |
Defined in: | lib/capybara/queries/selector_query.rb |
Class Method Summary
- .new(position) ⇒ Rectangle constructor Internal use only
Instance Attribute Summary
Instance Method Summary
- #above?(other) ⇒ Boolean Internal use only
- #below?(other) ⇒ Boolean Internal use only
- #distance(other) Internal use only
- #left_of?(other) ⇒ Boolean Internal use only
- #near?(other) ⇒ Boolean Internal use only
- #right_of?(other) ⇒ Boolean Internal use only
- #distance_segment_segment(l1p1, l1p2, l2p1, l2p2) private Internal use only
Constructor Details
.new(position) ⇒ Rectangle
# File 'lib/capybara/queries/selector_query.rb', line 629
def initialize(position) # rubocop:disable Style/RescueModifier @top = position['top'] rescue position['y'] @bottom = position['bottom'] rescue (@top + position['height']) @left = position['left'] rescue position['x'] @right = position['right'] rescue (@left + position['width']) # rubocop:enable Style/RescueModifier end
Instance Attribute Details
#bottom (readonly)
[ GitHub ]#left (readonly)
[ GitHub ]#right (readonly)
[ GitHub ]#top (readonly)
[ GitHub ]Instance Method Details
#above?(other) ⇒ Boolean
#below?(other) ⇒ Boolean
#distance(other)
[ GitHub ]# File 'lib/capybara/queries/selector_query.rb', line 638
def distance(other) distance = Float::INFINITY line_segments.each do |ls1| other.line_segments.each do |ls2| distance = [ distance, distance_segment_segment(*ls1, *ls2) ].min end end distance end
#distance_segment_segment(l1p1, l1p2, l2p1, l2p2) (private)
[ GitHub ]# File 'lib/capybara/queries/selector_query.rb', line 686
def distance_segment_segment(l1p1, l1p2, l2p1, l2p2) # See http://geomalgorithms.com/a07-_distance.html # rubocop:disable Naming/VariableName u = l1p2 - l1p1 v = l2p2 - l2p1 w = l1p1 - l2p1 a = u.dot u b = u.dot v c = v.dot v d = u.dot w e = v.dot w cap_d = (a * c) - (b**2) sD = tD = cap_d # compute the line parameters of the two closest points if cap_d < Float::EPSILON # the lines are almost parallel sN = 0.0 # force using point P0 on segment S1 sD = 1.0 # to prevent possible division by 0.0 later tN = e tD = c else # get the closest points on the infinite lines sN = (b * e) - (c * d) tN = (a * e) - (b * d) if sN.negative? # sc < 0 => the s=0 edge is visible sN = 0 tN = e tD = c elsif sN > sD # sc > 1 => the s=1 edge is visible sN = sD tN = e + b tD = c end end if tN.negative? # tc < 0 => the t=0 edge is visible tN = 0 # recompute sc for this edge if (-d).negative? sN = 0.0 elsif -d > a sN = sD else sN = -d sD = a end elsif tN > tD # tc > 1 => the t=1 edge is visible tN = tD # recompute sc for this edge if (-d + b).negative? sN = 0.0 elsif (-d + b) > a sN = sD else sN = (-d + b) sD = a end end # finally do the division to get sc and tc sc = sN.abs < Float::EPSILON ? 0.0 : sN / sD tc = tN.abs < Float::EPSILON ? 0.0 : tN / tD # difference of the two closest points dP = w + (u * sc) - (v * tc) Math.sqrt(dP.dot(dP)) # rubocop:enable Naming/VariableName end
#left_of?(other) ⇒ Boolean
#near?(other) ⇒ Boolean
# File 'lib/capybara/queries/selector_query.rb', line 669
def near?(other) distance(other) <= 50 end