5
$\begingroup$

Convert a letter into a FilledCurve:

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

clubs

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.

$\endgroup$
2
  • $\begingroup$ Perhaps check whether SignedRegionDistance[DiscretizeGraphics[g], {x, y}] < 0. $\endgroup$ Commented Nov 20, 2015 at 16:16
  • $\begingroup$ Related: Undocumented form for FilledCurve $\endgroup$ Commented Nov 20, 2015 at 19:26

1 Answer 1

6
$\begingroup$

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 *) 

enter image description here

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}}] 

enter image description here

$\endgroup$
5
  • $\begingroup$ 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 $\endgroup$ Commented Nov 20, 2015 at 19:45
  • $\begingroup$ @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. $\endgroup$ Commented Nov 20, 2015 at 19:49
  • $\begingroup$ @eldo please, check the update. Do you obtain the same figure? $\endgroup$ Commented Nov 20, 2015 at 19:55
  • $\begingroup$ Perfect +1 ---------- $\endgroup$ Commented Nov 20, 2015 at 20:00
  • $\begingroup$ 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}}]. $\endgroup$ Commented Dec 25, 2015 at 1:11

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.