2010-06-06 15 views
10

एक्सएनए में सर्कल ड्राइंग का समर्थन करने वाली कोई भी विधि नहीं है।
आम तौर पर जब मुझे सर्कल खींचना पड़ता था, हमेशा एक ही रंग के साथ, मैंने बस उस सर्कल के साथ छवि बनाई और फिर मैं इसे एक स्प्राइट के रूप में प्रदर्शित कर सकता था।
लेकिन अब सर्कल का रंग रनटाइम के दौरान निर्दिष्ट किया गया है, किसी भी विचार से निपटने के लिए कैसे?एक्सएनए में विशिष्ट रंग के साथ सर्कल कैसे आकर्षित करें?

+0

मुझे XNA के फ़ोरम पर ऐसा कुछ याद रखना याद है। – Mike

उत्तर

37

आप आसानी से Transparent पृष्ठभूमि और सर्कल के रंगीन भाग White के साथ एक मंडली की एक छवि बना सकते हैं। तब, जब यह Draw() विधि में हलकों ड्राइंग के लिए आता है, के रूप में रंग का चयन करें कि आप क्या यह होना चाहते हैं:

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 विधि है

+0

मुझे पता है कि यह धागा वास्तव में पुराना है, लेकिन आपका कोड मेरे लिए एक सर्कल देता है। क्या आप किसी भी बदलाव से जान सकते हैं कि मैं इसे कैसे ठीक कर सकता हूं? – Weszzz7

+13

@ वेस्ज़ज़ 7, क्या यह एक सर्कल वापस करने के लिए तैयार नहीं है? – Cyral

संबंधित मुद्दे