•    Free Pascal
• Website
• Downloads
• Wiki
• Bugtracker
• Mailing List
•    Lazarus
• Website
• Downloads (Laz+FPC)
• Packages (OPM)
• FAQ
• Wiki
• Bugtracker
• CCR Bugs
• IRC channel
• GIT
• Mailing List
• Other languages
•    Foundation
• Website
• Project Roadmap
• Getting the Source
• Screenshots
• How to use the forum

### Author Topic: Utterly stupid maths question: area of an ellipse  (Read 2082 times)

#### MarkMLl

• Hero Member
•     • Posts: 4179 ##### Utterly stupid maths question: area of an ellipse
« on: October 12, 2021, 01:07:59 pm »
I'm very sorry to be asking this in public, but for some reason this is challenging me. I'm trying to output a couple of outlines, represented by routes, into a .gpx file which is basically XML. I don't think that really matters, I'm just trying to give some background for the fragment below.

I've got mean and standard deviation lat and long, accumulated using Welford's Method. I'm fairly happy with that bit, and they display OK using QGIS.

I can draw a rectangular or elliptical outline with edges corresponding to the standard deviation. However that leaves the area of the ellipse slightly smaller than that of the rectangle: to get a better visual approximation what fudge factor do I have to apply to boost the ellipse's area to that of the rectangle, and why?

Code: Pascal  [Select][+][-]
1.   WriteLn(gpx, '  <rte>');
2.   WriteLn(gpx, '    <name>Cartesian standard deviation</name>');
3.   x := meanLong + sigmaLong;          y := meanLat + sigmaLat;
4.   WriteLn(gpx, '    <rtept lat="', y:14:12, '" lon="', x:14:12, '"/>');
5.   x := meanLong + sigmaLong;          y := meanLat - sigmaLat;
6.   WriteLn(gpx, '    <rtept lat="', y:14:12, '" lon="', x:14:12, '"/>');
7.   x := meanLong - sigmaLong;          y := meanLat - sigmaLat;
8.   WriteLn(gpx, '    <rtept lat="', y:14:12, '" lon="', x:14:12, '"/>');
9.   x := meanLong - sigmaLong;          y := meanLat + sigmaLat;
10.   WriteLn(gpx, '    <rtept lat="', y:14:12, '" lon="', x:14:12, '"/>');
11.   x := meanLong + sigmaLong;          y := meanLat + sigmaLat;
12.   WriteLn(gpx, '    <rtept lat="', y:14:12, '" lon="', x:14:12, '"/>');
13.   WriteLn(gpx, '  </rte>');
14.
15. // As an approximation, I suppose that I could get the area of an ellipse
16. // using pi * minor_radius * major_radius, compare it with the area of
17. // the Cartesian standard deviation and introduce a fudge factor to draw an
18. // ellipse with the same area...
19.
20.   fudge := 1.0;
21.
22.   WriteLn(gpx, '  <rte>');
23.   WriteLn(gpx, '    <name>Polar standard deviation</name>');
24.   for i := 0 to 30 do begin
25.     x := meanLong + sigmaLong * fudge * Cos(i * (2 * Pi / 30));
26.     y := meanLat + sigmaLat * fudge * Sin(i * (2 * Pi / 30));
27.     WriteLn(gpx, '    <rtept lat="', y:14:12, '" lon="', x:14:12, '"/>')
28.   end;
29.   WriteLn(gpx, '  </rte>');
30.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

#### wp

• Hero Member
•     • Posts: 9587 ##### Re: Utterly stupid maths question: area of an ellipse
« Reply #1 on: October 12, 2021, 02:36:50 pm »
If I understand correctly you have a rectangle and an ellipse which tightly is enclosed in the rectangle. The major and minor axes of the ellipse are a and b, respectively, and the lengths of the rectangle sides are 2*a and 2*b, respectively.

The area of the ellipse is pi*a*b.
The area of the rectangle is (2*a) * (2*b) = 4*a*b.
Their ratio is 4/pi (rectangle / ellipse).

Therefore, you must draw an ellipse with major and minor axes 4a/pi and 4b/pi to get an ellipse which has the same area as the rectangle.
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

#### engkin ##### Re: Utterly stupid maths question: area of an ellipse
« Reply #2 on: October 12, 2021, 02:44:33 pm »
Code: Pascal  [Select][+][-]
1.   fudge:=2/sqrt(pi);

Edit:
I think wp meant to mention the square root of the ratio to change it from area to length ratio.
« Last Edit: October 12, 2021, 02:47:47 pm by engkin »

#### MarkMLl

• Hero Member
•     • Posts: 4179 ##### Re: Utterly stupid maths question: area of an ellipse
« Reply #3 on: October 12, 2021, 02:46:58 pm »
If I understand correctly you have a rectangle and an ellipse which tightly is enclosed in the rectangle. The major and minor axes of the ellipse are a and b, respectively, and the lengths of the rectangle sides are 2*a and 2*b, respectively.

The area of the ellipse is pi*a*b.
The area of the rectangle is (2*a) * (2*b) = 4*a*b.
Their ratio is 4/pi (rectangle / ellipse).

Therefore, you must draw an ellipse with major and minor axes 4a/pi and 4b/pi to get an ellipse which has the same area as the rectangle.

That's what I thought, but it leaves the ellipse oversized...

Checking on the screen, rectangle is 47.25mm high, x 4/pi = 60.16 which is what I measure.

MarkMLl
« Last Edit: October 12, 2021, 02:52:22 pm by MarkMLl »
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

#### wp

