开发者

Uniformly distribute Points within an object using Graphics in Mathematica

开发者 https://www.devze.com 2023-03-15 19:30 出处:网络
Considering : preferred ={{1, 1, 63}, {2, 1, 44}, {3, 1, 27}, {4, 1, 33}, {5, 1, 33}} frmWidth=20.9067;

Considering :

preferred ={{1, 1, 63}, {2, 1, 44}, {3, 1, 27}, {4, 1, 33}, {5, 1, 33}}

frmWidth  =                 20.9067;
frmHeight =                   15.68;

I am displaying 5 types of stimuli 2 by 2. Subjects must choose the one they prefer. Each type of stimuli is displayed 80 times so :

{1,1,63} indicates that the stimuli Cond 1 was preferred 63 times out of the 80 times it was displayed. {3, 1, 27} indicates that the stimuli Cond 3 was preferred 27 times out of the 80 times it was displayed.

Cond1 refers to center of the screen

Cond2 refers to Top-Left Quadrant

Cond3 refers to Top-Right Quadrant

Cond4 refers to Bottom-Left Quadrant

Cond5 refers to Bottom-Right Quadrant

I would like to express this showin开发者_C百科g results.

This is what I have done :

Graphics[{
  Black, EdgeForm[{Thin, LightGray}], 
  Rectangle[{-1, -1}, {frmWidth + 1, frmHeight + 1}], 

  PointSize[0.03],
  Yellow,
  Point@Tuples[{Range[0, frmWidth/2, frmWidth/19], 
  Range[0, frmHeight/2, frmHeight/14]}][[;; preferred[[5, 3]]]],

  Red,
  Point@Tuples[{Range[frmWidth/2, frmWidth, frmWidth/19], 
  Range[0, frmHeight/2, frmHeight/14]}][[;; preferred[[4, 3]]]],

  Green,
  Point@Tuples[{Range[frmWidth/2, frmWidth, frmWidth/19], 
  Range[frmHeight/2, frmHeight, frmHeight/14]}][[;; preferred[[3, 3]]]],

  Orange,
  Point@Tuples[{Range[0, frmWidth/2, frmWidth/19], 
  Range[frmHeight/2, frmHeight, frmHeight/14]}][[;; 
  preferred[[2, 3]]]],

  Blue,
  Point@Tuples[{Range[frmWidth/4, 3/4 frmWidth, frmWidth/19], 
  Range[frmHeight/4, 3/4 frmHeight, frmHeight/14]}][[;; 
  preferred[[1, 3]]]]

  }]

Uniformly distribute Points within an object using Graphics in Mathematica

Problem is the rectangles are gradually filled with points from left to right, instead of the points being uniformly located.

Consider the following :

Graphics[{

  White, EdgeForm[Thick], 
  Rectangle[{0, 0}, {frmWidth, frmHeight}],

  Orange, Opacity[.5],
  Rectangle[{0, frmHeight/2}, {frmWidth/2, frmHeight}, RoundingRadius -> 3],

  Green,
  Rectangle[{frmWidth/2, frmHeight/2}, {frmWidth, frmHeight},RoundingRadius -> 3],

  Red,
  Rectangle[{frmWidth/2, 0}, {frmWidth, frmHeight/2}, RoundingRadius -> 3],

  Yellow,
  Rectangle[{0, 0}, {frmWidth/2, frmHeight/2}, RoundingRadius -> 3],

  Blue,
  Rectangle[{frmWidth/4, frmHeight/4}, {3/4 frmWidth, 3/4 frmHeight}, RoundingRadius -> 3]
  }]

Uniformly distribute Points within an object using Graphics in Mathematica

Now I would like to fill those edge rounded rectangles with the points but have the density changing rather than the part of the rectangles that are filled.

Below is something very ugly I draw in PPT :

Uniformly distribute Points within an object using Graphics in Mathematica

Ideally, the shapes filled with Points could be of any kind. Points would not overlap.

Please let me know alternative ideas.


OK, try this:

Manipulate[ld = Floor[Sqrt[n]];
Graphics[
{{EdgeForm[Dashed], White, 
  Polygon[{{0, 0}, {0, h}, {w, h}, {w, 0}}]},
 Point[Flatten[#, 1] &@
 Table[{x, y}, {x, 0, w, w/ld}, {y, 0, h, h/ld}]] },
PlotRange \[Rule] {{-1, 20}, {-1, 20}}],
{{n, 25}, 10, 100, 1},
{{h, 10}, 5, 20},
{{w, 10}, 5, 20}]

typical configuration:

Uniformly distribute Points within an object using Graphics in Mathematica

(the code I gave lets you control the total number and size of the box via sliders)


Given that your rectangles are rather small, the easiest solution is to use

RandomSample[ allPointsInAnObject ]

Kind of like so:

Graphics[{Circle[{0, 0}, 11], PointSize[0.02], 
  Point[RandomSample[
    Cases[Outer[List, Range[-11, 11], Range[-11, 11]], {x_, y_} /; 
      x^2 + y^2 <= 11^2, {2}], 50]]}]
0

精彩评论

暂无评论...
验证码 换一张
取 消