1
$\begingroup$

As you can see the bottom square is in the ${((0,2),(0,2))}$ range

enter image description here

  • The center of the square is indeterminate
  • The length of a square is indeterminate
  • The length of the square is indeterminate, and $1/2<\mathrm{edge length}<1$
  • have different color

Rectangle functions draw squares in Wolfram,But this function doesn't specify the center

Clear["Global`*"];
colorlist = {Red, Blue, Green, Purple};
center := RandomReal[{0, 2}];
length := RandomReal[{1/2, 1}];
pltRect[color_] := Graphics[
   {color, Rectangle[{0, 0}, {length, length}]},
   Axes -> True,
   PlotRange -> {{-1, 2}, {-1, 2}}
   ];
Show[pltRect /@ colorlist]

enter image description here

I have a lot of problems with the graph here, I don't know how to define the center, and I draw rectangles instead of squares, right

$\endgroup$

4 Answers 4

2
$\begingroup$

Part 1. To get a square of a given center and sidelength, use

square[center_,length_]:=With[{aux=length*{1/2,1/2}},
                           Rectangle[center-aux,center+aux]];

Then, for example,

Show[Graphics[{Purple,square[{1,1},1.5]}],
     Graphics[{Green,square[{2,2},1]}]]

gives

enter image description here

Part 2. To get one random square of the kind OP describes, use

randomSquare := With[{
    length = RandomReal[{1/2, 1}]},
   With[{center = RandomReal[{length/2, 2 - length/2}, 2]},
    square[center, length]]];

To get three, use

randomdraw := Show[
  Graphics[{Purple,randomSquare},Axes->True,PlotRange->{{0,2},{0,2}}],
  Graphics[{Green,randomSquare}],
  Graphics[{Red,randomSquare}]]

For example, using SeedRandom[2];randomdraw I get

enter image description here

$\endgroup$
1
  • $\begingroup$ Equivalent to their own midpoint, feeling pretty hard to think of $\endgroup$ Commented Jul 21, 2022 at 4:31
2
$\begingroup$

Another option

pltRect[color_, {x_, y_}, length_] := 
  Module[{minX = x - length/2, maxX = x + length/2, 
    minY = y - length/2, maxY = y + length/2},
   Graphics[{color, Rectangle[{minX, minY}, {maxX, maxY}]}, 
    Axes -> True, PlotRange -> {{-1, 2}, {-1, 2}}]
   ];
p1 = pltRect[Blue, {.5, .5}, 1];
p2 = pltRect[Red, {.5, 1.5}, .7];
p3 = pltRect[Orange, {1.5, 1}, .6];
Show[p1, p2, p3]

Mathematica graphics

$\endgroup$
2
$\begingroup$

I like using MinMax[] with its handy second padding argument with Rectangle[] for this. Using the same syntax as in user293787's answer:

square[center_, length_] :=
      Rectangle @@ Transpose[MinMax[#, length/2] & /@ Transpose[{center}]]

Another possibility is to use RegularPolygon[]:

square[center_, length_] := RegularPolygon[center, length/Sqrt[2], 4]
$\endgroup$
1
  • $\begingroup$ it is a good ideal $\endgroup$ Commented Jul 21, 2022 at 12:04
1
$\begingroup$

for @user293787 help

Clear["Global`*"];
colorlist = {Red, Blue, Green, Purple};
square[center_, length_] := 
  With[{aux = length*{-1/2, 1/2}}, 
   Rectangle[center - aux, center + aux]];
randomSquare := 
  With[{length = RandomReal[{1/2, 1}]}, 
   With[{center = RandomReal[{length/2, 2 - length/2}, 2]}, 
    square[center, length]]];
Map[Graphics[{#1, Opacity[0.8], randomSquare},
    Axes -> True,
    PlotRange -> {{-1, 2}, {-1, 2}}] &,
  colorlist] // Show

enter image description here

$\endgroup$
2
  • $\begingroup$ Hello. Is this now what you wanted, or is there still something missing? $\endgroup$
    – user293787
    Commented Jul 21, 2022 at 4:40
  • 1
    $\begingroup$ @user293787 yes,It is I want,thank you $\endgroup$ Commented Jul 21, 2022 at 4:43

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.