I am studying the maxima of function $\overline{r}(x,y)$ of closed shapes.
Suppose the curve is star-shaped with respect to this center point $\mathbf p=(u,v)$, so that any ray emanating from $\mathbf p$ meets the curve exactly once, at say point $\mathbf q$. Then $r = \|\mathbf q - \mathbf p\|$, $\theta$ is the angle between $\mathbf q-\mathbf p$ and the $x$-axis, $\overline{r}(x,y)$ is the average radius $$\overline{r}(x,y)=\frac1{2\pi}\oint_{\mathbf q\in\mathcal C}\|\mathbf q-\mathbf p\|\,\mathrm d\theta.$$
and $\mathbf{p}$ is the point maximizing $\overline{r}$.
(Conveniently, this integral can also be computed for non-star-shaped curves; for a ray that meets the curve multiple times, it amounts to taking the total length of all segments that lie in the interior of the curve.)
In a previous answer, using Mesh Coordinates, Discretization, and Euler's Distance, I could calculate the maxima $\mathbf{p}$ of $\overline{r}(x,y)$.
curve = DiscretizeRegion[
ImplicitRegion[
S1[x, y] == 1, {{x, -3, 3}, {y, -4, 4}}], {{-3, 3}, {-4, 4}},
AccuracyGoal -> 8]
q = MeshCoordinates[curve];
edges = First /@ MeshCells[curve, 1];
signedAngle[a_, b_] := Arg[(Complex @@ a)/(Complex @@ b)]
avgRadius[p_] :=
1/(2 \[Pi]) Abs[Sum[Module[{q1, q2, r, d\[Theta]}, q1 = q[[First@e]];
q2 = q[[Last@e]];
r = EuclideanDistance[p, (q1 + q2)/2];(*midpoint approximation*)
d\[Theta] = signedAngle[q1 - p, q2 - p];
r d\[Theta]], {e, edges}]]
s = FindMaximum[avgRadius[{x, y}], {{x, 0}, {y, 0}}]
However, I wanted to apply a similar definition to closed shapes that contain straight lines.
Shapes With Straight Lines: Polygons
For example, for a triangle with vertices $(-1,0)$, $(0,1)$ and $(1,0)$, if I apply DescritizeRegion
curve = DiscretizeRegion[
Line[{{-1, 0}, {0, 1}, {1, 0}, {-1, 0}}]], {{-1, 1}, {-1,
1}}, AccuracyGoal -> 8]
instead of showing the discretized region it gives the following.
DiscretizeRegion::regp: A correctly specified region expected at position 1 of DiscretizeRegion[,{{-1,1},{-1,1}},AccuracyGoal->8].
Assuming the positions should be labelled, I used BoundaryMeshRegion
curve = DiscretizeRegion[
BoundaryMeshRegion[{{-1, 0}, {0, 1}, {1, 0}},
Line[{1, 2, 3, 1}]], {{-1, 1}, {0, 1}}, AccuracyGoal -> 8]
but instead of descritizing the boundary, its descritizes the area.
How do I apply $\overline{r}(x,y)$ to the triangle? Similarily how would apply this to a dumbell shaped curve?
Graphics
is not a region. You can discretize the 1-dimensional line by removing theGraphics[...]
and just usingDiscretizeRegion[ Line[{{-1, 0}, {0, 1}, {1, 0}, {-1, 0}}], AccuracyGoal -> 8]
. $\endgroup$DiscretizeRegion::regp: A correctly specified region expected at position 1 of DiscretizeRegion[Line[{-1,0},{0,1},{1,0},{-1,0}],{{-2,2},{-2,2}},AccuracyGoal->4].
$\endgroup$MeshRegion
. However, even being not the most efficient way and having requirement that the curve has to be closed, you can usecurve = BoundaryMesh@ DiscretizeRegion[ BoundaryMeshRegion[{{-1, 0}, {0, 1}, {1, 0}}, Line[{1, 2, 3, 1}]], {{-1, 1}, {0, 1}}]
$\endgroup$FindMinimum
is{0,0}
which happens to lie on the curve. Trys = FindMaximum[avgRadius[{x, y}], {{x, 0.1}, {y, 0.1}}]
instead. $\endgroup$AccuracyGoal
inDiscretizeRegion
is the right way to achieve higher accuracy here; the error of your method is proportional to the maximal edge length ofcurve
. You can control the maximal edge length withMaxCellMeasure->{1->len}
(1 stands for 1-dimensional cells,len
for the maximal length you allow). Anyway, usingDiscretizeRegion
and applyingBoundaryMesh
on the result is a very expensive way to get a finely smapled curve. See my aanswer below for a more performant approach. $\endgroup$