Class: Ruby2D::Quad

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

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(opts = {}) ⇒ Quad

Returns a new instance of Quad.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/ruby2d/quad.rb', line 17

def initialize(opts = {})
  @x1 = opts[:x1] || 0
  @y1 = opts[:y1] || 0
  @x2 = opts[:x2] || 100
  @y2 = opts[:y2] || 0
  @x3 = opts[:x3] || 100
  @y3 = opts[:y3] || 100
  @x4 = opts[:x4] || 0
  @y4 = opts[:y4] || 100
  @z  = opts[:z]  || 0
  self.color = opts[:color] || 'white'
  self.color.opacity = opts[:opacity] if opts[:opacity]
  add
end

Instance Attribute Details

#c1Object

Coordinates in clockwise order, starting at top left: x1,y1 == top left x2,y2 == top right x3,y3 == bottom right x4,y4 == bottom left



12
13
14
# File 'lib/ruby2d/quad.rb', line 12

def c1
  @c1
end

#c2Object

Coordinates in clockwise order, starting at top left: x1,y1 == top left x2,y2 == top right x3,y3 == bottom right x4,y4 == bottom left



12
13
14
# File 'lib/ruby2d/quad.rb', line 12

def c2
  @c2
end

#c3Object

Coordinates in clockwise order, starting at top left: x1,y1 == top left x2,y2 == top right x3,y3 == bottom right x4,y4 == bottom left



12
13
14
# File 'lib/ruby2d/quad.rb', line 12

def c3
  @c3
end

#c4Object

Coordinates in clockwise order, starting at top left: x1,y1 == top left x2,y2 == top right x3,y3 == bottom right x4,y4 == bottom left



12
13
14
# File 'lib/ruby2d/quad.rb', line 12

def c4
  @c4
end

#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



12
13
14
# File 'lib/ruby2d/quad.rb', line 12

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



12
13
14
# File 'lib/ruby2d/quad.rb', line 12

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



12
13
14
# File 'lib/ruby2d/quad.rb', line 12

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



12
13
14
# File 'lib/ruby2d/quad.rb', line 12

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



12
13
14
# File 'lib/ruby2d/quad.rb', line 12

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



12
13
14
# File 'lib/ruby2d/quad.rb', line 12

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



12
13
14
# File 'lib/ruby2d/quad.rb', line 12

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



12
13
14
# File 'lib/ruby2d/quad.rb', line 12

def y4
  @y4
end

Class Method Details

.draw(opts = {}) ⇒ Object



51
52
53
54
55
56
57
58
59
60
# File 'lib/ruby2d/quad.rb', line 51

def self.draw(opts = {})
  Window.render_ready_check

  ext_draw([
    opts[:x1], opts[:y1], opts[:color][0][0], opts[:color][0][1], opts[:color][0][2], opts[:color][0][3],
    opts[:x2], opts[:y2], opts[:color][1][0], opts[:color][1][1], opts[:color][1][2], opts[:color][1][3],
    opts[:x3], opts[:y3], opts[:color][2][0], opts[:color][2][1], opts[:color][2][2], opts[:color][2][3],
    opts[:x4], opts[:y4], opts[:color][3][0], opts[:color][3][1], opts[:color][3][2], opts[:color][3][3]
  ])
end

Instance Method Details

#color=(c) ⇒ Object



32
33
34
35
# File 'lib/ruby2d/quad.rb', line 32

def color=(c)
  @color = Color.set(c)
  update_color(@color)
end

#contains?(x, y) ⇒ Boolean

The logic is the same as for a triangle See triangle.rb for reference

Returns:

  • (Boolean)


39
40
41
42
43
44
45
46
47
48
49
# File 'lib/ruby2d/quad.rb', line 39

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