mirror of
https://github.com/ciphervance/supercell-wx.git
synced 2025-10-30 23:10:05 +00:00
Centroid calculation for alert distance
This commit is contained in:
parent
a3d007d8c4
commit
5784abc117
3 changed files with 91 additions and 19 deletions
|
|
@ -2,6 +2,7 @@
|
|||
#include <scwx/common/characters.hpp>
|
||||
|
||||
#include <format>
|
||||
#include <numbers>
|
||||
|
||||
namespace scwx
|
||||
{
|
||||
|
|
@ -12,6 +13,44 @@ static std::string GetDegreeString(double degrees,
|
|||
DegreeStringType type,
|
||||
const std::string& suffix);
|
||||
|
||||
Coordinate GetCentroid(const std::vector<Coordinate>& coordinates)
|
||||
{
|
||||
double x = 0.0;
|
||||
double y = 0.0;
|
||||
double z = 0.0;
|
||||
|
||||
for (const Coordinate& c : coordinates)
|
||||
{
|
||||
// Convert latitude and longitude to radians
|
||||
double latitudeRadians = c.latitude_ * std::numbers::pi / 180.0;
|
||||
double longitudeRadians = c.longitude_ * std::numbers::pi / 180.0;
|
||||
|
||||
// Convert latitude and longitude to Cartesian coordinates
|
||||
double x1 = std::cos(latitudeRadians) * std::cos(longitudeRadians);
|
||||
double y1 = std::cos(latitudeRadians) * std::sin(longitudeRadians);
|
||||
double z1 = std::sin(latitudeRadians);
|
||||
|
||||
// Combine with accumulators
|
||||
x += x1;
|
||||
y += y1;
|
||||
z += z1;
|
||||
}
|
||||
|
||||
// Compute averages
|
||||
x /= coordinates.size();
|
||||
y /= coordinates.size();
|
||||
z /= coordinates.size();
|
||||
|
||||
// Convert Cartesian coordinates back to latitude and longitude
|
||||
double hyp = std::sqrt(x * x + y * y);
|
||||
double latitudeRadians = std::atan2(z, hyp);
|
||||
double longitudeRadians = std::atan2(y, x);
|
||||
|
||||
// Return latitude and longitude in degrees
|
||||
return {latitudeRadians * 180.0 / std::numbers::pi,
|
||||
longitudeRadians * 180.0 / std::numbers::pi};
|
||||
}
|
||||
|
||||
std::string GetLatitudeString(double latitude, DegreeStringType type)
|
||||
{
|
||||
std::string suffix {};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue