Class: Ruby2D::Triangle

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

Overview

A triangle

Instance Attribute Summary collapse

Attributes included from Renderable

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

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Renderable

#add, #remove

Constructor Details

#initialize(x1: 50, y1: 0, x2: 100, y2: 100, x3: 0, y3: 100, z: 0, color: 'white', colour: nil, opacity: nil) ⇒ Triangle

Create a triangle

Parameters:

  • x1 (Numeric) (defaults to: 50)
  • y1 (Numeric) (defaults to: 0)
  • x2 (Numeric) (defaults to: 100)
  • y2 (Numeric) (defaults to: 100)
  • x3 (Numeric) (defaults to: 0)
  • y3 (Numeric) (defaults to: 100)
  • z (Numeric) (defaults to: 0)
  • color (String, Array) (defaults to: 'white')

    A single colour or an array of exactly 3 colours

  • opacity (Numeric) (defaults to: nil)

    Opacity of the image when rendering

Raises:

  • (ArgumentError)

    if an array of colours does not have 3 entries



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/ruby2d/triangle.rb', line 26

def initialize(x1: 50, y1: 0, x2: 100, y2: 100, x3: 0, y3: 100,
               z: 0, color: 'white', colour: nil, opacity: nil)
  @x1 = x1
  @y1 = y1
  @x2 = x2
  @y2 = y2
  @x3 = x3
  @y3 = y3
  @z  = z
  self.color = color || colour
  self.color.opacity = opacity if opacity
  add
end

Instance Attribute Details

#colorObject

Returns the value of attribute color.



13
14
15
# File 'lib/ruby2d/triangle.rb', line 13

def color
  @color
end

#x1Object

Returns the value of attribute x1.



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

def x1
  @x1
end

#x2Object

Returns the value of attribute x2.



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

def x2
  @x2
end

#x3Object

Returns the value of attribute x3.



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

def x3
  @x3
end

#y1Object

Returns the value of attribute y1.



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

def y1
  @y1
end

#y2Object

Returns the value of attribute y2.



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

def y2
  @y2
end

#y3Object

Returns the value of attribute y3.



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

def y3
  @y3
end

Class Method Details

.draw(x1:, y1:, x2:, y2:, x3:, y3:, color:) ⇒ Object

Draw a triangle

Parameters:

  • x1 (Numeric)
  • y1 (Numeric)
  • x2 (Numeric)
  • y2 (Numeric)
  • x3 (Numeric)
  • y3 (Numeric)
  • z (Numeric)
  • color (Array<Array<float,float,float,float>>)

    An array of 3 arrays of colour components (e.g. [[1.0, 0, 0, 1.0], …])



79
80
81
82
83
84
85
86
# File 'lib/ruby2d/triangle.rb', line 79

def self.draw(x1:, y1:, x2:, y2:, x3:, y3:, color:)
  Window.render_ready_check
  ext_draw([
             x1, y1, *color[0], # splat the colour components
             x2, y2, *color[1],
             x3, y3, *color[2]
           ])
end

Instance Method Details

#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)


59
60
61
62
63
64
65
66
67
# File 'lib/ruby2d/triangle.rb', line 59

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