Class: Ruby2D::Quad

Inherits:
Object
  • Object
show all
Includes:
Renderable
Defined in:
lib/ruby2d/quad.rb

Overview

A quadrilateral based on four points in clockwise order starting at the top left.

Direct Known Subclasses

Rectangle

Instance Attribute Summary collapse

Attributes included from Renderable

#color, #height, #width, #x, #y, #z

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Renderable

#add, #remove

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

Parameters:

  • x1 (Numeric) (defaults to: 0)
  • y1 (Numeric) (defaults to: 0)
  • x2 (Numeric) (defaults to: 100)
  • y2 (Numeric) (defaults to: 0)
  • x3 (Numeric) (defaults to: 100)
  • y3 (Numeric) (defaults to: 100)
  • x4 (Numeric) (defaults to: 0)
  • y4 (Numeric) (defaults to: 100)
  • z (Numeric) (defaults to: 0)
  • color (String, Array) (defaults to: nil)

    A single colour or an array of exactly 4 colours

  • opacity (Numeric) (defaults to: nil)

    Opacity of the image when rendering

Raises:

  • (ArgumentError)

    if an array of colours does not have 4 entries



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

#x1Object

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

#x2Object

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

#x3Object

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

#x4Object

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

#y1Object

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

#y2Object

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

#y3Object

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

#y4Object

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

Parameters:

  • x1 (Numeric)
  • y1 (Numeric)
  • x2 (Numeric)
  • y2 (Numeric)
  • x3 (Numeric)
  • y3 (Numeric)
  • x4 (Numeric)
  • y4 (Numeric)
  • color (Array<Array<float,float,float,float>>)

    An array of 4 array of colour components (e.g. [[1.0, 0, 0, 1.0], …])



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

Parameters:

  • color (String, Array)

    A single colour or an array of exactly 4 colours

Raises:

  • (ArgumentError)

    if an array of colours does not have 4 entries



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

Returns:

  • (Boolean)


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