Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit be920ad

Browse files
committed
Improved qhull triangulations with large x,y offset
1 parent 8e517d8 commit be920ad

File tree

5 files changed

+29
-2
lines changed

5 files changed

+29
-2
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Improved Delaunay triangulations with large offsets
2+
```````````````````````````````````````````````````
3+
4+
Delaunay triangulations now deal with large x,y offsets in a better
5+
way. This can cause minor changes to any triangulations calculated
6+
using Matplotlib.

lib/matplotlib/tests/test_triangulation.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1123,3 +1123,14 @@ def test_internal_cpp_api():
11231123
with pytest.raises(ValueError) as excinfo:
11241124
trifinder.find_many([0], [0, 1])
11251125
excinfo.match(r'x and y must be array_like with same shape')
1126+
1127+
1128+
def test_qhull_large_offset():
1129+
# github issue 8682.
1130+
x = np.asarray([0, 1, 0, 1, 0.5])
1131+
y = np.asarray([0, 0, 1, 1, 0.5])
1132+
1133+
offset = 1e10
1134+
triang = mtri.Triangulation(x, y)
1135+
triang_offset = mtri.Triangulation(x + offset, y + offset)
1136+
assert len(triang.triangles) == len(triang_offset.triangles)

src/qhull_wrap.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,8 @@ delaunay_impl(int npoints, const double* x, const double* y,
108108
PyArrayObject* neighbors = NULL;
109109
int* triangles_ptr;
110110
int* neighbors_ptr;
111+
double x_mean = 0.0;
112+
double y_mean = 0.0;
111113

112114
QHULL_LIB_CHECK
113115

@@ -119,10 +121,18 @@ delaunay_impl(int npoints, const double* x, const double* y,
119121
goto error_before_qhull;
120122
}
121123

124+
/* Determine mean x, y coordinates. */
125+
for (i = 0; i < npoints; ++i) {
126+
x_mean += x[i];
127+
y_mean += y[i];
128+
}
129+
x_mean /= npoints;
130+
y_mean /= npoints;
131+
122132
/* Prepare points array to pass to qhull. */
123133
for (i = 0; i < npoints; ++i) {
124-
points[2*i ] = x[i];
125-
points[2*i+1] = y[i];
134+
points[2*i ] = x[i] - x_mean;
135+
points[2*i+1] = y[i] - y_mean;
126136
}
127137

128138
/* qhull expects a FILE* to write errors to. */

0 commit comments

Comments
 (0)