summaryrefslogtreecommitdiffhomepage
path: root/lib/ruby2d/triangle.rb
blob: eb6b07f6f06700945811f809b5abd505a3c0f999 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# triangle.rb

module Ruby2D
  class Triangle
    include Renderable

    attr_accessor :x1, :y1, :c1,
                  :x2, :y2, :c2,
                  :x3, :y3, :c3
    attr_reader :color, :type_id

    def initialize(x1=50, y1=0, x2=100, y2=100, x3=0, y3=100, c='white', z=0)
      @type_id = 1
      @x1, @y1 = x1, y1
      @x2, @y2 = x2, y2
      @x3, @y3 = x3, y3
      @z = z

      self.color = c
      add
    end

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

    private

    def update_color(c)
      if c.is_a? Color::Set
        if c.length == 3
          @c1 = c[0]
          @c2 = c[1]
          @c3 = c[2]
        else
          raise ArgumentError, "Triangles require 3 colors, one for each vertex. #{c.length} were given."
        end
      else
        @c1 = c
        @c2 = c
        @c3 = c
      end
    end
  end
end