Class: Ruby2D::Circle

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

Overview

Create a circle using Circle.new

Instance Attribute Summary collapse

Attributes included from Renderable

#color, #height, #width, #z

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Renderable

#add, #remove

Constructor Details

#initialize(x: 25, y: 25, z: 0, radius: 50, sectors: 30, color: nil, colour: nil, opacity: nil) ⇒ Circle

Create a circle

Parameters:

  • x (Numeric) (defaults to: 25)
  • y (Numeric) (defaults to: 25)
  • z (Numeric) (defaults to: 0)
  • radius (Numeric) (defaults to: 50)
  • sectors (Numeric) (defaults to: 30)

    Smoothness of the circle is better when more sectors are used.

  • color (String | Color) (defaults to: nil)

    Or colour

  • opacity (Float) (defaults to: nil)


23
24
25
26
27
28
29
30
31
32
33
# File 'lib/ruby2d/circle.rb', line 23

def initialize(x: 25, y: 25, z: 0, radius: 50, sectors: 30,
               color: nil, colour: nil, opacity: nil)
  @x = x
  @y = y
  @z = z
  @radius = radius
  @sectors = sectors
  self.color = color || colour || 'white'
  self.color.opacity = opacity unless opacity.nil?
  add
end

Instance Attribute Details

#radiusObject

Returns the value of attribute radius.



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

def radius
  @radius
end

#sectorsObject

Returns the value of attribute sectors.



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

def sectors
  @sectors
end

#xObject

Returns the value of attribute x.



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

def x
  @x
end

#yObject

Returns the value of attribute y.



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

def y
  @y
end

Class Method Details

.draw(opts = {}) ⇒ Object



40
41
42
43
44
45
46
47
# File 'lib/ruby2d/circle.rb', line 40

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

  ext_draw([
             opts[:x], opts[:y], opts[:radius], opts[:sectors],
             opts[:color][0], opts[:color][1], opts[:color][2], opts[:color][3]
           ])
end

Instance Method Details

#contains?(x, y) ⇒ Boolean

Check if the circle contains the point at (x, y)

Returns:

  • (Boolean)


36
37
38
# File 'lib/ruby2d/circle.rb', line 36

def contains?(x, y)
  Math.sqrt((x - @x)**2 + (y - @y)**2) <= @radius
end