Τετάρτη 23 Σεπτεμβρίου 2026

Spiral Matrix

 A M2000 program to produce various spiral matrices


matrix 13x13 inward right

   0   1   2   3   4   5   6   7   8   9  10  11  12

  47  48  49  50  51  52  53  54  55  56  57  58  13

  46  87  88  89  90  91  92  93  94  95  96  59  14

  45  86 119 120 121 122 123 124 125 126  97  60  15

  44  85 118 143 144 145 146 147 148 127  98  61  16

  43  84 117 142 159 160 161 162 149 128  99  62  17

  42  83 116 141 158 167 168 163 150 129 100  63  18

  41  82 115 140 157 166 165 164 151 130 101  64  19

  40  81 114 139 156 155 154 153 152 131 102  65  20

  39  80 113 138 137 136 135 134 133 132 103  66  21

  38  79 112 111 110 109 108 107 106 105 104  67  22

  37  78  77  76  75  74  73  72  71  70  69  68  23

  36  35  34  33  32  31  30  29  28  27  26  25  24


matrix 13x13 outward right

 168 121 122 123 124 125 126 127 128 129 130 131 132

 167 120  81  82  83  84  85  86  87  88  89  90 133

 166 119  80  49  50  51  52  53  54  55  56  91 134

 165 118  79  48  25  26  27  28  29  30  57  92 135

 164 117  78  47  24   9  10  11  12  31  58  93 136

 163 116  77  46  23   8   1   2  13  32  59  94 137

 162 115  76  45  22   7   0   3  14  33  60  95 138

 161 114  75  44  21   6   5   4  15  34  61  96 139

 160 113  74  43  20  19  18  17  16  35  62  97 140

 159 112  73  42  41  40  39  38  37  36  63  98 141

 158 111  72  71  70  69  68  67  66  65  64  99 142

 157 110 109 108 107 106 105 104 103 102 101 100 143

 156 155 154 153 152 151 150 149 148 147 146 145 144


matrix 13x13 inward left

   0  47  46  45  44  43  42  41  40  39  38  37  36

   1  48  87  86  85  84  83  82  81  80  79  78  35

   2  49  88 119 118 117 116 115 114 113 112  77  34

   3  50  89 120 143 142 141 140 139 138 111  76  33

   4  51  90 121 144 159 158 157 156 137 110  75  32

   5  52  91 122 145 160 167 166 155 136 109  74  31

   6  53  92 123 146 161 168 165 154 135 108  73  30

   7  54  93 124 147 162 163 164 153 134 107  72  29

   8  55  94 125 148 149 150 151 152 133 106  71  28

   9  56  95 126 127 128 129 130 131 132 105  70  27

  10  57  96  97  98  99 100 101 102 103 104  69  26

  11  58  59  60  61  62  63  64  65  66  67  68  25

  12  13  14  15  16  17  18  19  20  21  22  23  24


matrix 13x13 outward left

 168 167 166 165 164 163 162 161 160 159 158 157 156

 121 120 119 118 117 116 115 114 113 112 111 110 155

 122  81  80  79  78  77  76  75  74  73  72 109 154

 123  82  49  48  47  46  45  44  43  42  71 108 153

 124  83  50  25  24  23  22  21  20  41  70 107 152

 125  84  51  26   9   8   7   6  19  40  69 106 151

 126  85  52  27  10   1   0   5  18  39  68 105 150

 127  86  53  28  11   2   3   4  17  38  67 104 149

 128  87  54  29  12  13  14  15  16  37  66 103 148

 129  88  55  30  31  32  33  34  35  36  65 102 147

 130  89  56  57  58  59  60  61  62  63  64 101 146

 131  90  91  92  93  94  95  96  97  98  99 100 145

 132 133 134 135 136 137 138 139 140 141 142 143 144


matrix 10x10 inward right

  0  1  2  3  4  5  6  7  8  9

 35 36 37 38 39 40 41 42 43 10

 34 63 64 65 66 67 68 69 44 11

 33 62 83 84 85 86 87 70 45 12

 32 61 82 95 96 97 88 71 46 13

 31 60 81 94 99 98 89 72 47 14

 30 59 80 93 92 91 90 73 48 15

 29 58 79 78 77 76 75 74 49 16

 28 57 56 55 54 53 52 51 50 17

 27 26 25 24 23 22 21 20 19 18


matrix 10x10 outward right

 99 64 65 66 67 68 69 70 71 72

 98 63 36 37 38 39 40 41 42 73

 97 62 35 16 17 18 19 20 43 74

 96 61 34 15  4  5  6 21 44 75

 95 60 33 14  3  0  7 22 45 76

 94 59 32 13  2  1  8 23 46 77

 93 58 31 12 11 10  9 24 47 78

 92 57 30 29 28 27 26 25 48 79

 91 56 55 54 53 52 51 50 49 80

 90 89 88 87 86 85 84 83 82 81


matrix 10x10 inward left

  0 35 34 33 32 31 30 29 28 27

  1 36 63 62 61 60 59 58 57 26

  2 37 64 83 82 81 80 79 56 25

  3 38 65 84 95 94 93 78 55 24

  4 39 66 85 96 99 92 77 54 23

  5 40 67 86 97 98 91 76 53 22

  6 41 68 87 88 89 90 75 52 21

  7 42 69 70 71 72 73 74 51 20

  8 43 44 45 46 47 48 49 50 19

  9 10 11 12 13 14 15 16 17 18


