2015-10-18 37 views
0

我想计算五点恒星的五个角。我在x = 0.0y = 1.0上设置了第一个顶点。第二个点的第一个计算是正确的,因为该方法从点数组中获取值。计算恒星的五角 - 方法无法正常工作

但第三点的第二次计算不起作用。因为它需要第一次计算的值。也许当我从点数组中获得新值时,逗号会出现问题。 - >(。和,)计算中的值类型始终为双精度。

问题:我总是得到星形角最后三个位置的输出0,0。

using System; 
using System.Drawing; 
using System.Windows.Forms; 
using System.Threading; 
using System.Diagnostics; 

// draw a 5 point star 

namespace WindowsFormsApplication10 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
     } 

     private void Form1_Paint(object sender, PaintEventArgs e) 
     { 
      System.Drawing.Pen myPen = new System.Drawing.Pen(System.Drawing.Color.Red); 
      Graphics g = this.CreateGraphics(); 

      double[,] points = new double[5, 2] { 
       { 0.0, 1.0 }, 
       { 0.0, 0.0 }, 
       { 0.0, 0.0 }, 
       { 0.0, 0.0 }, 
       { 0.0, 0.0 } 
      }; 

      // debuging 
      // first value?     correct 
      // calculation second value? correct 
      // problem: doesn't take second value to calculation 
      //   type --> is always double 

      // calculation 
      for (int i = 0; i < 5; i++) 
      { 
       double[] newVector = RotateVector2d(points[i, 0], points[i, 1], 2.0*Math.PI/5); // degrees in rad ! 
       points[i, 0] = newVector[0]; 
       points[i, 1] = newVector[1]; 
       Debug.WriteLine(newVector[0] + " " + newVector[1]); 
      } 

      // drawing 
      for (int i = 0; i < 5; i++) 
      { 
       g.DrawLine(myPen, 100, 100, 100 + 50*Convert.ToSingle(points[i,0]) , 100 + 50*Convert.ToSingle(points[i, 1])); 
      } 

      myPen.Dispose(); 
      g.Dispose(); 
     } 

     static double[] RotateVector2d(double x, double y, double degrees) 
     { 
      Debug.WriteLine("calculating rotation"); 
      double[] result = new double[2]; 
      result[0] = x * Math.Cos(degrees) - y * Math.Sin(degrees); 
      result[1] = x * Math.Sin(degrees) - y * Math.Cos(degrees); 
      return result; 
     } 
    } 
} 
+0

一个建议的话,无关你的问题,但是你应该避免在表格内订阅到窗体的事件本身。例如,'Form1_Paint',你应该重写'OnPaint'。当另一个“消费者”接收事件时使用事件。 –

回答

2

你可能想以前的载体,而不是当前的旋转:

for (int i = 1; i < 5; i++) 
{ 
    double[] newVector = RotateVector2d(points[i - 1, 0], points[i - 1, 1], 2.0*Math.PI/5); // degrees in rad ! 
    points[i, 0] = newVector[0]; 
    points[i, 1] = newVector[1]; 
    Debug.WriteLine(newVector[0] + " " + newVector[1]); 
}