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

#z

Instance Method Summary collapse

Methods included from Renderable

#add, #opacity, #opacity=, #remove

Constructor Details

#initialize(opts = {}) ⇒ Quad

Returns a new instance of Quad.



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

def initialize(opts = {})
  @type_id = 2

  @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'
  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



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

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



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

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



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

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



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

def c4
  @c4
end

#colorObject

Returns the value of attribute color.



16
17
18
# File 'lib/ruby2d/quad.rb', line 16

def color
  @color
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



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

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



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

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



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

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



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

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



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

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



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

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



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

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



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

def y4
  @y4
end

Instance Method Details

#contains?(x, y) ⇒ Boolean

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

Returns:

  • (Boolean)


43
44
45
46
47
48
49
50
51
52
53
# File 'lib/ruby2d/quad.rb', line 43

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