Class: Circle

Inherits:
Object
  • Object
show all
Defined in:
src/ruby/models/circle.rb,
mrb_doc/models/circle.rb

Overview

The Circle class is used for drawing circles.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(x:, y:, radius:, colour: Colour::BLACK, outline: nil, thickness: 1, gradient: nil) ⇒ Circle

Creates a new instance of Circle.

If outline is specified then the circle will be drawn with an outline of that Colour. You can specify the thickness to make it thicker.

if gradient is specified then the circle will be drawn with a gradient between colour and gradient.

Examples:

Basic usage

circle = Circle.new(x: 100, y: 100, radius: 5, colour:Colour::RED)

Parameters:

  • x (Float)
  • y (Float)
  • radius (Float)
  • colour (Colour) (defaults to: Colour::BLACK)
  • outline (Colour) (defaults to: nil)
  • thickness (Float) (defaults to: 1)
  • gradient (Colour) (defaults to: nil)


27
28
29
30
# File 'mrb_doc/models/circle.rb', line 27

def initialize(x:, y:, radius:, colour: Colour::BLACK, outline: nil, thickness: 1, gradient: nil)
  # mrb_Circle_initialize
  # src/mruby_integration/models/circle.cpp
end

Instance Attribute Details

#colourColour

Returns:



4
5
6
# File 'src/ruby/models/circle.rb', line 4

def colour
  @colour
end

#gradientColour

Returns:



4
5
6
# File 'src/ruby/models/circle.rb', line 4

def gradient
  @gradient
end

#outlineColour

Returns:



4
5
6
# File 'src/ruby/models/circle.rb', line 4

def outline
  @outline
end

#radiusFloat

Returns:

  • (Float)


3
4
5
# File 'mrb_doc/models/circle.rb', line 3

def radius
  @radius
end

#thicknessFloat

Returns:

  • (Float)


3
4
5
# File 'mrb_doc/models/circle.rb', line 3

def thickness
  @thickness
end

#xFloat

Returns:

  • (Float)


3
4
5
# File 'mrb_doc/models/circle.rb', line 3

def x
  @x
end

#yFloat

Returns:

  • (Float)


3
4
5
# File 'mrb_doc/models/circle.rb', line 3

def y
  @y
end

Class Method Details

.[](x, y, radius, colour = Colour::BLACK) ⇒ Circle

A short form way to create new Circle objects.

Examples:

Basic usage

circle = Circle[120, 200, 50, Colour::GREEN]

Parameters:

  • x (Float)
  • y (Float)
  • radius (Float)
  • colour (Colour) (defaults to: Colour::BLACK)

Returns:



16
17
18
# File 'src/ruby/models/circle.rb', line 16

def self.[](x, y, radius, colour = Colour::BLACK)
  new(x: x, y: y, radius: radius, colour: colour)
end

Instance Method Details

#drawnil

Draws the Circle.

If outline is specified then the circle will be drawn with an outline of that Colour. You can specify the thickness to make it thicker.

if gradient is specified then the circle will be drawn with a gradient between colour and gradient.

Examples:

Basic usage

circle = Circle.new(x: 100, y: 100, radius: 5, colour:Colour::RED)
circle.draw

circle_with_gradient = Circle.new(x: 100, y: 100, radius: 5, colour:Colour::RED, gradient: Colour::GREEN)
circle_with_gradient.draw

circle_with_outline = Circle.new(x: 100, y: 100, radius: 5, colour:Colour::RED, outline: Colour::BLUE, thickness: 3)
circle_with_outline.draw

circle_with_gradient_and_outline = Circle.new(x: 100, y: 100, radius: 5, colour:Colour::RED, gradient: Colour::GREEN, outline: Colour::BLUE, thickness: 3)
circle_with_gradient_and_outline.draw

Returns:

  • (nil)


54
55
56
57
58
# File 'mrb_doc/models/circle.rb', line 54

def draw
  # mrb_Circle_draw
  # src/mruby_integration/models/circle.cpp
  nil
end

#overlaps?(other) ⇒ Boolean

Returns true if the Vector2 is contained inside this Circle or is on the border of.

Examples:

Basic usage

hitbox = Circle.new(x: 10, y: 10, radius: 10)
position = Vector2.new(x: 0, y: 0)

puts hitbox.overlaps?(position)
# => false

# The position changes to inside the circle...
position = Vector2.new(x: 15, y: 15)

puts hitbox.overlaps?(position)
# => true

# The position changes to the edge of the circle...
position = Vector2.new(x: 0, y: 10)

puts hitbox.overlaps?(position)
# => true

# The position changes to far outside circle...
position = Vector2.new(x: 100, y: 100)

puts hitbox.overlaps?(position)
# => false

Parameters:

Returns:

  • (Boolean)

Raises:

  • (ArgumentError)

    If passed anything other than a Vector2



85
86
87
88
89
90
91
92
93
94
# File 'src/ruby/models/circle.rb', line 85

def overlaps?(other)
  raise ArgumentError, "Must pass in a Vector2" unless other.is_a?(Vector2)

  x_distance = x - other.x
  y_distance = y - other.y

  distance = Math.sqrt((x_distance**2) + (y_distance**2))

  distance <= radius
end

#to_hHash

Return the Circle represented as a Hash

Examples:

Basic usage

circle = Circle.new(x: 50, y: 60, radius: 10, colour: Colour[128, 0, 255, 255])

p circle.to_h
# => {
#      x: 50,
#      y: 60,
#      radius: 10,
#      colour: {
#        red: 128,
#        green: 0,
#        blue: 255,
#        alpha: 255,
#      },
#      outline: nil,
#      thickness: 1,
#      gradient: nil,
#    }

Returns:

  • (Hash)


42
43
44
45
46
47
48
49
50
51
52
# File 'src/ruby/models/circle.rb', line 42

def to_h
  {
    x: x,
    y: y,
    radius: radius,
    colour: colour&.to_h,
    thickness: thickness,
    outline: outline&.to_h,
    gradient: gradient&.to_h
  }
end