# Tell if a point is inside a FilledCurve

Convert a letter into a FilledCurve:

curve = First[
First[ImportString[
ExportString[Style["♧", FontFamily -> "Times", FontSize -> 72], "PDF"],
"TextMode" -> "Outlines"]]];

g=Graphics[curve]


How to write a function that takes a pair (x,y) and returns 1 if point with coordinates (x,y) is inside of the FilledCurve, i.e. is black, and is 0 otherwise? I know a solution which uses Rasterize

rg=Rasterize[g, RasterSize -> 20, ImageSize -> 100] // Binarize
PixelValue[rg, {x, y}]


however, I am interested in analytical function.

The ultimate goal is to use this function as a testbed for my triangulation algorithm.

• Perhaps check whether SignedRegionDistance[DiscretizeGraphics[g], {x, y}] < 0. – ilian Nov 20 '15 at 16:16
• – ybeltukov Nov 20 '15 at 19:26

You can use RegionMember and discretized mesh region with small enough cells

curve = ImportString[
ExportString[Style["Q", FontFamily -> "Times", FontSize -> 72], "PDF"],
"TextMode" -> "Outlines"][[1, 1, 2, 1, 1]];
curve // Shallow
(* FilledCurve[{{<<4>>}, {<<14>>}}, {{<<13>>}, {<<41>>}}] *)

Graphics[curve, ImageSize -> Small]
(* There is a known bug with special characters and pdf export on Linux *)


f =
RegionMember@BoundaryDiscretizeGraphics[curve, MaxCellMeasure -> 0.01];
pts = RandomReal[60, {10, 2}];

f@pts
(* {False, False, False, False, True, False, False, False, True, False} *)


Visualization of the result (thanks to eldo):

pts = RandomReal[60, {100000, 2}];
Graphics[{{Red, AbsolutePointSize[1], Point@Pick[pts, f@pts]}, {FaceForm[],
EdgeForm[Black], curve}}]


• When I make pts = RandomReal[20, {1000, 2}] and then Show[{Graphics@{Red, PointSize@Large, Point@Pick[pts, f@pts]}, Graphics[curve, ImageSize -> Large]}] some points show up at the lower left outline but not inside the Q – eldo Nov 20 '15 at 19:45
• @eldo It is due to nonzero point size. Note that all point are at the lower part. May be RandomReal[50, {1000, 2}] is better. – ybeltukov Nov 20 '15 at 19:49
• @eldo please, check the update. Do you obtain the same figure? – ybeltukov Nov 20 '15 at 19:55
• Perfect +1 ---------- – eldo Nov 20 '15 at 20:00
• With version 10.2, there is a simple modification for the visualization which eliminates the test function f. For example, region=BoundaryDiscretizeGraphics[curve,MaxCellMeasure->0.01], followed by p=RandomPoint[region,5000], and Graphics[{{Red,AbsolutePointSize[1],Point[p]},{FaceForm[],EdgeForm[Black],curve}}]. – KennyColnago Dec 25 '15 at 1:11