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);         
        }
    }
}
2
Contributors
2
Replies
12
Views
5 Years
Discussion Span
Last Post by ddanbe
0

@skatamatic: Because I choose two numbers out of two different domains I guess. You are right about the seeds, I could have left them out.

Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.