为什么终点不能像我想要的那样工作?

时间:2017-07-29 18:37:46

标签: c# winforms

我正在尝试按照此视频https://www.youtube.com/watch?v=Jc5eXYwTROg&list=PLAKPUAwWLX6STF4YdOGJzAk-C6thSs4Xu&index=15进行操作,但我似乎无法找到问题所在。似乎结束坐标不会改变它的方式 应该。任何人都可以帮忙解决它吗?

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 专题作业
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        startx = panel2.Width / 2;
        starty = panel2.Height / 2;
    }
    Pen pen = new Pen(Color.Black);
    Graphics g = null;
    static int length = 0;
    static int lines = 0;
    static int increment = 0;
    static int angle = 0;

    static int centerx, centery, startx, starty, endx, endy;

    private void button1_Click(object sender, EventArgs e)
    {
        length = Int32.Parse(lengthbox.Text);
        increment = Int32.Parse(incrementbox.Text);
        angle = Int32.Parse(anglebox.Text);
        startx = panel2.Width / 2;
        starty = panel2.Height / 2;
        panel2.Refresh();
    }

    private void panel2_Paint(object sender, PaintEventArgs e)
    {
        pen.Width = 1;
        length = Int32.Parse(lengthbox.Text);
        g = panel2.CreateGraphics();
        for(int a = 0; a < Int32.Parse(linesbox.Text);a++)
            drawLine();            
    }

    private void drawLine()
    {
        angle += Int32.Parse(anglebox.Text);
        length += Int32.Parse(incrementbox.Text);

        endx = (int)(startx + Math.Cos(angle * .17453292519) * length);
        endy = (int)(starty + Math.Sin(angle * .17453292519) * length);
        Point[] p =
        {
            new Point(startx, starty),
            new Point(endx, endy)
        };
        startx = endx;
        starty = endy;
        g.DrawLines(pen, p);
    }
}
}

1 个答案:

答案 0 :(得分:0)

我将角度更改为math.pi * angle / 180而不是角度* 0.17453292519并且它有效。