# A square with different centers and sides of different lengths

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

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


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

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

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

• Equivalent to their own midpoint, feeling pretty hard to think of Commented Jul 21, 2022 at 4:31

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]


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]

• it is a good ideal Commented Jul 21, 2022 at 12:04

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


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