-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathellipse.cpp
41 lines (34 loc) · 1.06 KB
/
ellipse.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
//
// Created by Arias Arevalo, Carlos on 4/22/20.
//
#include "ellipse.h"
#include <string>
#include <sstream>
#include <cmath>
using std::string;
using std::stringstream;
Ellipse::Ellipse(double radiusA, double radiusB)
: _radiusA(radiusA), _radiusB(radiusB) {
}
string Ellipse::ToString() const {
stringstream retVal;
retVal << "{Ellipse " << Shape::ToString()
<< ", a: " << _radiusA << ", b: " << _radiusB << "}";
return retVal.str();
}
double Ellipse::Area() const {
return PI * _radiusA * _radiusB;
}
/**
* Calculates the Perimeter of the Ellipse.
* Using: https://www.mathsisfun.com/geometry/ellipse-perimeter.html
* This method will implement the third approximation (second by Ramanujan)
* @return an approximate measure of the perimeter of the ellipse
*/
double Ellipse::Perimeter() const {
double h = pow(_radiusA - _radiusB, 2.0) / pow(_radiusA + _radiusB, 2.0);
return PI * (_radiusA + _radiusB) * (1 + ((3 * h) / (10 + sqrt(4 - 3 * h))));
}
double Ellipse::Eccentricity() const {
return sqrt(pow(_radiusA, 2.0) - pow(_radiusB, 2.0)) / _radiusA;
}