Pre-Note: For this I assume, that you change your function to return 1 inside and 0 outside the region. You can simply achieve this using Boole
.
Pre-Note 2: This answer will probably not work in a real live example, because the used center of mass is not equivalent with the point most distant from the border. The purpose of my answer is to show, that it is not necessary to rely on ContourPlot
.
If you have a function, and as turned out in chat you are dealing with an InterpolatingFunction
, there is no need to make a ContourPlot
first. One simple solution is to sample your region with Table
and then calculate the center of mass. Note that the returned position is the position inside your raster, so you probably need to rescale it to your original region
data = Table[ Boole[Sin[x] + Sin[y] < 0], {y, -4.7, 1.5, 0.05}, {x, -4.7, 1.5, 0.05}];
Mean[Position[data, 1]] // N
(* {63.5, 63.5} *)
Basically, the solution through ContourPlot
would use similar data, because for this plot your function is sampled as well. Although, ContourPlot
uses a more fancy locally adapting sampling technique.
A solution with more possibilities is to use the data
, create an image and use all the morphological measurements which already come with Mathematica
img = Image[data];
ComponentMeasurements[MorphologicalComponents[img], "Centroid"]
(* {63., 62.} *)
Again pixel coordinates. The difference in coordinates comes from the fact how morphological components treat pixels as objects and that the y axis is reversed in images. Both is easily fixable. The nice thing with this approach is, that you can use all the fancy image component measures, so e.g.
"Centroid" center of mass coordinate
"Medoid" coordinate of the closest element to the centroid
"MeanCentroidDistance" mean distance of all elements from the centroid
"MaxCentroidDistance" maximum distance of all elements from the centroid
"MinCentroidDistance" minimum distance of all elements from the centroid
Finally, if you have already an interpolation function, nothing stops you from integrating. NIntegrate
will probably complain with an interpolation function with such hard jumps from 0 to 1 and honestly, I wouldn't use this approach in real live, but it is surely possible. Assuming ip
is your interpolation function, although I have here the exact expression
ip[x_, y_] := Boole[Sin[x] + Sin[y] < 0]
center = 1/NIntegrate[ip[x, y], {x, -4.7, 1.5}, {y, -4.7, 1.5}]*
NIntegrate[ip[x, y]*{x, y}, {x, -4.7, 1.5}, {y, -4.7, 1.5}];
ContourPlot[Sin[y] + Sin[x], {x, -4.7, 1.5}, {y, -4.7, 1.5},
Contours -> 1, Epilog -> {Red, PointSize[0.01], Point[center]}]

Here you get, when your interpolation function has the right region, real coordinates as you can see since I have put the calculated center
directly in the plot.
The above integration is equivalent to the formula from the center of mass Wikipedia site:
