Crazy Graphics (using C # + GDI Plus to simulate cluttered real-world scenarios)

Source: Internet
Author: User

Original: Crazy Graphics (using C # + GDI Plus to simulate cluttered reality scenarios)

This paper gives the key codes of simulating bamboo leaves, long blumei, messy stones, and stars in the sky. Use. NET environment in C # language, GDI + is written.

?

Imitation bamboo leaves

Kinda like that, huh? Who says not like, I with who urgent!

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);

Simulating long ye Qing grass

To tell the truth, this is not very similar, too neat, should be more chaotic point.

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);

?

Simulation of irregular stones

This one...... How do some of the stones always come out a little bit more, as if the stone grows grass? If you find out, it means you are a careful person. On the contrary, say you are careless not too, haha!

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.396f), corners);
G.fillclosedcurve (Brushes.gray, points, fillmode.winding);

?

Stars in the sky, sparkling

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, +)/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, 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 & 0xFFFFFFFF) | (int) (tick >> 32));
??????????????????? int corners = Random. Next (Mincorners, maxcorners);
??????????????????? if (Math.Abs (Corners-lastcorners) < (maxcorners-mincorners)/2) corners = Retrievrandomcorners (minCorners, MaxCorn ERS);
??????????????????? 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);
??????? }

?

Crazy Graphics (using C # + GDI Plus to simulate cluttered real-world scenarios)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.