Class: Cartesius::Ellipse

Inherits:
Object
  • Object
show all
Includes:
Determinator, Numerificator
Defined in:
lib/cartesius/ellipse.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(x2:, y2:, x:, y:, k:) ⇒ Ellipse

Conic Conic equation type: ax^2 + by^2 + dx + ey + f = 0



13
14
15
16
# File 'lib/cartesius/ellipse.rb', line 13

def initialize(x2:, y2:, x:, y:, k:)
  @x2_coeff, @y2_coeff, @x_coeff, @y_coeff, @k_coeff = normalize(x2, y2, x, y, k)
  validation
end

Class Method Details

.by_axes(major_axis:, minor_axis:) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/cartesius/ellipse.rb', line 46

def self.by_axes(major_axis:, minor_axis:)
  if major_axis == minor_axis
    raise ArgumentError.new('Axes must be different!')
  end

  if major_axis.inclined? or minor_axis.inclined?
    raise ArgumentError.new('Axes must not be inclined!')
  end

  if major_axis.horizontal? and minor_axis.horizontal?
    raise ArgumentError.new('Axes can not be both horizontal!')
  end

  if major_axis.vertical? and minor_axis.vertical?
    raise ArgumentError.new('Axes can not be both vertical!')
  end

  if major_axis.mid != minor_axis.mid
    raise ArgumentError.new('Axes must be the same mid point!')
  end

  if major_axis.horizontal?
    a2 = Rational(major_axis.length, 2) ** 2
    b2 = Rational(minor_axis.length, 2) ** 2
  else
    a2 = Rational(minor_axis.length, 2) ** 2
    b2 = Rational(major_axis.length, 2) ** 2
  end

  center = major_axis.mid

  build_by(a2, b2, center)
end

.by_definition(focus1:, focus2:, distance:) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/cartesius/ellipse.rb', line 18

def self.by_definition(focus1:, focus2:, distance:)
  if focus1 == focus2
    raise ArgumentError.new('Focus points must be different!')
  end

  focal_axis = Segment.new(extreme1: focus1, extreme2: focus2)
  if focal_axis.inclined?
    raise ArgumentError.new('Focal axis must not be inclined!')
  end

  if distance <= focal_axis.length
    raise ArgumentError.new('Sum of distances must be greater than focal distance!')
  end

  c2 = Rational(focal_axis.length, 2) ** 2
  if focal_axis.horizontal?
    a2 = Rational(distance, 2) ** 2
    b2 = a2 - c2
  else
    b2 = Rational(distance, 2) ** 2
    a2 = b2 - c2
  end

  center = focal_axis.mid

  build_by(a2, b2, center)
end

.by_points(center:, point1:, point2:) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/cartesius/ellipse.rb', line 80

def self.by_points(center:, point1:, point2:)
  if center == point1 or center == point2 or point1 == point2
    raise ArgumentError.new('Points must be different!')
  end

  shifted1 = Point.new(x: point1.x - center.x, y: point1.y - center.y)
  shifted2 = Point.new(x: point2.x - center.x, y: point2.y - center.y)

  begin
    alfa, beta = Cramer.solution2(
        [shifted1.x ** 2, shifted1.y ** 2],
        [shifted2.x ** 2, shifted2.y ** 2],
        [1, 1]
    )
  rescue
    raise ArgumentError.new('Center and points are not valid!')
  end

  a2 = Rational(1, alfa)
  b2 = Rational(1, beta)

  build_by(a2, b2, center)
end

Instance Method Details

#==(ellipse) ⇒ Object



158
159
160
161
# File 'lib/cartesius/ellipse.rb', line 158

def == (ellipse)
  ellipse.instance_of?(self.class) and
      ellipse.focus1 == focus1 and ellipse.focus2 == focus2 and ellipse.distance == distance
end

#congruent?(ellipse) ⇒ Boolean

Returns:

  • (Boolean)


153
154
155
156
# File 'lib/cartesius/ellipse.rb', line 153

def congruent?(ellipse)
  ellipse.instance_of?(self.class) and
      ellipse.eccentricity == eccentricity
end

#distanceObject



118
119
120
# File 'lib/cartesius/ellipse.rb', line 118

def distance
  discriminating(lambda {2 * a}, lambda {2 * b})
end

#eccentricityObject



149
150
151
# File 'lib/cartesius/ellipse.rb', line 149

def eccentricity
  Rational(focal_axis.length, major_axis.length)
end

#focal_axisObject



122
123
124
# File 'lib/cartesius/ellipse.rb', line 122

def focal_axis
  Segment.new(extreme1: focus1, extreme2: focus2)
end

#focus1Object



104
105
106
107
108
109
# File 'lib/cartesius/ellipse.rb', line 104

def focus1
  discriminating(
      lambda {Point.new(x: center.x + cx, y: center.y)},
      lambda {Point.new(x: center.x, y: center.y + cy)}
  )
end

#focus2Object



111
112
113
114
115
116
# File 'lib/cartesius/ellipse.rb', line 111

def focus2
  discriminating(
      lambda {Point.new(x: center.x - cx, y: center.y)},
      lambda {Point.new(x: center.x, y: center.y - cy)}
  )
end

#major_axisObject



126
127
128
129
130
131
# File 'lib/cartesius/ellipse.rb', line 126

def major_axis
  discriminating(
      lambda {Segment.new(extreme1: Point.new(x: center.x - a, y: center.y), extreme2: Point.new(x: center.x + a, y: center.y))},
      lambda {Segment.new(extreme1: Point.new(x: center.x, y: center.y - b), extreme2: Point.new(x: center.x, y: center.y + b))},
      )
end

#minor_axisObject



133
134
135
136
137
138
# File 'lib/cartesius/ellipse.rb', line 133

def minor_axis
  discriminating(
      lambda {Segment.new(extreme1: Point.new(x: center.x, y: center.y - b), extreme2: Point.new(x: center.x, y: center.y + b))},
      lambda {Segment.new(extreme1: Point.new(x: center.x - a, y: center.y), extreme2: Point.new(x: center.x + a, y: center.y))}
  )
end

#verticesObject



140
141
142
143
144
145
146
147
# File 'lib/cartesius/ellipse.rb', line 140

def vertices
  [
      Point.new(x: center.x + a, y: center.y),
      Point.new(x: center.x, y: center.y - b),
      Point.new(x: center.x - a, y: center.y),
      Point.new(x: center.x, y: center.y + b)
  ]
end