Class: Ruby2D::Triangle
- Inherits:
-
Object
- Object
- Ruby2D::Triangle
- Includes:
- Renderable
- Defined in:
- lib/ruby2d/triangle.rb
Instance Attribute Summary collapse
-
#c1 ⇒ Object
Returns the value of attribute c1.
-
#c2 ⇒ Object
Returns the value of attribute c2.
-
#c3 ⇒ Object
Returns the value of attribute c3.
-
#x1 ⇒ Object
Returns the value of attribute x1.
-
#x2 ⇒ Object
Returns the value of attribute x2.
-
#x3 ⇒ Object
Returns the value of attribute x3.
-
#y1 ⇒ Object
Returns the value of attribute y1.
-
#y2 ⇒ Object
Returns the value of attribute y2.
-
#y3 ⇒ Object
Returns the value of attribute y3.
Attributes included from Renderable
#color, #height, #width, #x, #y, #z
Instance Method Summary collapse
- #color=(c) ⇒ Object
-
#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.
-
#initialize(opts = {}) ⇒ Triangle
constructor
A new instance of Triangle.
Methods included from Renderable
#a, #a=, #add, #b, #b=, #colour, #colour=, #g, #g=, #opacity, #opacity=, #r, #r=, #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.opacity = opts[:opacity] if opts[:opacity] add end |
Instance Attribute Details
#c1 ⇒ Object
Returns the value of attribute c1.
7 8 9 |
# File 'lib/ruby2d/triangle.rb', line 7 def c1 @c1 end |
#c2 ⇒ Object
Returns the value of attribute c2.
7 8 9 |
# File 'lib/ruby2d/triangle.rb', line 7 def c2 @c2 end |
#c3 ⇒ Object
Returns the value of attribute c3.
7 8 9 |
# File 'lib/ruby2d/triangle.rb', line 7 def c3 @c3 end |
#x1 ⇒ Object
Returns the value of attribute x1.
7 8 9 |
# File 'lib/ruby2d/triangle.rb', line 7 def x1 @x1 end |
#x2 ⇒ Object
Returns the value of attribute x2.
7 8 9 |
# File 'lib/ruby2d/triangle.rb', line 7 def x2 @x2 end |
#x3 ⇒ Object
Returns the value of attribute x3.
7 8 9 |
# File 'lib/ruby2d/triangle.rb', line 7 def x3 @x3 end |
#y1 ⇒ Object
Returns the value of attribute y1.
7 8 9 |
# File 'lib/ruby2d/triangle.rb', line 7 def y1 @y1 end |
#y2 ⇒ Object
Returns the value of attribute y2.
7 8 9 |
# File 'lib/ruby2d/triangle.rb', line 7 def y2 @y2 end |
#y3 ⇒ Object
Returns the value of attribute y3.
7 8 9 |
# File 'lib/ruby2d/triangle.rb', line 7 def y3 @y3 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.
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 |