Подтвердить что ты не робот

Как нарисовать круг с определенным цветом в XNA?

XNA не имеет методов, поддерживающих рисование круга.
Обычно, когда мне приходилось рисовать круг, всегда с тем же цветом, я просто делал изображение с этим кругом, а затем я мог отображать его как спрайт. Но теперь цвет круга указан во время выполнения, любые идеи, как с этим бороться?

4b9b3361

Ответ 1

Вы можете просто создать изображение круга с фоном 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;
    }