Class: Ruby2D::Quad
Overview
A quadrilateral based on four points in clockwise order starting at the top left.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#x1 ⇒ Object
Coordinates in clockwise order, starting at top left: x1,y1 == top left x2,y2 == top right x3,y3 == bottom right x4,y4 == bottom left.
-
#x2 ⇒ Object
Coordinates in clockwise order, starting at top left: x1,y1 == top left x2,y2 == top right x3,y3 == bottom right x4,y4 == bottom left.
-
#x3 ⇒ Object
Coordinates in clockwise order, starting at top left: x1,y1 == top left x2,y2 == top right x3,y3 == bottom right x4,y4 == bottom left.
-
#x4 ⇒ Object
Coordinates in clockwise order, starting at top left: x1,y1 == top left x2,y2 == top right x3,y3 == bottom right x4,y4 == bottom left.
-
#y1 ⇒ Object
Coordinates in clockwise order, starting at top left: x1,y1 == top left x2,y2 == top right x3,y3 == bottom right x4,y4 == bottom left.
-
#y2 ⇒ Object
Coordinates in clockwise order, starting at top left: x1,y1 == top left x2,y2 == top right x3,y3 == bottom right x4,y4 == bottom left.
-
#y3 ⇒ Object
Coordinates in clockwise order, starting at top left: x1,y1 == top left x2,y2 == top right x3,y3 == bottom right x4,y4 == bottom left.
-
#y4 ⇒ Object
Coordinates in clockwise order, starting at top left: x1,y1 == top left x2,y2 == top right x3,y3 == bottom right x4,y4 == bottom left.
Attributes included from Renderable
#color, #height, #width, #x, #y, #z
Class Method Summary collapse
-
.draw(x1:, y1:, x2:, y2:, x3:, y3:, x4:, y4:, color:) ⇒ Object
Draw a line without creating a Line.
Instance Method Summary collapse
-
#color=(color) ⇒ Object
Change the colour of the line.
-
#contains?(x, y) ⇒ Boolean
The logic is the same as for a triangle See triangle.rb for reference.
-
#initialize(x1: 0, y1: 0, x2: 100, y2: 0, x3: 100, y3: 100, x4: 0, y4: 100, z: 0, color: nil, colour: nil, opacity: nil) ⇒ Quad
constructor
Create an quadrilateral.
Methods included from Renderable
Constructor Details
#initialize(x1: 0, y1: 0, x2: 100, y2: 0, x3: 100, y3: 100, x4: 0, y4: 100, z: 0, color: nil, colour: nil, opacity: nil) ⇒ Quad
Create an quadrilateral
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/ruby2d/quad.rb', line 33 def initialize(x1: 0, y1: 0, x2: 100, y2: 0, x3: 100, y3: 100, x4: 0, y4: 100, z: 0, color: nil, colour: nil, opacity: nil) @x1 = x1 @y1 = y1 @x2 = x2 @y2 = y2 @x3 = x3 @y3 = y3 @x4 = x4 @y4 = y4 @z = z self.color = color || colour || 'white' self.color.opacity = opacity unless opacity.nil? add end |
Instance Attribute Details
#x1 ⇒ Object
Coordinates in clockwise order, starting at top left: x1,y1 == top left x2,y2 == top right x3,y3 == bottom right x4,y4 == bottom left
15 16 17 |
# File 'lib/ruby2d/quad.rb', line 15 def x1 @x1 end |
#x2 ⇒ Object
Coordinates in clockwise order, starting at top left: x1,y1 == top left x2,y2 == top right x3,y3 == bottom right x4,y4 == bottom left
15 16 17 |
# File 'lib/ruby2d/quad.rb', line 15 def x2 @x2 end |
#x3 ⇒ Object
Coordinates in clockwise order, starting at top left: x1,y1 == top left x2,y2 == top right x3,y3 == bottom right x4,y4 == bottom left
15 16 17 |
# File 'lib/ruby2d/quad.rb', line 15 def x3 @x3 end |
#x4 ⇒ Object
Coordinates in clockwise order, starting at top left: x1,y1 == top left x2,y2 == top right x3,y3 == bottom right x4,y4 == bottom left
15 16 17 |
# File 'lib/ruby2d/quad.rb', line 15 def x4 @x4 end |
#y1 ⇒ Object
Coordinates in clockwise order, starting at top left: x1,y1 == top left x2,y2 == top right x3,y3 == bottom right x4,y4 == bottom left
15 16 17 |
# File 'lib/ruby2d/quad.rb', line 15 def y1 @y1 end |
#y2 ⇒ Object
Coordinates in clockwise order, starting at top left: x1,y1 == top left x2,y2 == top right x3,y3 == bottom right x4,y4 == bottom left
15 16 17 |
# File 'lib/ruby2d/quad.rb', line 15 def y2 @y2 end |
#y3 ⇒ Object
Coordinates in clockwise order, starting at top left: x1,y1 == top left x2,y2 == top right x3,y3 == bottom right x4,y4 == bottom left
15 16 17 |
# File 'lib/ruby2d/quad.rb', line 15 def y3 @y3 end |
#y4 ⇒ Object
Coordinates in clockwise order, starting at top left: x1,y1 == top left x2,y2 == top right x3,y3 == bottom right x4,y4 == bottom left
15 16 17 |
# File 'lib/ruby2d/quad.rb', line 15 def y4 @y4 end |
Class Method Details
.draw(x1:, y1:, x2:, y2:, x3:, y3:, x4:, y4:, color:) ⇒ Object
Draw a line without creating a Line
91 92 93 94 95 96 97 98 99 |
# File 'lib/ruby2d/quad.rb', line 91 def self.draw(x1:, y1:, x2:, y2:, x3:, y3:, x4:, y4:, color:) Window.render_ready_check ext_draw([ x1, y1, *color[0], # splat the colour components x2, y2, *color[1], x3, y3, *color[2], x4, y4, *color[3] ]) end |
Instance Method Details
#color=(color) ⇒ Object
Change the colour of the line
52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/ruby2d/quad.rb', line 52 def color=(color) # convert to Color or Color::Set color = Color.set(color) # require 4 colours if multiple colours provided if color.is_a?(Color::Set) && color.length != 4 raise ArgumentError, "`#{self.class}` requires 4 colors, one for each vertex. #{color.length} were given." end @color = color # converted above invalidate_color_components end |
#contains?(x, y) ⇒ Boolean
The logic is the same as for a triangle See triangle.rb for reference
68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/ruby2d/quad.rb', line 68 def contains?(x, y) self_area = triangle_area(@x1, @y1, @x2, @y2, @x3, @y3) + triangle_area(@x1, @y1, @x3, @y3, @x4, @y4) questioned_area = triangle_area(@x1, @y1, @x2, @y2, x, y) + triangle_area(@x2, @y2, @x3, @y3, x, y) + triangle_area(@x3, @y3, @x4, @y4, x, y) + triangle_area(@x4, @y4, @x1, @y1, x, y) questioned_area <= self_area end |