C# 按角度排列的圆上的点间距不均匀
鉴于: x=原点x+半径*Cos(角度) y=原点+半径*Sin(角度) 为什么这些点不能均匀地分布在圆的边缘上 结果图像:C# 按角度排列的圆上的点间距不均匀,c#,xna,geometry,angle,C#,Xna,Geometry,Angle,鉴于: x=原点x+半径*Cos(角度) y=原点+半径*Sin(角度) 为什么这些点不能均匀地分布在圆的边缘上 结果图像: class Circle { public Vector2 Origin { get; set; } public float Radius { get; set; } public List<Vector2> Points { get; set; } private Texture2D _texture; publ
class Circle
{
public Vector2 Origin { get; set; }
public float Radius { get; set; }
public List<Vector2> Points { get; set; }
private Texture2D _texture;
public Circle(float radius, Vector2 origin, ContentManager content)
{
Radius = radius;
Origin = origin;
_texture = content.Load<Texture2D>("pixel");
Points = new List<Vector2>();
//i = angle
for (int i = 0; i < 360; i++)
{
float x = origin.X + radius * (float)Math.Cos(i);
float y = origin.Y + radius * (float)Math.Sin(i);
Points.Add(new Vector2(x, y));
}
}
public void Draw(SpriteBatch spriteBatch)
{
for (int i = 0; i < Points.Count; i++)
spriteBatch.Draw(_texture, Points[i], new Rectangle(0, 0, _texture.Width, _texture.Height), Color.Red);
}
}
类圆
{
公共向量2原点{get;set;}
公共浮动半径{get;set;}
公共列表点{get;set;}
私有纹理2D_纹理;
公共圆圈(浮动半径、矢量2原点、ContentManager内容)
{
半径=半径;
原点=原点;
_纹理=内容。加载(“像素”);
点=新列表();
//i=角度
对于(int i=0;i<360;i++)
{
浮动x=原点x+半径*(浮动)数学Cos(i);
浮动y=原点y+半径*(浮动)数学Sin(i);
添加(新矢量2(x,y));
}
}
公共作废抽签(SpriteBatch SpriteBatch)
{
对于(int i=0;i
Math.Cos和Math.Sin以弧度表示的角度与度相反,您的代码应该是:
float x = origin.X + radius * (float)Math.Cos(i*Math.PI/180.0);
float y = origin.Y + radius * (float)Math.Sin(i*Math.PI/180.0);
要点:
1) 三角函数使用弧度,而不是度
2) 对于这种精度,最好使用double
而不是float
3) 计算机图形/游戏专业人士避免使用昂贵的函数,如正弦和余弦,而是使用增量整数像素导向的方法,其结果与直接的三角数学计算一样好或更好,并且速度更快。参见此问题。OP的问题和你的一样。我很长时间没有看到对基于整数的方法的偏好;)现代(但不是很现代)GPU实际上要求您使用
float
@AndrewRussell Right。显然,我不是我提到的那些职业选手之一。:-)其余部分仍然正确吗?#1显然是正确的#2很有趣-因为您通常使用float
来减少数据大小以获得更好的内存性能(GPU几乎完全使用它)。您很少需要双精度的double
(但是您必须能够知道何时需要它)。但C#只提供了double
版本的trig函数#我不确定这个建议是否有效。很少有人想画一个锯齿状的圆轮廓(当你可以在GPU上做一个漂亮的反锯齿粗线条轮廓时),而在现代GPU上像素戳操作相对来说非常慢@AndrewRussell很公平。在这个示例用例中,您甚至需要考虑优化,这是完全不可想象的。使用正反两方面。