如何制作移动点和连接点?

时间:2019-04-15 14:48:16

标签: c# .net

我正在尝试做这样的事情,

https://www.youtube.com/watch?v=elHqinIm4RI

但使用c#。

我想要例如100个点,它们从随机位置开始,然后首先沿随机方向移动。当它们与形状的一侧碰撞时,它们应该弹起。因此,它类似于乒乓球。 并且在给定半径范围内的所有其他点都应在它们之间绘制一条线,仅当它们之间的距离超过给定半径时才断开。

int i = 0;
Random rndm = new Random();
Brush aBrush = (Brush)Brushes.Black;

这只会在随机位置绘制点,但是我认为这不会让我到我想要的地方

for (; i < 3456; i++) 
{
  e.Graphics.FillEllipse(aBrush, rndm.Next(0, 801), rndm.Next(0, 601), 3, 3);
  //Thread.Sleep(1); //allows for smoother buildup
}

1 个答案:

答案 0 :(得分:1)

同意... WinForms不是此类应用程序的最佳选择。

话虽如此,下面是一些严重不足代码可帮助您入门:

public partial class Form1 : Form
{

    private int radius = 6;
    private int jiggleDistance = 3;      
    private int numberOfDots = 100;
    private Random R = new Random();
    private int lineDistanceThreshold = 50;
    private List<Point> dots = new List<Point>();
    private System.Windows.Forms.Timer tmr = new System.Windows.Forms.Timer();

    public Form1()
    {
        InitializeComponent();
        this.DoubleBuffered = true;
        this.WindowState = FormWindowState.Maximized;
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        populateDots();
        tmr.Tick += Tmr_Tick;
        tmr.Interval = 100;
        tmr.Start();
    }

    private void populateDots()
    {
        dots.Clear();
        for (int i = 1; i <= numberOfDots; i++)
        {
            Point dot = new Point(
                R.Next(radius, this.ClientRectangle.Width - radius),
                R.Next(radius, this.ClientRectangle.Height - radius));
            dots.Add(dot);
        }
    }

    private void Tmr_Tick(object sender, EventArgs e)
    {
        for(int i=0; i < dots.Count; i++)
        {
            Point dot = dots[i];
            dot.Offset(R.Next(-1, 2) * jiggleDistance, R.Next(-1, 2) * jiggleDistance);
            if (dot.X < radius)
            {
                dot = new Point(radius, dot.Y);
            }
            if (dot.X > this.ClientRectangle.Width - radius)
            {
                dot = new Point(this.ClientRectangle.Width - radius, dot.Y);
            }
            if (dot.Y < radius)
            {
                dot = new Point(dot.X, radius);
            }
            if (dot.Y > this.ClientRectangle.Height - radius)
            {
                dot = new Point(dot.X, this.ClientRectangle.Height - radius);
            }
            dots[i] = dot;
        }
        this.Invalidate();
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);

        // connecting lines: *very ineffecient, draws lines twice
        foreach(Point dot in dots)
        {
            foreach(Point otherDot in dots)
            {
                if (otherDot != dot)
                {
                    if(dotToDotDistance(dot, otherDot) <= lineDistanceThreshold)
                    {
                        e.Graphics.DrawLine(Pens.Blue, dot, otherDot);
                    }
                }
            }
        }
        // dots on top of lines:
        foreach (Point dot in dots)
        {
            Rectangle rc = new Rectangle(dot, new Size(1, 1));
            rc.Inflate(radius, radius);
            e.Graphics.FillEllipse(Brushes.Black, rc);
        }
    }

    private double dotToDotDistance(Point ptA, Point ptB)
    {
        return Math.Sqrt(Math.Pow(ptB.X - ptA.X, 2) + Math.Pow(ptB.Y - ptA.Y, 2));
    }

}

带有连接线的抖动点的屏幕截图: enter image description here