From 5670e6f1b87c836420e37972f49e81e400eaca6c Mon Sep 17 00:00:00 2001 From: Rob Tillaart Date: Fri, 10 May 2024 13:54:39 +0200 Subject: [PATCH] 0.1.4 ellipse --- libraries/ellipse/CHANGELOG.md | 11 ++- libraries/ellipse/README.md | 8 +- libraries/ellipse/ellipse.cpp | 18 +++- libraries/ellipse/ellipse.h | 5 +- .../ellipse_performance.ino | 27 ++++-- .../ellipse_performance/performance_0.1.4.txt | 45 +++++++++ .../ellipse_performance2.ino | 95 +++++++++++++++++++ .../performance_0.1.4.txt | 31 ++++++ libraries/ellipse/keywords.txt | 1 + libraries/ellipse/library.json | 2 +- libraries/ellipse/library.properties | 2 +- 11 files changed, 227 insertions(+), 18 deletions(-) create mode 100644 libraries/ellipse/examples/ellipse_performance/performance_0.1.4.txt create mode 100644 libraries/ellipse/examples/ellipse_performance2/ellipse_performance2.ino create mode 100644 libraries/ellipse/examples/ellipse_performance2/performance_0.1.4.txt diff --git a/libraries/ellipse/CHANGELOG.md b/libraries/ellipse/CHANGELOG.md index ceadc16d..a2dd08ab 100644 --- a/libraries/ellipse/CHANGELOG.md +++ b/libraries/ellipse/CHANGELOG.md @@ -6,11 +6,18 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). -## [0.1.2] - 2023-10-25 +## [0.1.4] - 2024-05-06 +- add **float perimeter_Parker()** +- update **ellipse_performance.ino** +- add **ellipse_performance2.ino** (table form) +- update keywords.txt +- minor edits + + +## [0.1.3] - 2023-10-25 - update readme.md - update keywords.txt - ## [0.1.2] - 2022-11-02 - add changelog.md - add rp2040 to build-CI diff --git a/libraries/ellipse/README.md b/libraries/ellipse/README.md index 87a5fba3..de07c2de 100644 --- a/libraries/ellipse/README.md +++ b/libraries/ellipse/README.md @@ -11,7 +11,7 @@ # ellipse -Arduino library with ellipse math +Arduino library with ellipse math. ## Description @@ -19,7 +19,8 @@ Arduino library with ellipse math This experimental library provides basic ellipse math. It was written to test different perimeter algorithms. -Trigger was a video of Dr. Matt Parker. +Trigger was a video of Dr. Matt Parker, with the title: +"Why is there no equation for the perimeter of an ellipse?" https://www.youtube.com/watch?v=5nW3nJhBHL0 @@ -48,9 +49,10 @@ Perimeter == circumference - **float perimeter_ref()** slower but best known algorithm (Ramanujan 2) - **float perimeter_Keppler()** good and fast algorithm for eccentricity between 1 and 2. - **float perimeter_Ramanujan1()** good algorithm. +- **float perimeter_Parker()** Matt Parker's algorithm, better than Ramanujan, but not symmetric. - **float perimeter_Ramanujan2()** slow but best known algorithm. -See performance example for differences in timing. +See performance example for differences in timing and accuracy. #### Convenience functions diff --git a/libraries/ellipse/ellipse.cpp b/libraries/ellipse/ellipse.cpp index ccf10c72..59d14f8f 100644 --- a/libraries/ellipse/ellipse.cpp +++ b/libraries/ellipse/ellipse.cpp @@ -2,7 +2,7 @@ // FILE: ellipse.cpp // AUTHOR: Rob Tillaart // DATE: 2021-10-31 -// VERSION: 0.1.3 +// VERSION: 0.1.4 // PURPOSE: Arduino library for ellipse maths // URL: https://github.com/RobTillaart/ellipse // TRIGGER: https://www.youtube.com/watch?v=5nW3nJhBHL0 @@ -38,7 +38,7 @@ float ellipse::perimeter_ref() float ellipse::perimeter_Keppler() { // Keppler - float p = 2 * PI * (_a + _b) / 2; // very fast for a ~ b + float p = 2 * PI * (_a + _b) / 2; // very fast for a ~ b return p; } @@ -50,7 +50,19 @@ float ellipse::perimeter_Ramanujan1() float a3 = 3 * _a; float b3 = 3 * _b; float p = PI * (a3 + b3 - sqrt( (a3 + _b)*(_a + b3))); - // one float operation less (~7% faster) + // one float operation less (~7% faster) + return p; +} + + +float ellipse::perimeter_Parker() +{ + // Matthew Parker, better than Ramanujan 1 for e > ~2.4 + // not symmetric. _a should be longest. + float a = (_a > _b) ? _a : _b; + float b = (_a > _b) ? _b : _a; + float t = sqrt(269 * a * a + 667 * a * b + 371 * b * b); // 269=prime 667=23*29 371=7*53 + float p = PI * ((53.0/3.0) * a + (717.0/35.0) * b - t); return p; } diff --git a/libraries/ellipse/ellipse.h b/libraries/ellipse/ellipse.h index 20073ccd..fd8f5f6c 100644 --- a/libraries/ellipse/ellipse.h +++ b/libraries/ellipse/ellipse.h @@ -3,14 +3,14 @@ // FILE: ellipse.h // AUTHOR: Rob Tillaart // DATE: 2021-10-31 -// VERSION: 0.1.3 +// VERSION: 0.1.4 // PURPOSE: Arduino library for ellipse maths // URL: https://github.com/RobTillaart/ellipse #include "Arduino.h" -#define ELLIPSE_LIB_VERSION (F("0.1.3")) +#define ELLIPSE_LIB_VERSION (F("0.1.4")) class ellipse @@ -23,6 +23,7 @@ public: float perimeter_ref(); // pretty slow but very good over long range float perimeter_Keppler(); float perimeter_Ramanujan1(); + float perimeter_Parker(); float perimeter_Ramanujan2(); float eccentricity(); diff --git a/libraries/ellipse/examples/ellipse_performance/ellipse_performance.ino b/libraries/ellipse/examples/ellipse_performance/ellipse_performance.ino index 4fe31ee2..fc1c0bc9 100644 --- a/libraries/ellipse/examples/ellipse_performance/ellipse_performance.ino +++ b/libraries/ellipse/examples/ellipse_performance/ellipse_performance.ino @@ -1,7 +1,7 @@ // // FILE: ellipse_performance.ino // AUTHOR: Rob Tillaart -// PURPOSE: demo +// PURPOSE: performance and accuracy test // DATE: 2021-10-31 // URL: https://github.com/RobTillaart/ellipse @@ -26,6 +26,7 @@ void setup() test_circumference(); test_perimeter_ref(); test_perimeter_keppler(); + test_perimeter_parker(); test_eccentricity(); Serial.println("\nEllipse A=10, B=5"); @@ -34,6 +35,7 @@ void setup() test_circumference(); test_perimeter_ref(); test_perimeter_keppler(); + test_perimeter_parker(); test_eccentricity(); Serial.println("\nEllipse A=10, B=1"); @@ -42,11 +44,10 @@ void setup() test_circumference(); test_perimeter_ref(); test_perimeter_keppler(); + test_perimeter_parker(); test_eccentricity(); - - Serial.println("\nDone..."); } @@ -65,7 +66,7 @@ void test_circumference() Serial.print("\tTIME: "); Serial.print(stop - start); Serial.print("\tCIRCUMFERENCE: "); - Serial.println(f); + Serial.println(f, 4); delay(100); } @@ -79,7 +80,7 @@ void test_perimeter_ref() Serial.print("\tTIME: "); Serial.print(stop - start); Serial.print("\tCIRCUMFERENCE: "); - Serial.println(f); + Serial.println(f, 4); delay(100); } @@ -93,7 +94,21 @@ void test_perimeter_keppler() Serial.print("\tTIME: "); Serial.print(stop - start); Serial.print("\tCIRCUMFERENCE: "); - Serial.println(f); + Serial.println(f, 4); + delay(100); +} + + +void test_perimeter_parker() +{ + Serial.println(__FUNCTION__); + start = micros(); + f = E.perimeter_Parker(); + stop = micros(); + Serial.print("\tTIME: "); + Serial.print(stop - start); + Serial.print("\tCIRCUMFERENCE: "); + Serial.println(f, 4); delay(100); } diff --git a/libraries/ellipse/examples/ellipse_performance/performance_0.1.4.txt b/libraries/ellipse/examples/ellipse_performance/performance_0.1.4.txt new file mode 100644 index 00000000..1ef44837 --- /dev/null +++ b/libraries/ellipse/examples/ellipse_performance/performance_0.1.4.txt @@ -0,0 +1,45 @@ + +Arduino UNO +IDE 1.18.19 + +ellipse_performance.ino + +Ellipse A=10, B=10 +test_circumference + TIME: 104 CIRCUMFERENCE: 62.8319 +test_perimeter_ref + TIME: 120 CIRCUMFERENCE: 62.8319 +test_perimeter_keppler + TIME: 36 CIRCUMFERENCE: 62.8319 +test_perimeter_parker + TIME: 160 CIRCUMFERENCE: 62.8296 +test_eccentricity + TIME: 8 ECCENTRICITY: 0.00 + +Ellipse A=10, B=5 +test_circumference + TIME: 104 CIRCUMFERENCE: 48.4421 +test_perimeter_ref + TIME: 188 CIRCUMFERENCE: 48.4422 +test_perimeter_keppler + TIME: 32 CIRCUMFERENCE: 47.1239 +test_perimeter_parker + TIME: 160 CIRCUMFERENCE: 48.4411 +test_eccentricity + TIME: 104 ECCENTRICITY: 0.87 + +Ellipse A=10, B=1 +test_circumference + TIME: 108 CIRCUMFERENCE: 40.6055 +test_perimeter_ref + TIME: 188 CIRCUMFERENCE: 40.6393 +test_perimeter_keppler + TIME: 36 CIRCUMFERENCE: 34.5575 +test_perimeter_parker + TIME: 164 CIRCUMFERENCE: 40.5942 +test_eccentricity + TIME: 108 ECCENTRICITY: 0.99 + +Done... + +Done... diff --git a/libraries/ellipse/examples/ellipse_performance2/ellipse_performance2.ino b/libraries/ellipse/examples/ellipse_performance2/ellipse_performance2.ino new file mode 100644 index 00000000..d17f121c --- /dev/null +++ b/libraries/ellipse/examples/ellipse_performance2/ellipse_performance2.ino @@ -0,0 +1,95 @@ +// +// FILE: ellipse_performance.ino +// AUTHOR: Rob Tillaart +// PURPOSE: performance and accuracy test +// DATE: 2021-10-31 +// URL: https://github.com/RobTillaart/ellipse + + +#include "ellipse.h" + + +ellipse E(1, 1); +uint32_t start, stop; +volatile float f; + + +void setup() +{ + Serial.begin(115200); + Serial.println(__FILE__); + Serial.println(); + + Serial.println(" A B t ECC t circum t Ramanu t Keppler t Parker"); + Serial.println("================================================================================"); + + float A = 10; + float B = 10; + while (B > 0) + { + E.setA(A); + E.setB(B); + Serial.print(A); + Serial.print(", "); + Serial.print(B); + Serial.print(", "); + delay(100); + + start = micros(); + f = E.eccentricity(); + stop = micros(); + Serial.print(stop - start); + Serial.print(", "); + Serial.print(f, 4); + Serial.print(", "); + delay(100); + + start = micros(); + f = E.circumference(); + stop = micros(); + Serial.print(stop - start); + Serial.print(", "); + Serial.print(f, 4); + Serial.print(", "); + delay(100); + + start = micros(); + f = E.perimeter_ref(); + stop = micros(); + Serial.print(stop - start); + Serial.print(", "); + Serial.print(f, 4); + Serial.print(", "); + delay(100); + + start = micros(); + f = E.perimeter_Keppler(); + stop = micros(); + Serial.print(stop - start); + Serial.print(", "); + Serial.print(f, 4); + Serial.print(", "); + delay(100); + + start = micros(); + f = E.perimeter_Parker(); + stop = micros(); + Serial.print(stop - start); + Serial.print(", "); + Serial.print(f, 4); + delay(100); + + Serial.println(); + B = B - 0.5; + } + + Serial.println("\nDone..."); +} + + +void loop() +{ +} + + +// -- END OF FILE -- diff --git a/libraries/ellipse/examples/ellipse_performance2/performance_0.1.4.txt b/libraries/ellipse/examples/ellipse_performance2/performance_0.1.4.txt new file mode 100644 index 00000000..43fe8949 --- /dev/null +++ b/libraries/ellipse/examples/ellipse_performance2/performance_0.1.4.txt @@ -0,0 +1,31 @@ + +Arduino UNO +IDE 1.18.19 + +ellipse_performance2.ino (slightly edited) + + A B t ECC t circum t Ramanu t Keppler t Parker +================================================================================ +10.00, 10.00, 8, 0.0000, 96, 62.8319, 104, 62.8319, 28, 62.8319, 148, 62.8296 +10.00, 9.50, 100, 0.3122, 104, 61.2711, 180, 61.2711, 28, 61.2611, 148, 61.2704 +10.00, 9.00, 96, 0.4359, 100, 59.7316, 172, 59.7316, 28, 59.6903, 156, 59.7319 +10.00, 8.50, 100, 0.5268, 96, 58.2150, 172, 58.2150, 28, 58.1195, 148, 58.2157 +10.00, 8.00, 100, 0.6000, 100, 56.7233, 176, 56.7233, 28, 56.5487, 148, 56.7241 +10.00, 7.50, 100, 0.6614, 104, 55.2587, 180, 55.2587, 32, 54.9779, 148, 55.2594 +10.00, 7.00, 92, 0.7141, 100, 53.8237, 176, 53.8237, 28, 53.4071, 148, 53.8238 +10.00, 6.50, 104, 0.7599, 96, 52.4210, 180, 52.4210, 28, 51.8363, 144, 52.4207 +10.00, 6.00, 92, 0.8000, 100, 51.0540, 176, 51.0540, 32, 50.2655, 148, 51.0532 +10.00, 5.50, 96, 0.8352, 100, 49.7262, 172, 49.7263, 28, 48.6947, 148, 49.7252 +10.00, 5.00, 96, 0.8660, 100, 48.4421, 176, 48.4422, 24, 47.1239, 148, 48.4411 +10.00, 4.50, 96, 0.8930, 100, 47.2066, 180, 47.2069, 24, 45.5531, 152, 47.2060 +10.00, 4.00, 96, 0.9165, 100, 46.0256, 172, 46.0262, 24, 43.9823, 148, 46.0258 +10.00, 3.50, 100, 0.9367, 96, 44.9062, 176, 44.9074, 28, 42.4115, 140, 44.9077 +10.00, 3.00, 96, 0.9539, 104, 43.8567, 172, 43.8591, 28, 40.8407, 144, 43.8595 +10.00, 2.50, 100, 0.9682, 100, 42.8875, 176, 42.8921, 28, 39.2699, 152, 42.8912 +10.00, 2.00, 104, 0.9798, 100, 42.0112, 176, 42.0201, 28, 37.6991, 148, 42.0142 +10.00, 1.50, 96, 0.9887, 96, 41.2437, 172, 41.2609, 28, 36.1283, 148, 41.2429 +10.00, 1.00, 100, 0.9950, 100, 40.6055, 176, 40.6393, 28, 34.5575, 152, 40.5942 +10.00, 0.50, 96, 0.9987, 96, 40.1234, 176, 40.1922, 28, 32.9867, 148, 40.0895 + + +Done... diff --git a/libraries/ellipse/keywords.txt b/libraries/ellipse/keywords.txt index d2caec47..a7f5f6b0 100644 --- a/libraries/ellipse/keywords.txt +++ b/libraries/ellipse/keywords.txt @@ -10,6 +10,7 @@ circumference KEYWORD2 perimeter_ref KEYWORD2 perimeter_Keppler KEYWORD2 +perimeter_Parker KEYWORD2 perimeter_Ramanujan1 KEYWORD2 perimeter_Ramanujan2 KEYWORD2 diff --git a/libraries/ellipse/library.json b/libraries/ellipse/library.json index 992e599d..344c28ba 100644 --- a/libraries/ellipse/library.json +++ b/libraries/ellipse/library.json @@ -15,7 +15,7 @@ "type": "git", "url": "https://github.com/RobTillaart/ellipse.git" }, - "version": "0.1.3", + "version": "0.1.4", "license": "MIT", "frameworks": "*", "platforms": "*" diff --git a/libraries/ellipse/library.properties b/libraries/ellipse/library.properties index 285db0d2..d893b0e5 100644 --- a/libraries/ellipse/library.properties +++ b/libraries/ellipse/library.properties @@ -1,5 +1,5 @@ name=ellipse -version=0.1.3 +version=0.1.4 author=Rob Tillaart maintainer=Rob Tillaart sentence=Arduino library for ellipse class