Class: Ruby2D::Quad

Inherits:
Object
  • Object
show all
Includes:
Renderable
Defined in:
lib/ruby2d/quad.rb,
ext/ruby2d/ruby2d-opal.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
# File 'lib/ruby2d/quad.rb', line 18

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


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

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

#ext_renderObject



117
118
119
120
121
122
123
124
# File 'ext/ruby2d/ruby2d-opal.rb', line 117

def ext_render
  `S2D.DrawQuad(
    #{self}.x1, #{self}.y1, #{self}.c1.r, #{self}.c1.g, #{self}.c1.b, #{self}.c1.a,
    #{self}.x2, #{self}.y2, #{self}.c2.r, #{self}.c2.g, #{self}.c2.b, #{self}.c2.a,
    #{self}.x3, #{self}.y3, #{self}.c3.r, #{self}.c3.g, #{self}.c3.b, #{self}.c3.a,
    #{self}.x4, #{self}.y4, #{self}.c4.r, #{self}.c4.g, #{self}.c4.b, #{self}.c4.a
  );`
end