Class: Ruby2D::Line
- Inherits:
-
Object
- Object
- Ruby2D::Line
- Includes:
- Renderable
- Defined in:
- lib/ruby2d/line.rb,
ext/ruby2d/ruby2d-opal.rb
Instance Attribute Summary collapse
-
#color ⇒ Object
Returns the value of attribute color.
-
#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
-
#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.
- #ext_render ⇒ Object
-
#initialize(opts = {}) ⇒ Line
constructor
A new instance of Line.
- #length ⇒ Object
Methods included from Renderable
#add, #opacity, #opacity=, #remove
Constructor Details
#initialize(opts = {}) ⇒ Line
Returns a new instance of Line.
8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/ruby2d/line.rb', line 8 def initialize(opts = {}) @x1 = opts[:x1] || 0 @y1 = opts[:y1] || 0 @x2 = opts[:x2] || 100 @y2 = opts[:y2] || 100 @width = opts[:width] || 2 @z = opts[:z] || 0 self.color = opts[:color] || 'white' add end |
Instance Attribute Details
#color ⇒ Object
Returns the value of attribute color.
6 7 8 |
# File 'lib/ruby2d/line.rb', line 6 def color @color end |
#width ⇒ Object
Returns the value of attribute width.
6 7 8 |
# File 'lib/ruby2d/line.rb', line 6 def width @width end |
#x1 ⇒ Object
Returns the value of attribute x1.
6 7 8 |
# File 'lib/ruby2d/line.rb', line 6 def x1 @x1 end |
#x2 ⇒ Object
Returns the value of attribute x2.
6 7 8 |
# File 'lib/ruby2d/line.rb', line 6 def x2 @x2 end |
#y1 ⇒ Object
Returns the value of attribute y1.
6 7 8 |
# File 'lib/ruby2d/line.rb', line 6 def y1 @y1 end |
#y2 ⇒ Object
Returns the value of attribute y2.
6 7 8 |
# File 'lib/ruby2d/line.rb', line 6 def y2 @y2 end |
Instance Method Details
#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. Check en.wikipedia.org/wiki/Distance_from_a_point_to_a_line for reference
32 33 34 35 36 |
# File 'lib/ruby2d/line.rb', line 32 def contains?(x, y) points_distance(x1, y1, x, y) < length and points_distance(x2, y2, x, y) < length and (((@y2 - @y1) * x - (@x2 - @x1) * y + @x2 * @y1 - @y2 * @x1).abs / length) < 0.5 * @width end |
#ext_render ⇒ Object
128 129 130 131 132 133 134 135 136 |
# File 'ext/ruby2d/ruby2d-opal.rb', line 128 def ext_render `S2D.DrawLine( #{self}.x1, #{self}.y1, #{self}.x2, #{self}.y2, #{self}.width, #{self}.c1.r, #{self}.c1.g, #{self}.c1.b, #{self}.c1.a, #{self}.c2.r, #{self}.c2.g, #{self}.c2.b, #{self}.c2.a, #{self}.c3.r, #{self}.c3.g, #{self}.c3.b, #{self}.c3.a, #{self}.c4.r, #{self}.c4.g, #{self}.c4.b, #{self}.c4.a );` end |
#length ⇒ Object
25 26 27 |
# File 'lib/ruby2d/line.rb', line 25 def length points_distance(@x1, @y1, @x2, @y2) end |