Class: Ruby2D::Ellipse

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

Overview

An ellipse

Constant Summary

Constants included from Interactive

Interactive::OBJECT_EVENTS, Interactive::OBJECT_EVENT_FILTER_PREDICATES

Instance Attribute Summary collapse

Attributes included from Renderable

#color, #padding_bottom, #padding_left, #padding_right, #padding_top, #visible, #x_align, #y_align, #z

Class Method Summary collapse

Instance Method Summary collapse

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, xradius: 50, yradius: 30, 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) ⇒ Ellipse

Create an ellipse



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
57
# File 'lib/ruby2d/ellipse.rb', line 30

def initialize(x: 0, y: 0, z: 0, xradius: 50, yradius: 30, 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(xradius: xradius, yradius: yradius)
  @x = x
  @y = y
  @z = z
  @xradius = xradius
  @yradius = yradius
  @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

#fillObject

Returns the value of attribute fill.



8
9
10
# File 'lib/ruby2d/ellipse.rb', line 8

def fill
  @fill
end

#rotateObject

Returns the value of attribute rotate.



8
9
10
# File 'lib/ruby2d/ellipse.rb', line 8

def rotate
  @rotate
end

#sectorsObject

Returns the value of attribute sectors.



8
9
10
# File 'lib/ruby2d/ellipse.rb', line 8

def sectors
  @sectors
end

#stroke_colorObject Also known as: stroke_colour

Returns the value of attribute stroke_color.



9
10
11
# File 'lib/ruby2d/ellipse.rb', line 9

def stroke_color
  @stroke_color
end

#stroke_widthObject

Returns the value of attribute stroke_width.



8
9
10
# File 'lib/ruby2d/ellipse.rb', line 8

def stroke_width
  @stroke_width
end

#xObject

Returns the value of attribute x.



9
10
11
# File 'lib/ruby2d/ellipse.rb', line 9

def x
  @x
end

#xradiusObject

Returns the value of attribute xradius.



8
9
10
# File 'lib/ruby2d/ellipse.rb', line 8

def xradius
  @xradius
end

#yObject

Returns the value of attribute y.



9
10
11
# File 'lib/ruby2d/ellipse.rb', line 9

def y
  @y
end

#yradiusObject

Returns the value of attribute yradius.



8
9
10
# File 'lib/ruby2d/ellipse.rb', line 8

def yradius
  @yradius
end

Class Method Details

.render(x: 0, y: 0, xradius: 50, yradius: 30, 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 an ellipse without creating an instance



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
# File 'lib/ruby2d/ellipse.rb', line 130

def self.render(x: 0, y: 0, xradius: 50, yradius: 30, 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 — ellipses
  # 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
  c = Renderable.flatten_color(fill_input, 1, opacity)

  # `rotate` (degrees) tilts the ellipse and, given a pivot other than the
  # center, orbits its center around that pivot — matching Quad/Triangle.
  # `rad` is passed to the draw so the rim itself tilts.
  rad = rotate * Math::PI / 180.0
  if rotate != 0
    cx = rx || x
    cy = ry || y
    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

  Ext.draw_ellipse(x, y, xradius, yradius, rad, sectors, c[0], c[1], c[2], c[3]) if fill
  # 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_ellipse(x, y, xradius, yradius, rad, sectors, stroke_width, sc.r, sc.g, sc.b, sc.a)
  end
end

Instance Method Details

#color=(color) ⇒ Object

Set the fill color. Single-color only — a per-vertex color array is rejected with a clear message rather than a generic "not a valid color".



90
91
92
93
94
95
96
97
98
# File 'lib/ruby2d/ellipse.rb', line 90

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
end

#contains?(x, y) ⇒ Boolean

Check if the ellipse contains the given point

Returns:

  • (Boolean)


113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/ruby2d/ellipse.rb', line 113

def contains?(x, y)
  x, y = _unrotate(x, y)
  dx = x - @x
  dy = y - @y
  rx = @xradius.to_f
  ry = @yradius.to_f
  # A zero radius collapses the ellipse on that axis, so the point must lie
  # exactly on it — mirrors Circle, which is "inside" only at the center
  # when radius is 0. (The plain formula would divide by zero here.)
  return false if rx == 0 && dx != 0
  return false if ry == 0 && dy != 0
  tx = rx == 0 ? 0.0 : (dx * dx) / (rx * rx)
  ty = ry == 0 ? 0.0 : (dy * dy) / (ry * ry)
  tx + ty <= 1.0
end

#heightObject



84
85
86
# File 'lib/ruby2d/ellipse.rb', line 84

def height
  @yradius * 2
end

#rxObject

Get the rotation center x coordinate



60
61
62
# File 'lib/ruby2d/ellipse.rb', line 60

def rx
  @_user_rx.nil? ? @x : @_user_rx
end

#rx=(val) ⇒ Object

Set the rotation center x coordinate



70
71
72
# File 'lib/ruby2d/ellipse.rb', line 70

def rx=(val)
  @_user_rx = val
end

#ryObject

Get the rotation center y coordinate



65
66
67
# File 'lib/ruby2d/ellipse.rb', line 65

def ry
  @_user_ry.nil? ? @y : @_user_ry
end

#ry=(val) ⇒ Object

Set the rotation center y coordinate



75
76
77
# File 'lib/ruby2d/ellipse.rb', line 75

def ry=(val)
  @_user_ry = val
end

#widthObject

Bounding-box width and height (the axis diameters)



80
81
82
# File 'lib/ruby2d/ellipse.rb', line 80

def width
  @xradius * 2
end