3
$\begingroup$

This example shows an unexpected result from RegionCentroid:

object = 
  Polygon[
    {{20000, 200000}, {20000.1, 200000.2}, {20000.3, 200000.2}, {20000., 200000.}}];
point = Point[RegionCentroid[object]];
Show[Graphics[{object, Red, PointSize[.2], point}]]

point should be inside object

The RegionCentroid result should be inside the convex region.

When the object is translated to the origin the result is correct. Any idea for a general solution or workaround for arbitrary regions?

$\endgroup$

1 Answer 1

5
$\begingroup$

Your issue is basically a numerics problem. If you were to do the computations exactly, there would be no problem.

obj =
  Polygon[
   {{20000, 200000}, {20000 + 1/10, 200000 + 2/10}, 
    {20000 + 3/10, 200000 + 2/10}, {20000, 200000}}];

pt = Point[RegionCentroid[obj]]
Point[{300002/15, 3000002/15}]
Graphics[{obj, Red, AbsolutePointSize[8], pt}, Frame -> True]

graphics

$\endgroup$
3
  • $\begingroup$ yes, looks like an accuracy issue inside RegionCentroid. The translated object Polygon[{{0, 0}, {0.1, 0.2}, {0.3, 0.2}, {0, 0}}] works fine. $\endgroup$
    – Ali
    Commented Nov 4, 2018 at 20:45
  • $\begingroup$ @Ali As another alternative to symbolic numbers you can also use arbitrary precision numbers via 20000.1`50 (will give 50 digits of precision) or via SetPrecision[20000.1,50]. Just using machine precision numbers implicitly (by just writing 20000.1) is considered intended behaviour and not an issue or bug. It's essentially a tradeoff between precision and computation time. See here for more info on this. $\endgroup$ Commented Nov 4, 2018 at 22:44
  • 1
    $\begingroup$ @Thies: Thanks, using Polygon[SetPrecision[{...},20]] indeed solves the issue in a general way. $\endgroup$
    – Ali
    Commented Nov 5, 2018 at 21:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.