简体   繁体   中英

How do i draw the trail of a button movement?

I am trying to make an application which controls, through a trackbar , the movement of 3 different buttons.

The buttons move towards a center point as well as in a circular way. What I intend to do is to draw the trail of the buttons movements: every time a button changes its location color that pixel it "lands" on.

I am coding in C#, a WFA project.

This is the code for the movement of buttons:

private void trackBar1_Scroll(object sender, EventArgs e)
    {
        //161, 114
        double sinX, cosX, sinY, cosY, sinZ, cosZ;
        sinX = Math.Sin(trackBar1.Value / 57.29);
        cosX = Math.Cos(trackBar1.Value / 57.29);
        sinX = Math.Truncate((360 - trackBar1.Value)/4 * sinX);
        cosX = Math.Truncate((360 - trackBar1.Value) / 4 * cosX);
        button1.Location = new System.Drawing.Point(161 + (int)sinX, 114 + (int)cosX);

        sinY = Math.Sin((trackBar1.Value + 120) / 57.29);
        cosY = Math.Cos((trackBar1.Value + 120)/ 57.29);
        sinY = Math.Truncate((360 - trackBar1.Value) / 4 * sinY);
        cosY = Math.Truncate((360 - trackBar1.Value) / 4 * cosY);
        button2.Location = new System.Drawing.Point(161 + (int)sinY, 114 + (int)cosY);

        sinZ = Math.Sin((trackBar1.Value + 240) / 57.29);
        cosZ = Math.Cos((trackBar1.Value + 240) / 57.29);
        sinZ = Math.Truncate((360 - trackBar1.Value) / 4 * sinZ);
        cosZ = Math.Truncate((360 - trackBar1.Value) / 4 * cosZ);
        button3.Location = new System.Drawing.Point(161 + (int)sinZ, 114 + (int)cosZ);

    } 

Moves like this towards a center point.

Generate a filed in your form that can hold all the points List track = new List();

Save the first location of button in the list in the constructor:

public MyForm()
{
    InitializeComponent();
    // Make sure you put this code after the InitializeComponent:
    this.track.Add(button3.Location)
}

then store the positions of your button in that list on move event:

private void trackBar1_Scroll(object sender, EventArgs e)
{
    //rest of your code
    button3.Location = new System.Drawing.Point(161 + (int)sinZ, 114 + (int)cosZ);
    this.track.Add(button3.Location)
} 

and then handle the Form.Paint method as below:

private void MyForm_Paint(object sender, PaintEventArgs e)
{
    Pen pen = new Pen(Color.Red);
    foreach(Point point in track)
    {
        Rectangle rect = new Rectangle(point, new Size(1,1));
        e.Graphics.DrawRectangle(pen, rect);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM