Created
October 20, 2020 17:19
-
-
Save edward1986/34c8c119da3c4620ba6cd4716d036e66 to your computer and use it in GitHub Desktop.
its a illustrated koch snowflake
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.ComponentModel; | |
using System.Data; | |
using System.Drawing; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using System.Windows.Forms; | |
namespace snowflake_ | |
{ | |
public partial class Form1 : Form | |
{ | |
public Form1() | |
{ | |
InitializeComponent(); | |
this.Paint += new PaintEventHandler(Form1_Paint); | |
} | |
private void Form1_Paint(object sender, PaintEventArgs e) | |
{ | |
DrawKochSnow(e.Graphics); | |
} | |
private void figure (Point p1, Point p2, Graphics g) // Polyline composed of 4 basic line segments | |
{ | |
Point p3 = new Point(p1.X + (p2.X-p1.X) / 3, p1.Y + (p2.Y-p1.Y) / 3); // coordinates of the third point | |
Point p4 = new Point(p1.X + (p2.X-p1.X) * 2 / 3, p1.Y + (p2.Y-p1.Y) * 2 / 3); // coordinates of the third point | |
Point p4XD3 = new Point(p4.X-p3.X, p4.Y-p3.Y); // the coordinates of p4 relative to p3 | |
//int x = (int)(p4XD3.X * Math.Cos(Math.PI / 3)-p4XD3.Y * Math.Sin(Math.PI / 3)); | |
//int y = (int)(p4XD3.X * Math.Sin(Math.PI / 3) + p4XD3.Y * Math.Cos(Math.PI / 3)); | |
// Note that the vertical coordinate of the computer screen is opposite to the mathematical one, so mathematically rotating counterclockwise is equivalent to clockwise rotating on the computer | |
int x = (int)Math.Round(p4XD3.X * Math.Cos(Math.PI / 3) + p4XD3.Y * Math.Sin(Math.PI / 3)); | |
int y = (int)Math.Round(p4XD3.Y * Math.Cos(Math.PI / 3)-p4XD3.X * Math.Sin(Math.PI / 3)); | |
Point p5XD3 = new Point(x, y); // The coordinates of the raised point p5 relative to the point p3 | |
Point p5 = new Point(p3.X + x, p3.Y + y); // the coordinates of p5 relative to the origin | |
Pen pen = new Pen(Brushes.Black, 1); | |
double length = Math.Sqrt(Math.Pow(p2.X-p1.X, 2) + Math.Pow(p2.Y-p1.Y, 2)) / 3; | |
//Console.WriteLine(length); | |
if (length> 20) // The iteration can be controlled by the length of the final line segment | |
{ | |
figure(p1, p3, g); | |
figure(p3, p5, g); | |
figure(p5, p4, g); | |
figure(p4, p2, g); | |
} | |
else | |
{ | |
g.DrawLine(pen, p1, p3); | |
g.DrawLine(pen, p3, p5); | |
g.DrawLine(pen, p5, p4); | |
g.DrawLine(pen, p4, p2); | |
} | |
} | |
private void DrawKochSnow(Graphics g) // Koch snowflake (the Swedish Koch proposed the famous "snowflake" curve in 1904) | |
{ | |
int length = 480; | |
Point origin = new Point(this.ClientSize.Width / 2, this.ClientSize.Height / 2); | |
g.FillEllipse(Brushes.Blue, new RectangleF(origin, new Size(10, 10))); | |
// Calculate the vertices of the triangle so that its center coincides with the center of the window | |
Point A = new Point(origin.X-length / 2, (int)(origin.Y + length / (2 * Math.Sqrt(3)))); | |
Point B = new Point(origin.X, (int)(origin.Y-length / Math.Sqrt(3))); | |
Point C = new Point(origin.X + length / 2, (int)(origin.Y + length / (2 * Math.Sqrt(3)))); | |
figure(A, B, g); | |
figure(B, C, g); | |
figure(C, A, g); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment