Class: Ruby2D::Line
Overview
A line between two points.
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
Class Method Summary collapse
-
.draw(x1:, y1:, x2:, y2:, width:, color:) ⇒ Object
Draw a line without creating a Line.
Instance Method Summary collapse
-
#color=(color) ⇒ Object
Change the colour of the line.
-
#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(x1: 0, y1: 0, x2: 100, y2: 100, z: 0, width: 2, color: nil, colour: nil, opacity: nil) ⇒ Line
constructor
Create an Line.
-
#length ⇒ Object
Return the length of the line.
Methods included from Renderable
Constructor Details
#initialize(x1: 0, y1: 0, x2: 100, y2: 100, z: 0, width: 2, color: nil, colour: nil, opacity: nil) ⇒ Line
Create an Line
22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/ruby2d/line.rb', line 22 def initialize(x1: 0, y1: 0, x2: 100, y2: 100, z: 0, width: 2, color: nil, colour: nil, opacity: nil) @x1 = x1 @y1 = y1 @x2 = x2 @y2 = y2 @z = z @width = width self.color = color || colour || 'white' self.color.opacity = opacity unless opacity.nil? add end |
Instance Attribute Details
#width ⇒ Object
Returns the value of attribute width.
10 11 12 |
# File 'lib/ruby2d/line.rb', line 10 def width @width end |
#x1 ⇒ Object
Returns the value of attribute x1.
10 11 12 |
# File 'lib/ruby2d/line.rb', line 10 def x1 @x1 end |
#x2 ⇒ Object
Returns the value of attribute x2.
10 11 12 |
# File 'lib/ruby2d/line.rb', line 10 def x2 @x2 end |
#y1 ⇒ Object
Returns the value of attribute y1.
10 11 12 |
# File 'lib/ruby2d/line.rb', line 10 def y1 @y1 end |
#y2 ⇒ Object
Returns the value of attribute y2.
10 11 12 |
# File 'lib/ruby2d/line.rb', line 10 def y2 @y2 end |
Class Method Details
.draw(x1:, y1:, x2:, y2:, width:, color:) ⇒ Object
Draw a line without creating a Line
75 76 77 78 79 80 81 82 83 |
# File 'lib/ruby2d/line.rb', line 75 def self.draw(x1:, y1:, x2:, y2:, width:, color:) Window.render_ready_check ext_draw([ x1, y1, x2, y2, width, # splat each colour's components *color[0], *color[1], *color[2], *color[3] ]) end |
Instance Method Details
#color=(color) ⇒ Object
Change the colour of the line
38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/ruby2d/line.rb', line 38 def color=(color) # convert to Color or Color::Set color = Color.set(color) # require 4 colours if multiple colours provided if color.is_a?(Color::Set) && color.length != 4 raise ArgumentError, "`#{self.class}` requires 4 colors, one for each vertex. #{color.length} were given." end @color = color # converted above invalidate_color_components 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
61 62 63 64 65 66 |
# File 'lib/ruby2d/line.rb', line 61 def contains?(x, y) line_len = length points_distance(@x1, @y1, x, y) <= line_len && points_distance(@x2, @y2, x, y) <= line_len && (((@y2 - @y1) * x - (@x2 - @x1) * y + @x2 * @y1 - @y2 * @x1).abs / line_len) <= 0.5 * @width end |
#length ⇒ Object
Return the length of the line
53 54 55 |
# File 'lib/ruby2d/line.rb', line 53 def length points_distance(@x1, @y1, @x2, @y2) end |