matrix 10x10 outward left

 99 98 97 96 95 94 93 92 91 90

 64 63 62 61 60 59 58 57 56 89

 65 36 35 34 33 32 31 30 55 88

 66 37 16 15 14 13 12 29 54 87

 67 38 17  4  3  2 11 28 53 86

 68 39 18  5  0  1 10 27 52 85

 69 40 19  6  7  8  9 26 51 84

 70 41 20 21 22 23 24 25 50 83

 71 42 43 44 45 46 47 48 49 82

 72 73 74 75 76 77 78 79 80 81


matrix 5x5 inward right

  0  1  2  3  4

 15 16 17 18  5

 14 23 24 19  6

 13 22 21 20  7

 12 11 10  9  8


matrix 5x5 outward right

 24  9 10 11 12

 23  8  1  2 13

 22  7  0  3 14

 21  6  5  4 15

 20 19 18 17 16


matrix 5x5 inward left

  0 15 14 13 12

  1 16 23 22 11

  2 17 24 21 10

  3 18 19 20  9

  4  5  6  7  8


matrix 5x5 outward left

 24 23 22 21 20

  9  8  7  6 19

 10  1  0  5 18

 11  2  3  4 17

 12 13 14 15 16


matrix 3x3 inward right

 0 1 2

 7 8 3

 6 5 4


matrix 3x3 outward right

 8 1 2

 7 0 3

 6 5 4


matrix 3x3 inward left

 0 7 6

 1 8 5

 2 3 4


matrix 3x3 outward left

 8 7 6

 1 0 5

 2 3 4


matrix 2x2 inward right

 0 1

 3 2


matrix 2x2 outward right

 3 0

 2 1


matrix 2x2 inward left

 0 3

 1 2


matrix 2x2 outward left

 3 2

 0 1


matrix 1x1 inward right

 0


matrix 1x1 outward right

 0


matrix 1x1 inward left

 0


matrix 1x1 outward left

 0





Module Spiral_Matrix (&ret, n as integer=5, outward as boolean=false, reverse as boolean=false) {
   Function PrintArray(Ar()) {
      if dimension(Ar())<>2 then Error "This is for 2D arrays"
      integer i, j, n=dimension(Ar(),1), n1=dimension(Ar(),2)
      string ruller=string$(" ",log(n*n)+2), newline
      mtab=len(ruller)
      for j=1 to n
         for i=1 to n1
            newline+=right(ruller+Ar(i, j),mtab)
         next
         newline+={
         }
      next
      =newline
   }
   Function MakeArray(n as integer=5, outward as boolean=false, reverse as boolean=true) {
      dim a(1 to n, 1 to n) as integer=0
      static sub addone
      var ox=0, oy=0, x=0
      if outward then x=n*n-1
      for n=n to (2-n mod 2) step 2
          if n>=2 then
             j=1:for i=1 to n:addone:next     
             i=n:for j=2 to n:addone:next
             j=n:for i=n-1 to 1:addone:next
             if n>2 then
                i=1:for j=n-1 to 2:addone:next
             end if
         else
            i=1:j=1
            addone
         end if
         ox++:oy++
         next
      =a() // return array (as a pointer)
      sub addone()
         if outward then
           if not reverse then
            a(j+oy, i+ox)=x
           else
            a(i+ox,j+oy)=x
           end if
            x--
         else
            if reverse then
               a(j+oy, i+ox)=x
            else
               a(i+ox,j+oy)=x
            end if
            x++
         end if
      end sub
   }
   ret+="matrix "+n+"x"+n+if$(outward->" outward", " inward")+if$(reverse->" left", " right")+{
   }+PrintArray(makeArray(n, outward, reverse))+{
   }
}
string all
Spiral_Matrix &all, 13
Spiral_Matrix &all, 13, true
Spiral_Matrix &all, 13, false, true
Spiral_Matrix &all, 13, true, true


Spiral_Matrix &all, 10
Spiral_Matrix &all, 10, true
Spiral_Matrix &all, 10, false, true
Spiral_Matrix &all, 10, true, true


Spiral_Matrix &all, 5
Spiral_Matrix &all, 5, true
Spiral_Matrix &all, 5, false, true
Spiral_Matrix &all, 5, true, true
Spiral_Matrix &all, 3
Spiral_Matrix &all, 3, true
Spiral_Matrix &all, 3, false, true
Spiral_Matrix &all, 3, true, true
Spiral_Matrix &all, 2
Spiral_Matrix &all, 2, true
Spiral_Matrix &all, 2, false, true
Spiral_Matrix &all, 2, true, true
Spiral_Matrix &all, 1
Spiral_Matrix &all, 1, true
Spiral_Matrix &all, 1, false, true
Spiral_Matrix &all, 1, true, true


clipboard all
report all


Δεν υπάρχουν σχόλια:

Δημοσίευση σχολίου

You can feel free to write any suggestion, or idea on the subject.