VBA
Sub PolkaDotArt()
Dim shp As Shape
Dim x As Long, y As Long
Dim radius As Double
Dim color As Long
' Set the boundaries of the rectangular frame
Dim left As Long, top As Long, width As Long, height As Long
left = 50
top = 50
width = 400
height = 300
' Number of circles to create
Dim numCircles As Long
numCircles = 1000
' Loop to create the polka dots
For i = 1 To numCircles
' Generate random position within the frame
x = left + Rnd() * width
y = top + Rnd() * height
' Generate random radius (small or large)
radius = Rnd() * 20 + 5 ' Radius between 5 and 25 points
' Generate random color
color = RGB(Rnd() * 255, Rnd() * 255, Rnd() * 255)
' Create the circle shape
Set shp = ActiveSheet.Shapes.AddShape(msoShapeOval, x - radius, y - radius, radius * 2, radius * 2)
With shp
.Fill.ForeColor.RGB = color ' Set fill color
.Line.Visible = msoFalse ' Remove the outline (optional)
End With
Next i
End Sub
Explanation and How it Creates the Art:
-
Setting the Stage:
left
,top
,width
, andheight
define the rectangular frame within which the polka dots will be drawn. Adjust these values to change the size and position of the frame.numCircles
determines how many polka dots will be created.
-
The Loop:
- The
For
loop iteratesnumCircles
times, creating one circle in each iteration.
- The
-
Randomness is Key:
x = left + Rnd() * width
: Generates a random x-coordinate within the frame's width.Rnd()
produces a random number between 0 and 1.y = top + Rnd() * height
: Generates a random y-coordinate within the frame's height.radius = Rnd() * 20 + 5
: Generates a random radius between 5 and 25 points, creating a mix of small and large circles.color = RGB(Rnd() * 255, Rnd() * 255, Rnd() * 255)
: Creates a random color by generating random values for red, green, and blue components.
-
Creating the Circle:
Set shp = ActiveSheet.Shapes.AddShape(msoShapeOval, x - radius, y - radius, radius * 2, radius * 2)
: Adds a circle (oval shape) at the calculated position and with the random radius. Notice that thex
andy
are adjusted by subtracting the radius so that the center of the circle is at the random coordinates. The width and height of the oval areradius * 2
to make it a circle.
-
Styling the Circle:
.Fill.ForeColor.RGB = color
: Sets the fill color of the circle to the randomly generated color..Line.Visible = msoFalse
: (Optional) Removes the outline around the circle. If you want the outline, either remove this line or set.Line.Visible = msoTrue
.
How to Use:
- Open the VBA editor (Alt + F11).
- Insert a new module (Insert > Module).
- Paste the code into the module.
- Run the macro (F5 or Run > Run Sub/UserForm).
Enhancements (Optional):
- More Control over Colors: Instead of completely random colors, you could define a color palette and randomly choose colors from that palette.
- Varying Transparency: You can add transparency to the circles using
shp.Fill.Transparency = Rnd() * 0.5
(for example, to make them semi-transparent). - Different Shapes: Experiment with other shapes like squares (
msoShapeRectangle
), stars (msoShapeStar
), or even custom shapes. - Background Color: Set a background color for the frame using
ActiveSheet.Range(Cells(top, left), Cells(top + height, left + width)).Interior.Color = RGB(200, 200, 200)
(for a light gray background, for example). Place this before the loop.
This code provides a basic framework. By playing with the parameters and adding more features, you can create even more complex and beautiful works of art!
0 件のコメント:
コメントを投稿