2010-06-06 2 views
10

XNA에는 원 그리기를 지원하는 방법이 없습니다.
일반적으로 항상 동그라미를 그릴 때, 같은 색상으로, 나는 그 원으로 이미지를 만들었고 스프라이트로 표시 할 수있었습니다.
하지만 이제는 원의 색상이 런타임 중에 지정됩니다. 어떻게 처리 할 수 ​​있습니까?XNA에서 특정 색상의 원을 그리는 방법은 무엇입니까?

+0

나는 XNA의 포럼에서 그런 것을 읽었던 것을 기억합니다. – Mike

답변

37

원의 이미지를 Transparent 배경으로, 원색 부분을 White으로 간단하게 만들 수 있습니다.

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

Texture2D circle = CreateCircle(100); 

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

그냥 재미가 여기 CreateCircle 방법입니다 : 그것은 Draw() 방법에 동그라미를 그리기에 올 때 당신이 원하는 무엇을 그리고, 같은 색조를 선택

+0

이 스레드는 실제로 오래되었지만 코드는 나를 위해 서클을 반환합니다. 어떤 변화로이 문제를 어떻게 해결할 수 있는지 알고 있니? – Weszzz7

+13

@ Weszzz7, 서클을 반환 할 수 있습니까? – Cyral