当前位置:编程学习 > C#/ASP.NET >>

疯狂的图形(利用C# + GDI plus模拟杂乱无章的现实场景)

本文给出了模拟竹叶、长叶草、杂乱石头、天上繁星等关键代码。使用.Net环境下C#语言,GDI+编写。
 
 
 
 
 
模拟竹叶
 
//关键参数
 
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);
 
 
 
模拟长叶青草
 
//关键参数
 
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);
 
 
 
 
 
模拟不规则的石块
 
//关键参数:
 
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.396f), corners);
g.FillClosedCurve(Brushes.Gray, points, FillMode.Winding);
 
 
 
 
 
天上星,亮晶晶
 
//关键参数:
 
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);
 
 
 
//关键代码:
 
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;
        }
 
    }
}
 
 
 
测试用代码:
 
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;
&nbs
补充:软件开发 , C# ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,