Silverlight Draw fractal (Koch curve)

Source: Internet
Author: User
Tags abs silverlight

Koch curve is a kind of plane curve: From a segment of the middle One-third of the section with equilateral two edge, the formation of the graph has four edges, five nodes. Then make the same processing for each line segment of the edge in turn.

Iterations as shown:

A little change, painted into hexagonal, raised direction to both sides, with random color, painted after the snowflake-shaped.

Program code:

private void Layoutroot_loaded (object sender, RoutedEventArgs ea)
{
Double sqrt3 = math.sqrt (3);
Point A = new Point ((float) (+ sqrt3));
Point B = new Point ((float) (+ + * sqrt3));
Point C = new Point (float) (+ + * sqrt3));
Point d = new Point (Sqrt3, (float) (+ +));
Point e = new Point (200, 100);
Point f = new Point (100, 100);
Line (A, B, 5);
Line (b, a, 5);
Line (b, C, 5);
Line (c, B, 5);
Line (c, D, 5);
Line (d, C, 5);
Line (d, E, 5);
Line (E, D, 5);
Line (E, F, 5);
Line (f, E, 5);
Line (F, a, 5);
Line (A, F, 5);
}
private void Line (point A, point B, int n)
{
if (n > 0)
{
Double r = Math.atan (Math.Abs (A.Y-B.Y)/Math.Abs (a.x-b.x)); Angle
Double v = 0;
Point C = new Point (a.x + (b.x-a.x)/3, A.Y + (B.Y-A.Y)/3);
Point d = new Point (a.x + 2 * (b.x-a.x)/3, A.Y + 2 * (B.Y-A.Y)/3);
Double L = math.sqrt ((c.x-d.x) * (c.x-d.x) + (C.Y-D.Y) * (C.Y-D.Y));
Point e;
if (b.y-a.y >= 0 && b.x-a.x > 0)
{
v = r;
}
else if (b.y-a.y > 0 && b.x-a.x <= 0)
{
v = math.pi-r;
}
else if (b.y-a.y <= 0 && b.x-a.x < 0)
{
v = Math.PI + R;
}
else if (B.y-a.y < 0 && b.x-a.x >= 0)
{
v = 2 * math.pi-r;
}
E = new Point ((float) (L * Math.Cos (v + math.pi/3) + c.x), (float) (c.y + L * Math.sin (v + math.pi/3)));
Line (A, C, n-1);
Line (c, E, n-1);
Line (E, D, n-1);
Line (d, b, n-1);
}
Else
{
Line L = new Line ();
Random rnd = new Random ();
Color C = Color.FromArgb (255, (byte) rnd. Next (255), (byte) rnd. Next (255), (byte) rnd. Next ());
L.stroke = new SolidColorBrush (c);
l.x1 = a.x;
L.y1 = A.Y;
l.x2 = b.x;
L.y2 = B.Y;
LAYOUTROOT.CHILDREN.ADD (l);
}
}

Run the results as shown in figure:

Each part of the Koch curve consists of 4 small curves with the same shape as its own ratio of 1:3, then its Hausdorff dimension number (fractal dimension) is D=log (4)/log (3) =1.26185950714 ...

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.