• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

利用C# + GDI plus模拟杂乱无章的现实场景

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文给出了模拟竹叶、长叶草、杂乱石头、天上繁星等关键代码。使用.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;
            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 & 0xffffffff) | (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);
        }


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C#4.0Dynamic关键字全解析发布时间:2022-07-14
下一篇:
C#中委托和事件的关系发布时间:2022-07-14
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap