colors - 如何在 XNA 中绘制具有特定颜色的圆圈?

标签 colors drawing xna geometry

XNA 没有任何支持圆绘制的方法。
通常,当我必须绘制圆圈时,总是使用相同的颜色,我只是用那个圆圈制作图像,然后我可以将其显示为 Sprite 。
但是现在圆圈的颜色是在运行时指定的,任何想法如何处理?

最佳答案

您可以简单地制作带有 Transparent 的圆形图像。背景和圆圈的彩色部分为 White .然后,当涉及到在 Draw() 中绘制圆圈时方法,选择您想要的色调:

Texture2D circle = CreateCircle(100);

// Change Color.Red to the colour you want
spriteBatch.Draw(circle, new Vector2(30, 30), Color.Red); 

只是为了好玩,这里是 CreateCircle 方法:
    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.TransparentWhite;

        // 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;
    }

关于colors - 如何在 XNA 中绘制具有特定颜色的圆圈?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2983809/

相关文章:

c++ - 设置背景颜色 CMDIFrameWnd

javascript - HTML colorpicker 发生变化时如何获取新值?

D3.js 强制有向图,每组不同颜色?

javascript - Javascript 的 ctx.arc 方法是否计算相对于像素大小和半径的顶点?

xcode - NSTableView 在 NSSplitView 中显示不正确

c# - XNA 4.0 中的 2D BoundingRectangle 旋转

css - 我在这个 CSS 中错误地使用了选择器吗?让 h2 改变颜色有问题

android - 如何在 Android 中绘制支持触摸的矩形

xna - XNA 中的这种旋转行为是什么?

c# - 将 2D 仿射变换矩阵转换为 3D 仿射变换矩阵