This article provides key code for simulating bamboo leaves, Sloga leaves, messy stones, and stars in the sky. Use the C # language in the. Net environment, and write it using GDI +.
Bamboo Simulation
// Key Parameters
Int minCorners = 3;
Int maxCorners = 4;
PointF [] points = Stone. createStone (new Point (int) (perX * j), (int) (perY * I), (int) (perX * 1.4f), (int) (perX * 0.009f), corners );
Simulate long-leaf grass
// Key Parameters
Int minCorners = 20;
Int maxCorners = 38;
PointF [] points = Stone. createStone (new Point (int) (perX * j), (int) (perY * I), (int) (perX * 0.88f), (int) (perX * 0.01f), corners );
G. FillClosedCurve (Brushes. Green, points, FillMode. Winding );
Simulate irregular stones
// Key parameters:
Int minCorners = 3;
Int maxCorners = 4;
PointF [] points = Stone. createStone (new Point (int) (perX * j), (int) (perY * I), (int) (perX * 0.4f), (int) (perX * 0.20.f), corners );
G. FillClosedCurve (Brushes. Gray, points, FillMode. Winding );
The sky is shining
// Key parameters:
Int minCorners = 3;
Int maxCorners = 4;
PointF [] points = Stone. createStone (new Point (int) (perX * j), (int) (perY * I), (int) (perX * 0.18f), (int) (perX * 0.06f), corners );
G. FillClosedCurve (Brushes. White, points, FillMode. Winding );
// Key code:
Using System;
Using System. Collections. Generic;
Using System. Drawing;
Using System. Text;
Namespace ZPXP. Com. Util
{
Public static class Stone
{
Public static PointF [] CreateStone (Point center, int outerRadius, int inner_radius, int arms)
{
Int center_x = center. X;
Int center_y = center. Y;
PointF [] points = new PointF [arms * 2];
Double offset = Math. PI/2;
Double arc = 2 * Math. PI/arms;
Double half = arc/2;
Double angle = 0;
For (int I = 0; I <arms; I ++)
{
Random randomOuter = new Random (int) DateTime. Now. Ticks );
OuterRadius = outerRadius-randomOuter. Next (int) (inner_radius * 0.06 * new Random (). Next (-20, 20)/30d), (int) (inner_radius * 0.08 ));
// OuterRadius = outerRadius-randomOuter. next (int) (inner_radius * 0.16 * new Random (). next (-20, 20)/30d), (int) (inner_radius * 0.18 ));
Random randomInner = new Random (Guid. NewGuid (). GetHashCode ());
Inner_radius = inner_radius + randomInner. Next (int) (inner_radius * 0.02 * new Random (). Next (-100,100)/150d), (int) (inner_radius * 0.08 ));
// Inner_radius = inner_radius + randomInner. next (int) (inner_radius * 0.02 * new Random (). next (-100,100)/150d), (int) (inner_radius * 0.22 ));
If (inner_radius> outerRadius)
{
Int temp = outerRadius;
OuterRadius = inner_radius;
Inner_radius = temp;
}
Double angleTemp = arc * randomInner. Next (-5, 5)/10d;
Angle = I * arc;
Angle + = angleTemp;
Points [I * 2]. X = (float) (center_x + Math. Cos (angle-offset) * outerRadius );
Points [I * 2]. Y = (float) (center_y + Math. Sin (angle-offset) * outerRadius );
Points [I * 2 + 1]. X = (float) (center_x + Math. Cos (angle + half-offset) * inner_radius );
Points [I * 2 + 1]. Y = (float) (center_y + Math. Sin (angle + half-offset) * inner_radius );
}
Return points;
}
}
}
Code for testing:
Private void button3_Click (object sender, EventArgs e)
{
Int width = 500;
Int height = 500;
Int x = 0;
Int y = 0;
Int numX = 10;
Int numY = 10;
Float perX = width * 1f/numX;
Float perY = height * 1f/numY;
Bitmap image = new Bitmap (width, height );
Graphics g = Graphics. FromImage (image );
G. CompositingQuality = CompositingQuality. HighQuality;
G. SmoothingMode = SmoothingMode. HighQuality;
G. InterpolationMode = InterpolationMode. HighQualityBicubic;
G. FillRectangle (Brushes. Black, new Rectangle (0, 0, width, height ));
Int minCorners = 3;
Int maxCorners = 18;
Int lastCorners = minCorners;
For (int I = 0; I <numX; I ++)
{
For (int j = 0; j <numY; j ++)
{
Long tick = DateTime. Now. Ticks;
Random random = new Random (int) (tick & 0 xffffffff) | (int) (tick> 32 ));
Int corners = random. Next (minCorners, maxCorners );
If (Math. Abs (corners-lastCorners) <(maxCorners-minCorners)/2) corners = RetrievRandomCorners (minCorners, maxCorners );
LastCorners = corners;
// PointF [] points = Stone. createStone (new Point (int) (perX * j), (int) (perY * I), (int) (perX * 0.9f), (int) (perX * 0.8f), corners );
PointF [] points = Stone. createStone (new Point (int) (perX * j), (int) (perY * I), (int) (perX * 0.18f), (int) (perX * 0.06f), corners );
G. FillClosedCurve (Brushes. White, points, FillMode. Winding );
}
}
PicEncode. Image = image;
}
Private int RetrievRandomCorners (int minCorners, int maxCorners)
{
Return new Random (Guid. NewGuid (). GetHashCode (). Next (minCorners, maxCorners );
}