C# 画一个圆
在XNA上的另一个线程中,它创建了一个具有圆形轮廓的纹理,但我正在尝试创建一个填充颜色的圆形。为了用颜色填充圆圈,我必须对代码进行哪些修改C# 画一个圆,c#,math,c#-4.0,xna,xna-4.0,C#,Math,C# 4.0,Xna,Xna 4.0,在XNA上的另一个线程中,它创建了一个具有圆形轮廓的纹理,但我正在尝试创建一个填充颜色的圆形。为了用颜色填充圆圈,我必须对代码进行哪些修改 public Texture2D CreateCircle(int radius) { int outerRadius = radius*2 + 2; // So circle doesn't go out of bounds Texture2D texture = new Texture2D(GraphicsDevice, outerRad
public Texture2D CreateCircle(int radius)
{
int outerRadius = radius*2 + 2; // So circle doesn't go out of bounds
Texture2D texture = new Texture2D(GraphicsDevice, outerRadius, outerRadius);
Color[] data = new Color[outerRadius * outerRadius];
// Colour the entire texture transparent first.
for (int i = 0; i < data.Length; i++)
data[i] = Color.Transparent;
// Work out the minimum step necessary using trigonometry + sine approximation.
double angleStep = 1f/radius;
for (double angle = 0; angle < Math.PI*2; angle += angleStep)
{
// Use the parametric definition of a circle: http://en.wikipedia.org/wiki/Circle#Cartesian_coordinates
int x = (int)Math.Round(radius + radius * Math.Cos(angle));
int y = (int)Math.Round(radius + radius * Math.Sin(angle));
data[y * outerRadius + x + 1] = Color.White;
}
texture.SetData(data);
return texture;
}
public Texture2D CreateCircle(整数半径)
{
int outerRadius=radius*2+2;//所以圆不会超出边界
纹理2d纹理=新纹理2d(图形设备、外层、外层);
颜色[]数据=新颜色[outerRadius*outerRadius];
//首先将整个纹理着色为透明。
for(int i=0;i
如果您需要从头开始进行渲染(尽管我猜有更简单的方法),请更改执行渲染的方式。不要迭代角度和绘制像素,而是迭代像素并确定它们相对于圆的位置。如果它们是的话,就不要使用纹理来处理这样的东西(特别是那些只有一种颜色的东西!)——也不要尝试逐像素处理。你有3D加速是有原因的
用三角形扇子画一个类似于饼的圆。您将需要以下顶点
- 圆心
- 圆边界上的x点
前两个点将在圆的中心与其边界之间定义一条线。第三个顶点将定义第一个多边形。顶点1、3和4将定义第二个多边形,以此类推
要获取圆边界上的点,请使用示例中的公式。第一个角度为0°,以下角度为(360°/圆上的点)的倍数。要得到一个完整的圆,您需要一个与第二个点(边界上的第一个点)匹配的附加点
根据圆上顶点的数量,您将得到不同的n角。使用的顶点越多,形状看起来就越圆(以一定的性能代价):
- (不可能少于2个顶点,因为多边形至少需要绘制3个顶点。)
- 总共4个点(圆上3个点)将形成一个三角形
- 总共5点(圆上4点)将形成一个正方形
- 总共6点(圆上5点)将形成五角大楼
实际上,这本书展示了如何用三角形扇子画一个圆(或n边形)。对于任何想逐像素画圆的人来说都很好。。。我根据给出的信息制定了解决方案。在2d纹理方法中,添加以下代码以填充圆。我正在制作一个游戏,希望能够制作不同颜色和大小的圆圈。因此,在CreateCircle(int-radius)方法中,在创建轮廓后添加以下代码:
bool finished = false;
int firstSkip = 0;
int lastSkip = 0;
for (int i = 0; i <= data.Length - 1; i++)
{
if (finished == false)
{
//T = transparent W = White;
//Find the First Batch of Colors TTTTWWWTTTT The top of the circle
if ((data[i] == Color.White) && (firstSkip == 0))
{
while (data[i + 1] == Color.White)
{
i++;
}
firstSkip = 1;
i++;
}
//Now Start Filling TTTTTTTTWWTTTTTTTT
//circle in Between TTTTTTW--->WTTTTTT
//transaparent blancks TTTTTWW--->WWTTTTT
// TTTTTTW--->WTTTTTT
// TTTTTTTTWWTTTTTTTT
if (firstSkip == 1)
{
if (data[i] == Color.White && data[i + 1] != Color.White)
{
i++;
while (data[i] != Color.White)
{
//Loop to check if its the last row of pixels
//We need to check this because of the
//int outerRadius = radius * 2 + -->'2'<--;
for (int j = 1; j <= outerRadius; j++)
{
if (data[i + j] != Color.White)
{
lastSkip++;
}
}
//If its the last line of pixels, end drawing
if (lastSkip == outerRadius)
{
break;
finished = true;
}
else
{
data[i] = Color.White;
i++;
lastSkip = 0;
}
}
while (data[i] == Color.White)
{
i++;
}
i--;
}
}
}
}
// Set the data when finished
//-- don't need to paste this part, already given up above
texture.SetData(data);
return texture;
bool finished=false;
int firstSkip=0;
int lastSkip=0;
对于(int i=0;i wtttt)
//变速箱变速箱变速箱变速箱变速箱变速箱变速箱变速箱变速箱变速箱变速箱变速箱变速箱变速箱变速箱变速箱变速箱变速箱变速箱变速箱变速箱变速箱变速箱变速箱变速箱变速箱变速箱变速箱变速箱变速箱变速箱变速箱
//TTTTTTW--->WTTTTTTTT
//TTTTTTTTWWTTTTTTTTTT
if(firstSkip==1)
{
if(数据[i]==Color.White&&data[i+1]!=Color.White)
{
i++;
while(数据[i]!=Color.White)
{
//循环检查它是否是最后一行像素
//我们需要检查这一点,因为
//int outerRadius=radius*2+-->'2'我知道我有点晚了,但我修改了您的代码以填充中间部分
public static Texture2D CreateCircle(GraphicsDevice importedGraphicsDevice, int radius)
{
int outerRadius = radius * 2 + 2; // So circle doesn't go out of bounds
Texture2D texture = new Texture2D(importedGraphicsDevice, outerRadius, outerRadius);
Color[] data = new Color[outerRadius * outerRadius];
// Colour the entire texture transparent first.
for (int i = 0; i < data.Length; i++)
data[i] = Color.Transparent;
// Work out the minimum step necessary using trigonometry + sine approximation.
double angleStep = 1f / radius;
for (double angle = 0; angle < Math.PI * 2; angle += angleStep)
{
// Use the parametric definition of a circle: http://en.wikipedia.org/wiki/Circle#Cartesian_coordinates
int x = (int)Math.Round(radius + radius * Math.Cos(angle));
int y = (int)Math.Round(radius + radius * Math.Sin(angle));
data[y * outerRadius + x + 1] = Color.White;
}
//width
for (int i = 0; i < outerRadius; i++)
{
int yStart = -1;
int yEnd = -1;
//loop through height to find start and end to fill
for (int j = 0; j < outerRadius; j++)
{
if (yStart == -1)
{
if (j == outerRadius - 1)
{
//last row so there is no row below to compare to
break;
}
//start is indicated by Color followed by Transparent
if (data[i + (j * outerRadius)] == Color.White && data[i + ((j + 1) * outerRadius)] == Color.Transparent)
{
yStart = j + 1;
continue;
}
}
else if (data[i + (j * outerRadius)] == Color.White)
{
yEnd = j;
break;
}
}
//if we found a valid start and end position
if (yStart != -1 && yEnd != -1)
{
//height
for (int j = yStart; j < yEnd; j++)
{
data[i + (j * outerRadius)] = new Color(10, 10, 10, 10);
}
}
}
texture.SetData(data);
return texture;
}
public static Texture2D CreateCircle(图形设备导入图形设备,整数半径)
{
int outerRadius=radius*2+2;//所以圆不会超出边界
纹理2d纹理=新纹理2d(导入的图形设备、外层、外层);
颜色[]数据=新颜色[outerRadius*outerRadius];
//首先将整个纹理着色为透明。
for(int i=0;i