计算星形的五个角——方法不正确
本文关键字:五个 方法 不正确 计算 | 更新日期: 2023-09-27 18:18:10
我想计算五角星的五个角。我把第一个点放在顶部的x = 0.0
和y = 1.0
。第二个点的第一次计算是正确的,因为该方法从点数组中获取值。
但是第三个点的第二次计算不起作用。因为它取第一次计算的值。当我从点数组中获取新值时,也许逗号有问题。——>(。计算中的值类型始终为double且正确。
问题:我总是得到最后三个星角位置的输出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;
}
}
}
您可能想要旋转前一个向量而不是当前向量:
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]);
}