summaryrefslogtreecommitdiffhomepage
path: root/lib/ruby2d/triangle.rb
blob: 75615cfa8b4a89bc85e732ae0c0778336c7bd536 (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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# triangle.rb

module Ruby2D
  class Triangle
    include Renderable

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

    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'
      add
    end

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

    # Point is inside a triangle if
    # the area of 3 triangles, constructed from triangle sides and that point
    # is equal to the area of triangle.
    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

    private

    def triangle_area(x1, y1, x2, y2, x3, y3)
      (x1*y2 + x2*y3 + x3*y1 - x3*y2 - x1*y3 - x2*y1).abs / 2
    end

    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