Class: Ruby2D::Triangle
- Inherits:
-
Object
- Object
- Ruby2D::Triangle
- Includes:
- Renderable
- Defined in:
- lib/ruby2d/triangle.rb
Overview
A triangle
Instance Attribute Summary collapse
-
#color ⇒ Object
Returns the value of attribute color.
-
#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
Class Method Summary collapse
-
.draw(x1:, y1:, x2:, y2:, x3:, y3:, color:) ⇒ Object
Draw a triangle.
Instance Method Summary collapse
-
#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(x1: 50, y1: 0, x2: 100, y2: 100, x3: 0, y3: 100, z: 0, color: 'white', colour: nil, opacity: nil) ⇒ Triangle
constructor
Create a triangle.
Methods included from Renderable
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
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
#color ⇒ Object
Returns the value of attribute color.
13 14 15 |
# File 'lib/ruby2d/triangle.rb', line 13 def color @color end |
#x1 ⇒ Object
Returns the value of attribute x1.
10 11 12 |
# File 'lib/ruby2d/triangle.rb', line 10 def x1 @x1 end |
#x2 ⇒ Object
Returns the value of attribute x2.
10 11 12 |
# File 'lib/ruby2d/triangle.rb', line 10 def x2 @x2 end |
#x3 ⇒ Object
Returns the value of attribute x3.
10 11 12 |
# File 'lib/ruby2d/triangle.rb', line 10 def x3 @x3 end |
#y1 ⇒ Object
Returns the value of attribute y1.
10 11 12 |
# File 'lib/ruby2d/triangle.rb', line 10 def y1 @y1 end |
#y2 ⇒ Object
Returns the value of attribute y2.
10 11 12 |
# File 'lib/ruby2d/triangle.rb', line 10 def y2 @y2 end |
#y3 ⇒ Object
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
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.
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 |