Class: Ruby2D::Line

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

Instance Attribute Summary collapse

Attributes included from Renderable

#z

Instance Method Summary collapse

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
19
20
# File 'lib/ruby2d/line.rb', line 8

def initialize(opts = {})
  @type_id = 3

  @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

#colorObject

Returns the value of attribute color.



6
7
8
# File 'lib/ruby2d/line.rb', line 6

def color
  @color
end

#widthObject

Returns the value of attribute width.



6
7
8
# File 'lib/ruby2d/line.rb', line 6

def width
  @width
end

#x1Object

Returns the value of attribute x1.



6
7
8
# File 'lib/ruby2d/line.rb', line 6

def x1
  @x1
end

#x2Object

Returns the value of attribute x2.



6
7
8
# File 'lib/ruby2d/line.rb', line 6

def x2
  @x2
end

#y1Object

Returns the value of attribute y1.



6
7
8
# File 'lib/ruby2d/line.rb', line 6

def y1
  @y1
end

#y2Object

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

Returns:

  • (Boolean)


34
35
36
37
38
# File 'lib/ruby2d/line.rb', line 34

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

#lengthObject



27
28
29
# File 'lib/ruby2d/line.rb', line 27

def length
  points_distance(@x1, @y1, @x2, @y2)
end