Class: Ruby2D::Triangle

Inherits:
Object
  • Object
show all
Includes:
Renderable
Defined in:
lib/ruby2d/triangle.rb,
ext/ruby2d/ruby2d-opal.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 = {}) ⇒ Triangle

Returns a new instance of Triangle.



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

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

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

#colorObject

Returns the value of attribute color.



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

def color
  @color
end

#type_idObject (readonly)

Returns the value of attribute type_id.



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

def type_id
  @type_id
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

Instance Method Details

#contains?(x, y) ⇒ Boolean

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.

Returns:

  • (Boolean)


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

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

#ext_renderObject



107
108
109
110
111
112
113
# File 'ext/ruby2d/ruby2d-opal.rb', line 107

def ext_render
  `S2D.DrawTriangle(
    #{self}.x1, #{self}.y1, #{self}.c1.r, #{self}.c1.g, #{self}.c1.b, #{self}.c1.a,
    #{self}.x2, #{self}.y2, #{self}.c2.r, #{self}.c2.g, #{self}.c2.b, #{self}.c2.a,
    #{self}.x3, #{self}.y3, #{self}.c3.r, #{self}.c3.g, #{self}.c3.b, #{self}.c3.a
  );`
end