@@ -76,5 +76,83 @@ public static PlotModel AnnotationLayerProperty()
7676 model . Series . Add ( new FunctionSeries ( Math . Sin , - 20 , 30 , 400 ) ) ;
7777 return model ;
7878 }
79+
80+ [ Example ( "Koch Snowflakes" ) ]
81+ public static PlotModel KockSnowflakes ( )
82+ {
83+ DataPoint [ ] triangle ( DataPoint centre )
84+ {
85+ return new [ ]
86+ {
87+ new DataPoint ( centre . X , centre . Y + 1 ) ,
88+ new DataPoint ( centre . X + Math . Sin ( Math . PI * 2 / 3 ) , centre . Y + Math . Cos ( Math . PI * 2 / 3 ) ) ,
89+ new DataPoint ( centre . X + Math . Sin ( Math . PI * 4 / 3 ) , centre . Y + Math . Cos ( Math . PI * 4 / 3 ) ) ,
90+ } ;
91+ }
92+
93+ var model = new PlotModel { Title = "PolygonAnnotation" , PlotType = PlotType . Cartesian } ;
94+ model . Axes . Add ( new LinearAxis { Position = AxisPosition . Bottom , Minimum = - 4 , Maximum = 4 } ) ;
95+ model . Axes . Add ( new LinearAxis { Position = AxisPosition . Left , Minimum = - 2 , Maximum = 2 } ) ;
96+
97+ var a1 = new PolygonAnnotation { Text = "MSL = 4" , MinimumSegmentLength = 4 } ;
98+ a1 . Points . AddRange ( KochFractal ( triangle ( new DataPoint ( - 2 , 0 ) ) , 8 , true , true ) ) ;
99+ model . Annotations . Add ( a1 ) ;
100+
101+ var a2 = new PolygonAnnotation { Text = "MSL = 2" , MinimumSegmentLength = 2 } ;
102+ a2 . Points . AddRange ( KochFractal ( triangle ( new DataPoint ( 0 , 0 ) ) , 8 , true , true ) ) ;
103+ model . Annotations . Add ( a2 ) ;
104+
105+ var a3 = new PolygonAnnotation { Text = "MSL = 1" , MinimumSegmentLength = 1 } ;
106+ a3 . Points . AddRange ( KochFractal ( triangle ( new DataPoint ( 2 , 0 ) ) , 8 , true , true ) ) ;
107+ model . Annotations . Add ( a3 ) ;
108+
109+ return model ;
110+ }
111+
112+ public static DataPoint [ ] KochFractal ( DataPoint [ ] seed , int n , bool clockwise , bool closed )
113+ {
114+ var cos60 = Math . Cos ( Math . PI / 3 ) ;
115+ var sin60 = Math . Sin ( Math . PI / 3 ) ;
116+ var cur = seed ;
117+
118+ for ( int i = 0 ; i < n ; i ++ )
119+ {
120+ var next = new DataPoint [ closed ? cur . Length * 4 : cur . Length * 4 - 3 ] ;
121+ for ( int j = 0 ; j < ( closed ? cur . Length : cur . Length - 1 ) ; j ++ )
122+ {
123+ var p0 = cur [ j ] ;
124+ var p1 = cur [ ( j + 1 ) % cur . Length ] ;
125+
126+ var dx = ( p1 . X - p0 . X ) / 3 ;
127+ var dy = ( p1 . Y - p0 . Y ) / 3 ;
128+
129+ double dx2 , dy2 ;
130+ if ( clockwise )
131+ {
132+ dx2 = cos60 * dx - sin60 * dy ;
133+ dy2 = cos60 * dy + sin60 * dx ;
134+ }
135+ else
136+ {
137+ dx2 = cos60 * dx - sin60 * dy ;
138+ dy2 = cos60 * dy + sin60 * dx ;
139+ }
140+
141+ next [ j * 4 ] = p0 ;
142+ next [ j * 4 + 1 ] = new DataPoint ( p0 . X + dx , p0 . Y + dy ) ;
143+ next [ j * 4 + 2 ] = new DataPoint ( p0 . X + dx + dx2 , p0 . Y + dy + dy2 ) ;
144+ next [ j * 4 + 3 ] = new DataPoint ( p0 . X + dx * 2 , p0 . Y + dy * 2 ) ;
145+ }
146+
147+ if ( ! closed )
148+ {
149+ next [ next . Length - 1 ] = cur [ cur . Length - 1 ] ;
150+ }
151+
152+ cur = next ;
153+ }
154+
155+ return cur ;
156+ }
79157 }
80- }
158+ }
0 commit comments