2
$\begingroup$

Here's a simple Manipulate with a closed BSplineCurve, and a Locator.

Manipulate[Graphics[
 {BSplineCurve[{{0.5, 0.05}, {1.05, 0.7}, {0.4, 1.5}, 
 {-1.15,0.85}, {-0.75, 0.02}, {-0.93, -1}, {0.2, -1.3}, {1.05, -0.83}},
 SplineClosed -> True]}],{pt, Locator}]

Is it possible to detect if the Locator is within the boundary of the curve? So that I can perform some action if it is?

I've found similar things here (52322) and here (17306), but these are all explicit x-y functions, so not so helpful.

Thanks.

$\endgroup$
1
  • 2
    $\begingroup$ Why not use RegionMember[] on a ParametricRegion[], using BSplineFunction[] to represent your curve? $\endgroup$ Jun 2, 2015 at 23:27

1 Answer 1

6
$\begingroup$
Manipulate[
 With[{f = 
    BSplineFunction[{{0.5, 0.05}, {1.05, 0.7}, {0.4, 1.5}, {-1.15, 
       0.85}, {-0.75, 0.02}, {-0.93, -1}, {0.2, -1.3}, {1.05, -0.83}},
      SplineClosed -> True]},
  reg = Polygon[Table[f[t], {t, 0, 1, 0.01}]];
  rm = RegionMember[reg];
  ];
 Column[{Graphics[{Point[p], EdgeForm[{Red, Thick}], Yellow, reg}, 
    PlotRange -> Table[{-2, 2}, {2}]], 
   Row[{"Region Member: ", rm[p]}]}],
 {{p, {0.5, 0.5}}, Locator}
 ]

enter image description here

Note

As @Guesswhoitis. notes in comments region could also be obtained as:

Cases[ParametricPlot[(* stuff *)], Line[l_] :> Polygon[l], ∞][[1]]
$\endgroup$
5
  • $\begingroup$ This is mostly what I had in mind, except that I'd have used ParametricPlot[] to construct the shape. (Still, + 1!) $\endgroup$ Jun 3, 2015 at 2:17
  • $\begingroup$ @J. M. when I originally looked I used ParametricPlot of BSplineFunction...but just thought Polygon would end being cleaner...thanks for upvote :) $\endgroup$
    – ubpdqn
    Jun 3, 2015 at 2:20
  • $\begingroup$ Well, you could still use Cases[ParametricPlot[(* stuff *)], Line[l_] :> Polygon[l], ∞][[1]], of course. :) $\endgroup$ Jun 3, 2015 at 2:40
  • $\begingroup$ @J. M. yes...I will add to answer :) $\endgroup$
    – ubpdqn
    Jun 3, 2015 at 2:43
  • $\begingroup$ Ah, RegionMember. Thanks. That makes it simple. $\endgroup$ Jun 3, 2015 at 15:33

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.