This is code to plot a "random walk" in polar coordinates.See also: http://www.daniweb.com/software-development/csharp/code/379275
Just posted the code of my main form, more details can be found in the zip file.
I also included a sample output.
I perhaps intend to use this in some sort of game, were a biologist has to track down an animal with a sender device attached.
Let's take a walk on the wild side!
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace RandomWalk
{
public partial class Form1 : Form
{
private List<PointF> Path;
private int NPts = 0;
private int size = 0;
private Plot myPlot;
private float X_Old = 0f;
private float Y_Old = 0f;
PointF aPnt = new PointF();
int MaxMoveLen = 0;
private Random RandLen = new Random(1);
private Random RandAngle = new Random(2);// 1 and 2, just two seeds, nothing special
public Form1()
{
InitializeComponent();
Path = new List<PointF>();
myPlot = new Plot(panel1.Size, panel1.Size);
}
private void DrawBtn_Click(object sender, EventArgs e)
{
bool OK = int.TryParse(NMovesTxb.Text, out NPts);
if (OK)
{
if (int.TryParse(MoveLengthTbx.Text, out MaxMoveLen))
{
if (int.TryParse(AreaSizeTxb.Text, out size))
{
myPlot = new Plot(new Size(size,size),this.panel1.Size);
panel1.Refresh();
}
}
}
else
{ }
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
myPlot.PlotAxes(e.Graphics);
Path.Clear();
float r = 0f;
int ang = 0;
double RadConv = Math.PI / 180.0;
X_Old = 0f;
Y_Old = 0f;
aPnt.X = 0f;
aPnt.Y = 0f;
float X_New = 0f;
float Y_New = 0f;
Path.Add(aPnt);
for (int i = 0; i < NPts; i++)
{
r = RandLen.Next(0, MaxMoveLen);
ang = RandAngle.Next(0, 361);
X_New = X_Old + r * (float)Math.Cos(ang * RadConv);
Y_New = Y_Old + r * (float)Math.Sin(ang * RadConv);
aPnt.X = X_New;
aPnt.Y = Y_New;
Path.Add(aPnt);
X_Old = X_New;
Y_Old = Y_New;
}
myPlot.PlotPoints(Path, Pens.Black, e.Graphics);
}
}
}
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.