计算星星的五个角 - 方法无法正常工作
Calculate the five corners of a star - method doesn't work correctly
我想计算五角星的五个角。我将第一个点设置在 x = 0.0
和 y = 1.0
的顶部。第二个点的第一次计算是正确的,因为该方法从点数组中获取值。
但是第三点的第二次计算不行。因为它取的是第一次计算的值。当我从点数组中获取新值时,逗号可能有问题。 --> (. and ,) 计算中的值类型总是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]);
}
我想计算五角星的五个角。我将第一个点设置在 x = 0.0
和 y = 1.0
的顶部。第二个点的第一次计算是正确的,因为该方法从点数组中获取值。
但是第三点的第二次计算不行。因为它取的是第一次计算的值。当我从点数组中获取新值时,逗号可能有问题。 --> (. and ,) 计算中的值类型总是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]);
}