3
$\begingroup$

I have created a plot using the code below

DensityPlot[(E^-(x^2 + 
      y^2)^2)^2 + ((E^-(x^2 + y^2)^2) (x^2 + y^2) Cos[
      2 Pi])^2, {x, -3, 3}, {y, -3, 3}, PlotTheme -> "Minimal", 
 PlotRange -> All, PlotPoints -> 50, ColorFunction -> "Rainbow"]

To create

enter image description here

I have then separated my plot into n number (50 for this code below) of grids and found the center of each grid with the code (from this answer):

means[n_] := MovingAverage[Subdivide[##, n] & @@ #, 2] &

centers[{nc_, nr_}, {xrange_, yrange_}] := 
 Tuples[{means[nc]@xrange, means[nr]@yrange}]

{xrange, yrange} = {{-3, 3}, {-3, 3}};

{nc, nr} = {50, 50};

centers[{nc, nr}, {xrange, yrange}]

If I plot my image using my grids and center points I get

enter image description here

My problem is that I would like to only get the center points of the grids that cover the circular shape.

My attempt

I have tried to initiate some kind of if loop that would be able to only pick out the corresponding co ordinates that are on the circular image from the second piece of the code (reposted below again). However I haven't been able to actually come up with a logical condition for this.

means[n_] := MovingAverage[Subdivide[##, n] & @@ #, 2] &

centers[{nc_, nr_}, {xrange_, yrange_}] := 
 Tuples[{means[nc]@xrange, means[nr]@yrange}]

{xrange, yrange} = {{-3, 3}, {-3, 3}};

{nc, nr} = {50, 50};

centers[{nc, nr}, {xrange, yrange}]

Thank you for your help

$\endgroup$

1 Answer 1

6
$\begingroup$
dp = DensityPlot[(E^-(x^2 + y^2)^2)^2 + ((E^-(x^2 + y^2)^2) (x^2 + 
        y^2) Cos[2 Pi])^2, {x, -3, 3}, {y, -3, 3}, 
  PlotTheme -> "Minimal", PlotRange -> All, PlotPoints -> 50, 
  ColorFunction -> "Rainbow"]; 

Modify the function centers to select points that lie within a specified region:

ClearAll[centers2]
centers2[region_: Disk[{0, 0}, 1]][{nc_, nr_}, {xrange_, yrange_}] :=
 Select[RegionMember[region]]@centers[{nc, nr}, {xrange, yrange}]

Examples:

{xrange, yrange} = {{-3, 3}, {-3, 3}};

{nc, nr} = {50, 50};


Show[dp, Graphics[{White, PointSize[Medium], Circle[], 
   Point @ centers2[][{nc, nr}, {xrange, yrange}]}]]

enter image description here

radius = 1.3; 

Show[dp, Graphics[{White, PointSize[Medium], Circle[{0, 0}, radius], 
   Point @ centers2[Disk[{0,0}, radius]][{nc, nr}, {xrange, yrange}]}]]

enter image description here

SeedRandom[1]
rp = RandomReal[{-3, 3}, {15, 2}];
region = Polygon[rp[[Last@FindShortestTour@rp]]];

Show[dp, Graphics[{White, PointSize[Medium], FaceForm[], 
   EdgeForm[White], region, 
   Point @ centers2[region][{nc, nr}, {xrange, yrange}]}]]

enter image description here

$\endgroup$
4
  • $\begingroup$ Thank you so much! But how will I now get the co ordinates of the points that are displayed? $\endgroup$ Jun 2, 2021 at 7:47
  • $\begingroup$ @PracticalFruit, centers2[Disk[{0,0}, radius]][{nc, nr}, {xrange, yrange}] gives the coordinates. $\endgroup$
    – kglr
    Jun 2, 2021 at 8:22
  • $\begingroup$ Thank you very much! $\endgroup$ Jun 2, 2021 at 8:41
  • $\begingroup$ Hi, for some reason, when I input the code you have above I get the plot with no white dots for some reason. Can you maybe think why? $\endgroup$ Jun 4, 2021 at 4:56

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.