Skip to content

Instantly share code, notes, and snippets.

@fujidig
Created April 1, 2016 21:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fujidig/392ffd0343b5b9c7c160a8575a0d4552 to your computer and use it in GitHub Desktop.
Save fujidig/392ffd0343b5b9c7c160a8575a0d4552 to your computer and use it in GitHub Desktop.
c#ランダムウォーク
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 WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Label label = new Label();
label.Text = "Hello, World!";
label.Font = new Font("Verdana", 20, FontStyle.Regular);
label.Height = 30;
label.Width = 300;
this.Controls.Add(label);
Bitmap canvas = new Bitmap(pictureBox1.Width, pictureBox1.Height);
Graphics g = Graphics.FromImage(canvas);
double px = 150, py = 150;
Random random = new System.Random();
for (int i = 0; i < 1000; i++)
{
double x = 2 * random.NextDouble() - 1;
double y = 2 * random.NextDouble() - 1;
double r = Math.Sqrt(x * x + y * y);
x *= 3 / r;
y *= 3 / r;
g.DrawLine(Pens.Black, (float)px, (float)py, (float)(px + x), (float)(py + y));
px += x;
py += y;
}
g.Dispose();
pictureBox1.Image = canvas;
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment