Koch曲線是一類平面曲線:從一條線段中間三分之一的部分用等邊三角形的兩條邊代替,形成的圖形有四條邊,五個(gè)節(jié)點(diǎn)。再依次組成邊的每條線段做相同的處理。
迭代如
稍微變換一下,畫(huà)成六邊形的,凸起方向向兩邊,用隨機(jī)顏色,畫(huà)完后為雪花狀。
程序代碼:
private void LayoutRoot_Loaded(object sender, RoutedEventArgs ea)
{
double sqrt3 = Math.Sqrt(3);
Point a = new Point(50, (float)(100 + 50 * sqrt3));
Point b = new Point(100, (float)(100 + 100 * sqrt3));
Point c = new Point(200, (float)(100 + 100 * sqrt3));
Point d = new Point(250, (float)(100 + 50 * sqrt3));
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)); //角度
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);
}
}運(yùn)行結(jié)果如
Koch曲線的每一部分都由4個(gè)跟它自身比例為1:3的形狀相同的小曲線組成,那么它的豪斯多夫維數(shù)(分維數(shù))為d=log(4)/log(3) =1.26185950714...
北京 | 天津 | 上海 | 江蘇 | 山東 |
安徽 | 浙江 | 江西 | 福建 | 深圳 |
廣東 | 河北 | 湖南 | 廣西 | 河南 |
海南 | 湖北 | 四川 | 重慶 | 云南 |
貴州 | 西藏 | 新疆 | 陜西 | 山西 |
寧夏 | 甘肅 | 青海 | 遼寧 | 吉林 |
黑龍江 | 內(nèi)蒙古 |