Class: Ruby2D::Circle
- Inherits:
-
Object
- Object
- Ruby2D::Circle
- Includes:
- Renderable
- Defined in:
- lib/ruby2d/circle.rb
Overview
A circle
Constant Summary
Constants included from Interactive
Interactive::OBJECT_EVENTS, Interactive::OBJECT_EVENT_FILTER_PREDICATES
Instance Attribute Summary collapse
-
#fill ⇒ Object
Returns the value of attribute fill.
-
#radius ⇒ Object
Returns the value of attribute radius.
-
#rotate ⇒ Object
Returns the value of attribute rotate.
-
#sectors ⇒ Object
Returns the value of attribute sectors.
-
#stroke_color ⇒ Object
(also: #stroke_colour)
Returns the value of attribute stroke_color.
-
#stroke_width ⇒ Object
Returns the value of attribute stroke_width.
-
#x ⇒ Object
Returns the value of attribute x.
-
#y ⇒ Object
Returns the value of attribute y.
Attributes included from Renderable
#color, #padding_bottom, #padding_left, #padding_right, #padding_top, #visible, #x_align, #y_align, #z
Class Method Summary collapse
-
.render(x: 0, y: 0, radius: 50, sectors: 30, rotate: 0, rx: nil, ry: nil, color: nil, colour: nil, opacity: nil, fill: true, stroke_width: 0, stroke_color: nil, stroke_colour: nil) ⇒ Object
Render a circle without creating an instance.
Instance Method Summary collapse
-
#color=(color) ⇒ Object
Set the color value.
-
#contains?(x, y) ⇒ Boolean
Check if the circle contains the given point.
- #height ⇒ Object
-
#initialize(x: 0, y: 0, z: 0, radius: 50, sectors: 30, rotate: 0, rx: nil, ry: nil, color: nil, colour: nil, opacity: nil, fill: true, stroke_width: 0, stroke_color: nil, stroke_colour: nil, add: true, visible: true, padding: nil, padding_top: nil, padding_right: nil, padding_bottom: nil, padding_left: nil) ⇒ Circle
constructor
Create a circle.
-
#rx ⇒ Object
Get the rotation center x coordinate.
-
#rx=(val) ⇒ Object
Set the rotation center x coordinate.
-
#ry ⇒ Object
Get the rotation center y coordinate.
-
#ry=(val) ⇒ Object
Set the rotation center y coordinate.
-
#width ⇒ Object
Bounding-box width and height (the diameter).
Methods included from Renderable
#_apply_padding, #_extract_alignment, #_point_in_polygon?, #_point_on_segment?, #_require_numeric_position, #_resolve_alignment, #_unrotate, #_validate_dimensions, #add, #colour=, flatten_color, flatten_per_vertex, flatten_points, flatten_resolved_color, #hide, #opacity, #opacity=, #padding=, #remove, resolve_color_or_default, resolve_single_color, #show
Methods included from Interactive
#_fire_event, #interactive?, #off, #on
Constructor Details
#initialize(x: 0, y: 0, z: 0, radius: 50, sectors: 30, rotate: 0, rx: nil, ry: nil, color: nil, colour: nil, opacity: nil, fill: true, stroke_width: 0, stroke_color: nil, stroke_colour: nil, add: true, visible: true, padding: nil, padding_top: nil, padding_right: nil, padding_bottom: nil, padding_left: nil) ⇒ Circle
Create a circle
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/ruby2d/circle.rb', line 30 def initialize(x: 0, y: 0, z: 0, radius: 50, sectors: 30, rotate: 0, rx: nil, ry: nil, color: nil, colour: nil, opacity: nil, fill: true, stroke_width: 0, stroke_color: nil, stroke_colour: nil, add: true, visible: true, padding: nil, padding_top: nil, padding_right: nil, padding_bottom: nil, padding_left: nil) x, y = _extract_alignment(x, y) _apply_padding(padding, padding_top, padding_right, padding_bottom, padding_left) _validate_dimensions(radius: radius) @x = x @y = y @z = z @radius = radius @sectors = sectors @rotate = rotate @_user_rx = rx @_user_ry = ry self.color = color || colour || 'white' self.opacity = opacity unless opacity.nil? @fill = fill @stroke_width = stroke_width self.stroke_color = stroke_color || stroke_colour || (color || colour) self.stroke_color.opacity = opacity unless opacity.nil? @visible = visible self.add if add end |
Instance Attribute Details
#fill ⇒ Object
Returns the value of attribute fill.
8 9 10 |
# File 'lib/ruby2d/circle.rb', line 8 def fill @fill end |
#radius ⇒ Object
Returns the value of attribute radius.
8 9 10 |
# File 'lib/ruby2d/circle.rb', line 8 def radius @radius end |
#rotate ⇒ Object
Returns the value of attribute rotate.
8 9 10 |
# File 'lib/ruby2d/circle.rb', line 8 def rotate @rotate end |
#sectors ⇒ Object
Returns the value of attribute sectors.
8 9 10 |
# File 'lib/ruby2d/circle.rb', line 8 def sectors @sectors end |
#stroke_color ⇒ Object Also known as: stroke_colour
Returns the value of attribute stroke_color.
9 10 11 |
# File 'lib/ruby2d/circle.rb', line 9 def stroke_color @stroke_color end |
#stroke_width ⇒ Object
Returns the value of attribute stroke_width.
8 9 10 |
# File 'lib/ruby2d/circle.rb', line 8 def stroke_width @stroke_width end |
#x ⇒ Object
Returns the value of attribute x.
9 10 11 |
# File 'lib/ruby2d/circle.rb', line 9 def x @x end |
#y ⇒ Object
Returns the value of attribute y.
9 10 11 |
# File 'lib/ruby2d/circle.rb', line 9 def y @y end |
Class Method Details
.render(x: 0, y: 0, radius: 50, sectors: 30, rotate: 0, rx: nil, ry: nil, color: nil, colour: nil, opacity: nil, fill: true, stroke_width: 0, stroke_color: nil, stroke_colour: nil) ⇒ Object
Render a circle without creating an instance
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/ruby2d/circle.rb', line 125 def self.render(x: 0, y: 0, radius: 50, sectors: 30, rotate: 0, rx: nil, ry: nil, color: nil, colour: nil, opacity: nil, fill: true, stroke_width: 0, stroke_color: nil, stroke_colour: nil) fill_input = color || colour explicit_stroke = stroke_color || stroke_colour stroke_input = explicit_stroke || fill_input # Reject a per-vertex stroke color, matching the instance setter — circles # have a single stroke color. Only check when the stroke is drawn or an # explicit stroke color was given, so an invalid one still raises. if (stroke_width > 0 || !explicit_stroke.nil?) && !stroke_input.nil? && Color.set(stroke_input).is_a?(Color::Set) raise ArgumentError, "`#{self}` does not support per-vertex stroke colors; pass a single color" end Window.render_ready_check # Resolve the fill color once (as Quad#draw_immediate does). The common # case — a single Color with scalar opacity — reads the resolved # channels directly; only a `Color::Set` or per-vertex opacity array # pays for the flattened array (and gets the arity errors it raises). # `for_render` returns a shared cached instance — read-only here. resolved = Color.for_render(fill_input.nil? ? 'white' : fill_input) uniform = !resolved.is_a?(Color::Set) && !opacity.is_a?(Array) c = uniform ? nil : Renderable.flatten_resolved_color(resolved, 1, opacity, label: self) if rotate != 0 cx = rx || x cy = ry || y rad = rotate * Math::PI / 180.0 sa = Math.sin(rad); ca = Math.cos(rad) dx = x - cx; dy = y - cy x = dx * ca - dy * sa + cx y = dx * sa + dy * ca + cy end if fill if uniform Ext.draw_circle(x, y, radius, sectors, resolved.r, resolved.g, resolved.b, opacity || resolved.a) else Ext.draw_circle(x, y, radius, sectors, c[0], c[1], c[2], c[3]) end end # Resolve the stroke color only when actually stroking. if stroke_width > 0 sc = Renderable.resolve_single_color(stroke_input) || Color.new('white') sc = Color.new(sc) sc.opacity = opacity if opacity Ext.stroke_circle(x, y, radius, sectors, stroke_width, sc.r, sc.g, sc.b, sc.a) end end |
Instance Method Details
#color=(color) ⇒ Object
Set the color value. Single-color only — a per-vertex color array is rejected with a clear message rather than a generic "not a valid color".
101 102 103 104 105 106 107 108 109 |
# File 'lib/ruby2d/circle.rb', line 101 def color=(color) c = Color.set(color) if c.is_a?(Color::Set) raise ArgumentError, "`#{self.class}` does not support per-vertex colors; pass a single color" end @color = c @_cc = nil # invalidate cached color components end |
#contains?(x, y) ⇒ Boolean
Check if the circle contains the given point. Compares squared distance
to squared radius to skip the square root on this per-event hit path. A
negative radius (reachable via the unguarded runtime setter) contains
nothing, matching the old sqrt(...) <= @radius behavior the squared form
would otherwise lose.
116 117 118 119 120 121 122 |
# File 'lib/ruby2d/circle.rb', line 116 def contains?(x, y) return false if @radius.negative? x, y = _unrotate(x, y) if @rotate != 0 dx = x - @x dy = y - @y dx * dx + dy * dy <= @radius * @radius end |
#height ⇒ Object
95 96 97 |
# File 'lib/ruby2d/circle.rb', line 95 def height @radius * 2 end |
#rx ⇒ Object
Get the rotation center x coordinate
71 72 73 |
# File 'lib/ruby2d/circle.rb', line 71 def rx @_user_rx.nil? ? @x : @_user_rx end |
#rx=(val) ⇒ Object
Set the rotation center x coordinate
81 82 83 |
# File 'lib/ruby2d/circle.rb', line 81 def rx=(val) @_user_rx = val end |
#ry ⇒ Object
Get the rotation center y coordinate
76 77 78 |
# File 'lib/ruby2d/circle.rb', line 76 def ry @_user_ry.nil? ? @y : @_user_ry end |
#ry=(val) ⇒ Object
Set the rotation center y coordinate
86 87 88 |
# File 'lib/ruby2d/circle.rb', line 86 def ry=(val) @_user_ry = val end |
#width ⇒ Object
Bounding-box width and height (the diameter)
91 92 93 |
# File 'lib/ruby2d/circle.rb', line 91 def width @radius * 2 end |