Pi Walk simulator
Alweer geinspireert door FB post. Simpel idee: geef de getallen 0-9 oplopend 0, 36, 72 etc graden en teken dan de lijn die ontstaat bij de decimalen van Pi. De code is simpel, het resultaat facinerend.
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.IO;
public class MainForm : Form
{
private readonly List<PointF> points = new List<PointF>();
private Timer timer;
private string digits;
private int currentIndex = 0;
private float x = 0;
private float y = 0;
private float scale = 20f;
Color[] colors =
{
Color.Cyan,
Color.Lime,
Color.Yellow,
Color.Orange,
Color.Red,
Color.Magenta,
Color.White
};
public MainForm()
{
// InitializeComponent();
Text = "Pi Walk Simulator";
Width = 1200;
Height = 900;
DoubleBuffered = true;
points.Add(new PointF(0, 0));
digits = GetPiDigits();
timer = new Timer();
timer.Interval = 10; // milliseconden
timer.Tick += Timer_Tick;
timer.Start();
}
private void GenerateWalk(int steps)
{
float x = 0, y = 0;
points.Add(new PointF(x,y));
string digits = File.ReadAllText("10m.txt");
for (int i = 0; i < Math.Min(steps, digits.Length); i++)
{
int d = digits[i] - '0';
double angle = d * 36.0 * Math.PI / 180.0;
x += (float)Math.Cos(angle);
y += (float)Math.Sin(angle);
points.Add(new PointF(x,y));
}
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
if (points.Count < 2)
return;
Graphics g = e.Graphics;
g.Clear(Color.Black);
g.SmoothingMode =
System.Drawing.Drawing2D.SmoothingMode.HighSpeed;
float ox = ClientSize.Width / 2f;
float oy = ClientSize.Height / 2f;
Pen[] pens =
{
new Pen(Color.Cyan, 1),
new Pen(Color.Lime, 1),
new Pen(Color.Yellow, 1),
new Pen(Color.Orange, 1),
new Pen(Color.Red, 1),
new Pen(Color.Magenta, 1),
new Pen(Color.White, 1),
new Pen(Color.DeepSkyBlue, 1)
};
const int segmentSize = 5000;
for (int i = 1; i < points.Count; i++)
{
int colorIndex =
(i / segmentSize) % pens.Length;
g.DrawLine(
pens[colorIndex],
ox + points[i - 1].X * scale,
oy + points[i - 1].Y * scale,
ox + points[i].X * scale,
oy + points[i].Y * scale);
}
using (Font f = new Font("Segoe UI", 10))
{
g.DrawString(
"Digits processed: " +
currentIndex.ToString("N0"),
f,
Brushes.White,
10,
10);
}
foreach (Pen pen in pens)
{
pen.Dispose();
}
}
private string GetPiDigits()
{
string digits = File.ReadAllText("10m.txt");
digits = digits.Replace("\r", "")
.Replace("\n", "")
.Replace(" ", "");
if (digits.StartsWith("3."))
{
digits = digits.Substring(2);
}
return digits;
}
private void Timer_Tick(object sender, EventArgs e)
{
for (int n = 0; n < 50; n++)
{
if (currentIndex >= digits.Length)
{
timer.Stop();
break;
}
int d = digits[currentIndex] - '0';
x += (float)Math.Cos(angle);
y += (float)Math.Sin(angle);
points.Add(new PointF(x, y));
currentIndex++;
}
AutoZoom();
Invalidate();
}
private void AutoZoom()
{
float maxX = 0;
float maxY = 0;
foreach (PointF p in points)
{
maxX = Math.Max(maxX, Math.Abs(p.X));
maxY = Math.Max(maxY, Math.Abs(p.Y));
}
float margin = 50;
float scaleX = (ClientSize.Width - margin) / (2 * maxX + 1);
float scaleY = (ClientSize.Height - margin) / (2 * maxY + 1);
scale = Math.Min(scaleX, scaleY);
if (scale < 1f)
scale = 1f;
}
}