• Hero Member
•     • Posts: 9587 ##### Re: Utterly stupid maths question: area of an ellipse
« Reply #4 on: October 12, 2021, 02:58:09 pm »
Oh, yes! When x and y of the ellipse are scaled by 4/pi then the area increases by the factor sqr(4/pi), of course! You must scale the ellipse axes by the factor 2/sqrt(pi) only  (square root).
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

#### MarkMLl

• Hero Member
•     • Posts: 4179 ##### Re: Utterly stupid maths question: area of an ellipse
« Reply #5 on: October 12, 2021, 03:17:20 pm »
Many thanks: I'm happy to take it on trust that that's right, and that even if it still looks a bit tight that's an optical illusion.

At some point I'll return to this and redo the standard deviation using polar coordinates so that I can orient the rectangle/ellipse, but that's low down in the list of priorities.

Of rather more interest, but only if somebody can answer it without effort: allowing that the data I'm looking at can't be assumed to be normally distributed, is there an equivalent or extension of Welford's Method which also produces the 2-sigma and 3-sigma values?

I looked a bit at LazMapViewer for displaying this stuff over the weekend, but ended up going back to QGIS since it can zoom in further (presumably just by scaling the raster map). I'll almost certainly get back to that at some point...

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

#### wp

• Hero Member
•     • Posts: 9587 ##### Re: Utterly stupid maths question: area of an ellipse
« Reply #6 on: October 12, 2021, 04:13:20 pm »
Of rather more interest, but only if somebody can answer it without effort: allowing that the data I'm looking at can't be assumed to be normally distributed, is there an equivalent or extension of Welford's Method which also produces the 2-sigma and 3-sigma values?
When Welford's method calculates the standard deviation of a distribution, sigma, then 2-sigma is the double value, 3-sigma the triple. There's no magic here.
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

#### MarkMLl

• Hero Member
•     • Posts: 4179 ##### Re: Utterly stupid maths question: area of an ellipse
« Reply #7 on: October 12, 2021, 04:22:33 pm »
When Welford's method calculates the standard deviation of a distribution, sigma, then 2-sigma is the double value, 3-sigma the triple. There's no magic here.

But surely that applies only to normal distribution? The stuff I'm looking at (part-corrected GNSS constellation data) is decidedly non-normal.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

#### wp

• Hero Member
•     • Posts: 9587 ##### Re: Utterly stupid maths question: area of an ellipse
« Reply #8 on: October 12, 2021, 04:39:18 pm »
When Welford's method calculates the standard deviation of a distribution, sigma, then 2-sigma is the double value, 3-sigma the triple. There's no magic here.
But surely that applies only to normal distribution? The stuff I'm looking at (part-corrected GNSS constellation data) is decidedly non-normal.
2-sigma is the double single standard deviation, in the same way that 2*3 is 6, just arithmetics, no relation to the underlying distribution. The standard deviation, however, comes into play when you make a statement that 68 percent of all events are within a range of +/- 1 sigma around the mean, and that 95 percent are within +/- 2 sigma and 99.7 percent are within +/- 3 sigma (https://en.wikipedia.org/wiki/Standard_deviation#Rules_for_normally_distributed_data). These percentages do depend on the distribution.
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

#### MarkMLl

• Hero Member
•     • Posts: 4179 ##### Re: Utterly stupid maths question: area of an ellipse
« Reply #9 on: October 12, 2021, 05:18:37 pm »
So put another way: Welford's Method allows you to estimate the mean and spread of a distribution which might or might not be normal, with ~68% of the points falling inside that spread. Is there a corresponding way of finding out the ~95% and ~99.7% spreads which make no assumption about the actual distribution shape?

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

#### wp

• Hero Member
•     • Posts: 9587 ##### Re: Utterly stupid maths question: area of an ellipse
« Reply #10 on: October 12, 2021, 06:04:32 pm »
I even doubt that for an arbitrary distribution 68% of the points are inside the range calculated by Welford's method.

I have no idea what GNSS constellation data are... What do you know about the distribution of these points? Is it radially symmetric? This would mean that the distribution depends only on the distance from the center. The ellipse that you mentioned indicates that this is not true, but this may just be a consequence of not enough data points.

Here's what I would do (again: not knowing anything about GNSS): I'd calculate the "center" of the distribution, this is the mean of the x and y coordinates. Then I would calculate the distance, sqrt((xi-xcenter)^2 + (yi-ycenter)^2) of all points from the center and store the values in an array and sort it by the distances. Then I'd take the index of each distance point in this array and normalize it to the total count of data points - this produces the cumulative probability of all data points having the same or a smaller distance from the center. For a given percentile, I'd look for the distance for which the cumulative probability is less than this percentage value. Finally I'd use this as the radius of a circle in the original x/y plot of the data points.

Note that very many data points are required to make a statement on the 95% and 99.7% limits.

I can write a small project for you which demonstrates this idea, but you should upload a typical data file (ideally as csv so that I do not have to struggle with the xml of the gpx file).
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

#### MarkMLl

• Hero Member
•     • Posts: 4179 ##### Re: Utterly stupid maths question: area of an ellipse
« Reply #11 on: October 12, 2021, 06:20:25 pm »
Thanks @wp, I think I've probably got enough for the moment and the fact that you've not pulled anything out of a hat suggests that my own next-stage thoughts- much the same as yours- are probably something to pursue if I need to.

GNSS is the generic term for things like GPS, but covering multiple constellations. So you've got the GPS constellation, and can compare that instantaneously against the Chinese Beidou constellation etc.

The .gpx files I'm emitting and postprocessing are a standardised format used by lots of programs to store tracks of points, e.g. generated by people orienteering etc. That allows me to use standard programs such as QGIS and Google Earth (which is far more powerful than most people realise) to render the output.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories