Class: Ruby2D::Line
Instance Attribute Summary collapse
-
#width ⇒ Object
Returns the value of attribute width.
-
#x1 ⇒ Object
Returns the value of attribute x1.
-
#x2 ⇒ Object
Returns the value of attribute x2.
-
#y1 ⇒ Object
Returns the value of attribute y1.
-
#y2 ⇒ Object
Returns the value of attribute y2.
Attributes included from Renderable
Instance Method Summary collapse
- #color=(c) ⇒ Object
-
#contains?(x, y) ⇒ Boolean
Line contains a point if the point is closer than the length of line from both ends and if the distance from point to line is smaller than half of the width.
-
#initialize(opts = {}) ⇒ Line
constructor
A new instance of Line.
-
#length ⇒ Object
Return the length of the line.
Methods included from Renderable
#a, #a=, #add, #b, #b=, #colour, #colour=, #g, #g=, #opacity, #opacity=, #r, #r=, #remove
Constructor Details
#initialize(opts = {}) ⇒ Line
Returns a new instance of Line.
9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/ruby2d/line.rb', line 9 def initialize(opts = {}) @x1 = opts[:x1] || 0 @y1 = opts[:y1] || 0 @x2 = opts[:x2] || 100 @y2 = opts[:y2] || 100 @z = opts[:z] || 0 @width = opts[:width] || 2 self.color = opts[:color] || 'white' self.opacity = opts[:opacity] if opts[:opacity] add end |
Instance Attribute Details
#width ⇒ Object
Returns the value of attribute width.
7 8 9 |
# File 'lib/ruby2d/line.rb', line 7 def width @width end |
#x1 ⇒ Object
Returns the value of attribute x1.
7 8 9 |
# File 'lib/ruby2d/line.rb', line 7 def x1 @x1 end |
#x2 ⇒ Object
Returns the value of attribute x2.
7 8 9 |
# File 'lib/ruby2d/line.rb', line 7 def x2 @x2 end |
#y1 ⇒ Object
Returns the value of attribute y1.
7 8 9 |
# File 'lib/ruby2d/line.rb', line 7 def y1 @y1 end |
#y2 ⇒ Object
Returns the value of attribute y2.
7 8 9 |
# File 'lib/ruby2d/line.rb', line 7 def y2 @y2 end |
Instance Method Details
#color=(c) ⇒ Object
21 22 23 24 |
# File 'lib/ruby2d/line.rb', line 21 def color=(c) @color = Color.set(c) update_color(@color) end |
#contains?(x, y) ⇒ Boolean
Line contains a point if the point is closer than the length of line from both ends and if the distance from point to line is smaller than half of the width. For reference:
https://en.wikipedia.org/wiki/Distance_from_a_point_to_a_line
35 36 37 38 39 |
# File 'lib/ruby2d/line.rb', line 35 def contains?(x, y) points_distance(x1, y1, x, y) <= length && points_distance(x2, y2, x, y) <= length && (((@y2 - @y1) * x - (@x2 - @x1) * y + @x2 * @y1 - @y2 * @x1).abs / length) <= 0.5 * @width end |
#length ⇒ Object
Return the length of the line
27 28 29 |
# File 'lib/ruby2d/line.rb', line 27 def length points_distance(@x1, @y1, @x2, @y2) end |