2010-07-19 3 views
5

द्वारा 2 डी ऐरे घुमाएं मैं कैसे 2 डिग्री आयताकार सरणी को घुमा सकता हूं जिसमें पंक्तियों की विषम संख्या 45 डिग्री है?45 डिग्री

तो

int[] myArray = new int[,] 
{ 
    {1, 0 ,1}, 
    {0, 1 ,0}, 
    {0, 0 ,0}, 
} 

की तरह कुछ

int[] rotatedArray = new int[,] 
{ 
    {0, 1 ,0}, 
    {0, 1 ,1}, 
    {0, 0 ,0}, 
} 
किसी भी आयाम (3x3, 5x5, 7x7, आदि) के लिए

में। इस सूत्र http://yfrog.com/n6matrix45p

5x5

0 0 0 0 0 
2 0 0 0 0 
1 1 1 1 1 
0 0 0 0 0 
0 0 0 0 0 

में

1 2 0 0 0 
0 1 0 0 0 
0 0 1 0 0 
0 0 0 1 0 
0 0 0 0 1 

5x5

0 0 0 3 0 
0 0 0 3 0 
0 0 0 3 0 
0 0 0 3 0 
0 0 0 3 0 

में से
0 0 0 0 0 
0 0 0 0 3 
0 0 0 3 0 
0 0 3 3 0 
0 3 0 0 0 

उत्तर

1

यह एक कोड मुझे द्वारा लिखित और एक दोस्त को हल करती है कि यह है:

public static class ArrayExtensions 
{ 
    public static Point RoundIndexToPoint(int index, int radius) 
    { 
     if (radius == 0) 
      return new Point(0, 0); 
     Point result = new Point(-radius, -radius); 

     while (index < 0) index += radius * 8; 
     index = index % (radius * 8); 

     int edgeLen = radius * 2; 

     if (index < edgeLen) 
     { 
      result.X += index; 
     } 
     else if ((index -= edgeLen) < edgeLen) 
     { 
      result.X = radius; 
      result.Y += index; 
     } 
     else if ((index -= edgeLen) < edgeLen) 
     { 
      result.X = radius - index; 
      result.Y = radius; 
     } 
     else if ((index -= edgeLen) < edgeLen) 
     { 
      result.Y = radius - index; 
     } 

     return result; 
    } 

    public static T[,] Rotate45<T>(this T[,] array) 
    { 
     int dim = Math.Max(array.GetLength(0), array.GetLength(0)); 

     T[,] result = new T[dim, dim]; 

     Point center = new Point((result.GetLength(0) - 1)/2, (result.GetLength(1) - 1)/2); 
     Point center2 = new Point((array.GetLength(0) - 1)/2, (array.GetLength(1) - 1)/2); 
     for (int r = 0; r <= (dim - 1)/2; r++) 
     { 
      for (int i = 0; i <= r * 8; i++) 
      { 
       Point source = RoundIndexToPoint(i, r); 
       Point target = RoundIndexToPoint(i + r, r); 

       if (!(center2.X + source.X < 0 || center2.Y + source.Y < 0 || center2.X + source.X >= array.GetLength(0) || center2.Y + source.Y >= array.GetLength(1))) 
        result[center.X + target.X, center.Y + target.Y] = array[center2.X + source.X, center2.Y + source.Y]; 
      } 
     } 
     return result; 
    }  
} 

0

आप इस पुस्तकालय की कोशिश कर सकते हैं:

http://www.drunkenhyena.com/cgi-bin/view_net_article.pl?chapter=2;article=28#Rotation

को न भूलें:

मैट्रिक्स के संचालन के लिए Math.NET Project ... http://numerics.mathdotnet.com/

इस कोड को भी उपयोगी प्रतीत होता है डायरेक्टएक्स प्रबंधित और अप्रबंधित नामस्थान और कक्षाएं। बहुत सारे और जांच करने के लिए वहां बहुत अच्छी चीजें हैं।

उदाहरण के लिए:

Matrix..::.Rotate Method (Single, MatrixOrder)

+0

उन मैट्रिक्स केवल 4x4 या 3x3 कर रहे हैं, मैं math.net कोशिश करता हूँ, लेकिन मैं डर इस रोटेशन भी विशिष्ट है – Kikaimaru

+4

ये परिवर्तन के लिए रोटेशन मैट्रिस हैं। एक पूरी तरह से अलग बात है। – Cloudanger

0

मुझे लगता है कि हम इन नियम है:

  1. "फ्रेम या केन्द्रों के बिना बॉक्स" का एक सेट रूस की तरह "एक दूसरे के भीतर के रूप में मैट्रिक्स कल्पना कीजिए गुड़िया "।

  2. एक तरफ के केंद्र (शीर्ष/बाएं/दाएं/नीचे) के तत्व निकटतम कोने की दिशा में आगे बढ़ते हैं।

  3. कॉर्नर अगले केंद्र की दिशा में आगे बढ़ते हैं।

  4. तत्व जो कि कोनों नहीं हैं और न ही केंद्र अगले स्थान (घड़ी की दिशा में) पर जाते हैं जो कि वर्तमान में एक कोने से एक ही दूरी है।

मैंने कुछ कोड लिखना शुरू कर दिया है, लेकिन मुझे नहीं लगता कि यह मामूली है और मेरे पास परीक्षण करने का समय नहीं है।