Class: Ruby2D::Triangle

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

Instance Attribute Summary collapse

Attributes included from Renderable

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

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Renderable

#add, #remove

Constructor Details

#initialize(opts = {}) ⇒ Triangle

Returns a new instance of Triangle.



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/ruby2d/triangle.rb', line 11

def initialize(opts= {})
  @x1 = opts[:x1] || 50
  @y1 = opts[:y1] || 0
  @x2 = opts[:x2] || 100
  @y2 = opts[:y2] || 100
  @x3 = opts[:x3] || 0
  @y3 = opts[:y3] || 100
  @z  = opts[:z]  || 0
  self.color = opts[:color] || 'white'
  self.color.opacity = opts[:opacity] if opts[:opacity]
  add
end

Instance Attribute Details

#c1Object

Returns the value of attribute c1.



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

def c1
  @c1
end

#c2Object

Returns the value of attribute c2.



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

def c2
  @c2
end

#c3Object

Returns the value of attribute c3.



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

def c3
  @c3
end

#x1Object

Returns the value of attribute x1.



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

def x1
  @x1
end

#x2Object

Returns the value of attribute x2.



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

def x2
  @x2
end

#x3Object

Returns the value of attribute x3.



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

def x3
  @x3
end

#y1Object

Returns the value of attribute y1.



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

def y1
  @y1
end

#y2Object

Returns the value of attribute y2.



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

def y2
  @y2
end

#y3Object

Returns the value of attribute y3.



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

def y3
  @y3
end

Class Method Details

.draw(opts = {}) ⇒ Object



41
42
43
44
45
46
47
48
49
# File 'lib/ruby2d/triangle.rb', line 41

def self.draw(opts = {})
  Window.render_ready_check

  ext_draw([
    opts[:x1], opts[:y1], opts[:color][0][0], opts[:color][0][1], opts[:color][0][2], opts[:color][0][3],
    opts[:x2], opts[:y2], opts[:color][1][0], opts[:color][1][1], opts[:color][1][2], opts[:color][1][3],
    opts[:x3], opts[:y3], opts[:color][2][0], opts[:color][2][1], opts[:color][2][2], opts[:color][2][3]
  ])
end

Instance Method Details

#color=(c) ⇒ Object



24
25
26
27
# File 'lib/ruby2d/triangle.rb', line 24

def color=(c)
  @color = Color.set(c)
  update_color(@color)
end

#contains?(x, y) ⇒ Boolean

A point is inside a triangle if the area of 3 triangles, constructed from triangle sides and the given point, is equal to the area of triangle.

Returns:

  • (Boolean)


31
32
33
34
35
36
37
38
39
# File 'lib/ruby2d/triangle.rb', line 31

def contains?(x, y)
  self_area = triangle_area(@x1, @y1, @x2, @y2, @x3, @y3)
  questioned_area =
    triangle_area(@x1, @y1, @x2, @y2, x, y) +
    triangle_area(@x2, @y2, @x3, @y3, x, y) +
    triangle_area(@x3, @y3, @x1, @y1, x, y)

  questioned_area <= self_area
end