Class: PerfectShape::Ellipse

Inherits:
Arc
  • Object
show all
Defined in:
lib/perfect_shape/ellipse.rb

Direct Known Subclasses

Circle

Constant Summary collapse

MESSAGE_CANNOT_UPDATE_ATTRIUBTE =
"Ellipse %s cannot be updated. If you want to update type, use Arc instead!"

Constants inherited from Arc

Arc::DEFAULT_OUTLINE_RADIUS, Arc::TYPES

Instance Attribute Summary

Attributes inherited from Arc

#extent, #start, #type

Attributes included from RectangularShape

#height, #width

Attributes included from PointLocation

#x, #y

Instance Method Summary collapse

Methods inherited from Arc

#btan, #center_x, #center_x=, #center_y, #center_y=, #contain_angle?, #end_point, #height, #height=, #intersect?, #radius_x, #radius_x=, #radius_y, #radius_y=, #start_point, #to_path_shapes, #width, #width=, #x, #x=, #y, #y=

Methods included from RectangularShape

#max_x, #max_y

Methods included from PointLocation

#first_point, #min_x, #min_y

Methods inherited from Shape

#==, #bounding_box, #center_point, #center_x, #center_y, #height, #max_x, #max_y, #min_x, #min_y, #width

Constructor Details

#initialize(x: 0, y: 0, width: 1, height: 1, center_x: nil, center_y: nil, radius_x: nil, radius_y: nil) ⇒ Ellipse

Returns a new instance of Ellipse.



28
29
30
31
# File 'lib/perfect_shape/ellipse.rb', line 28

def initialize(x: 0, y: 0, width: 1, height: 1, center_x: nil, center_y: nil, radius_x: nil, radius_y: nil)
  super
  @initialized = true
end

Instance Method Details

#contain?(x_or_point, y = nil, outline: false, distance_tolerance: 0) ⇒ Boolean

Checks if ellipse contains point (two-number Array or x, y args)

the ellipse, false if the point lies outside of the ellipseā€™s bounds.

Parameters:

  • x

    The X coordinate of the point to test.

  • y (defaults to: nil)

    The Y coordinate of the point to test.

Returns:

  • (Boolean)

    true if the point lies within the bound of



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/perfect_shape/ellipse.rb', line 65

def contain?(x_or_point, y = nil, outline: false, distance_tolerance: 0)
  # This is implemented again even though super would have just worked to have an optimized algorithm for Ellipse.
  x, y = Point.normalize_point(x_or_point, y)
  return unless x && y
  if outline
    super(x, y, outline: true, distance_tolerance: distance_tolerance)
  else
    ellw = self.width
    return false if ellw <= 0.0
    normx = (x - self.x) / ellw - 0.5
    ellh = self.height
    return false if ellh <= 0.0
    normy = (y - self.y) / ellh - 0.5
    (normx * normx + normy * normy) < 0.25
  end
end

#extent=(value) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/perfect_shape/ellipse.rb', line 49

def extent=(value)
  if @initialized
    raise MESSAGE_CANNOT_UPDATE_ATTRIUBTE % 'extent'
  else
    super
  end
end

#start=(value) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/perfect_shape/ellipse.rb', line 41

def start=(value)
  if @initialized
    raise MESSAGE_CANNOT_UPDATE_ATTRIUBTE % 'start'
  else
    super
  end
end

#type=(value) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/perfect_shape/ellipse.rb', line 33

def type=(value)
  if @initialized
    raise MESSAGE_CANNOT_UPDATE_ATTRIUBTE % 'type'
  else
    super
  end
end