Class: Ruby2D::Line

Inherits:
Object
  • Object
show all
Includes:
Renderable
Defined in:
lib/ruby2d/line.rb

Overview

A line between two points.

Instance Attribute Summary collapse

Attributes included from Renderable

#color, #height, #x, #y, #z

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Renderable

#add, #remove

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

Parameters:

  • x1 (Numeric) (defaults to: 0)
  • y1 (Numeric) (defaults to: 0)
  • x2 (Numeric) (defaults to: 100)
  • y2 (Numeric) (defaults to: 100)
  • width (Numeric) (defaults to: 2)

    The width or thickness of the line

  • z (Numeric) (defaults to: 0)
  • color (String, Array) (defaults to: nil)

    A single colour or an array of exactly 4 colours

  • opacity (Numeric) (defaults to: nil)

    Opacity of the image when rendering

Raises:

  • (ArgumentError)

    if an array of colours does not have 4 entries



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

#widthObject

Returns the value of attribute width.



10
11
12
# File 'lib/ruby2d/line.rb', line 10

def width
  @width
end

#x1Object

Returns the value of attribute x1.



10
11
12
# File 'lib/ruby2d/line.rb', line 10

def x1
  @x1
end

#x2Object

Returns the value of attribute x2.



10
11
12
# File 'lib/ruby2d/line.rb', line 10

def x2
  @x2
end

#y1Object

Returns the value of attribute y1.



10
11
12
# File 'lib/ruby2d/line.rb', line 10

def y1
  @y1
end

#y2Object

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

Parameters:

  • x1 (Numeric)
  • y1 (Numeric)
  • x2 (Numeric)
  • y2 (Numeric)
  • width (Numeric)

    The width or thickness of the line

  • colors (Array)

    An array or 4 arrays of colour components.



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

Parameters:

  • color (String, Array)

    A single colour or an array of exactly 4 colours

Raises:

  • (ArgumentError)

    if an array of colours does not have 4 entries



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

Returns:

  • (Boolean)


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

#lengthObject

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