-1
$\begingroup$

Display 100 polygons with side length 10, opacity.5, and random choices of colors, sides between 3 and 8, and integer coordinates up to 100.

Graphics[
    Table[
    Style[
    RegularPolygon[RandomInteger[100, 2], 10, n], RandomColor[], 
    Opacity[0.5]], 100, {n, 3, 8}]]

this is the expected output. there is no error but something is wrong. plz help. thank you. enter image description here

$\endgroup$

2 Answers 2

1
$\begingroup$

You are generating 600 polygons (6 groups of 100).You could make the sides random.

Graphics[
 Table[Style[
   RegularPolygon[RandomInteger[100, 2], 10, RandomInteger[{3, 8}]], 
   RandomColor[], Opacity[0.5]], 100]]

enter image description here

$\endgroup$
1
  • $\begingroup$ Note that this answer is based on a radius of 10 (as per your "SAMPLE EXPECTED OUTPUT"). But the text of the question mentioned a "side length 10" (see cvgmt answer for this approach). $\endgroup$ Commented Oct 10, 2022 at 16:02
0
$\begingroup$

To keep the length of the side of the polygons to 10, we using AnglePath.

length = 10;
Graphics[
 Table[{EdgeForm[RandomColor[]], FaceForm[], 
   Polygon[AnglePath[ConstantArray[{length, 2 π/n}, n]]]}, {n, 3, 
   8}]]

enter image description here

Graphics[
 Table[With[{n = RandomInteger[{3, 8}], 
    position = RandomInteger[100, 2]}, {EdgeForm[White], Opacity[.5], 
    FaceForm[RandomColor[]], 
    Polygon[AnglePath[position, ConstantArray[{10, 2 π/n}, n]]]}],
   100], Background -> Black]

enter image description here

$\endgroup$

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.