summaryrefslogtreecommitdiff
path: root/sql/spatial.cc
diff options
context:
space:
mode:
authorSergei Golubchik <serg@mariadb.org>2017-08-26 00:34:43 +0200
committerSergei Golubchik <serg@mariadb.org>2017-08-26 00:34:43 +0200
commitbb8e99fdc367a470c6195a2ae4c33f273fe55a25 (patch)
tree3b3bcacec8faab66bf6756f20dff1e45462fdc1c /sql/spatial.cc
parentadd44e684cb9f1b46d1d5facdf6255360fa7b656 (diff)
parentc02d61bc11ee06b6043e6db9a15d00b9f40bce1f (diff)
downloadmariadb-git-bb8e99fdc367a470c6195a2ae4c33f273fe55a25.tar.gz
Merge branch 'bb-10.2-ext' into 10.3
Diffstat (limited to 'sql/spatial.cc')
-rw-r--r--sql/spatial.cc27
1 files changed, 21 insertions, 6 deletions
diff --git a/sql/spatial.cc b/sql/spatial.cc
index 36c19115311..8d595541eef 100644
--- a/sql/spatial.cc
+++ b/sql/spatial.cc
@@ -1663,8 +1663,8 @@ int Gis_polygon::centroid_xy(double *x, double *y) const
uint32 n_points, org_n_points;
double prev_x, prev_y;
double cur_area= 0;
- double cur_cx= 0;
- double cur_cy= 0;
+ double cur_cx= 0, cur_cy= 0;
+ double sum_cx= 0, sum_cy= 0;
if (no_data(data, 4))
return 1;
@@ -1678,17 +1678,32 @@ int Gis_polygon::centroid_xy(double *x, double *y) const
while (--n_points) // One point is already read
{
double tmp_x, tmp_y;
+ double loc_area;
get_point(&tmp_x, &tmp_y, data);
data+= POINT_DATA_SIZE;
- cur_area+= (prev_x + tmp_x) * (prev_y - tmp_y);
+ loc_area= prev_x * tmp_y - tmp_x * prev_y;
+ cur_area+= loc_area;
cur_cx+= tmp_x;
cur_cy+= tmp_y;
+ sum_cx+= (prev_x + tmp_x) * loc_area;
+ sum_cy+= (prev_y + tmp_y) * loc_area;
+
prev_x= tmp_x;
prev_y= tmp_y;
}
- cur_area= fabs(cur_area) / 2;
- cur_cx= cur_cx / (org_n_points - 1);
- cur_cy= cur_cy / (org_n_points - 1);
+
+ if (fabs(cur_area) > 1e-10)
+ {
+ cur_cx= sum_cx / cur_area / 3.0;
+ cur_cy= sum_cy / cur_area / 3.0;
+ }
+ else
+ {
+ cur_cx= cur_cx / (org_n_points - 1);
+ cur_cy= cur_cy / (org_n_points - 1);
+ }
+
+ cur_area= fabs(cur_area);
if (!first_loop)
{