| ... | @@ -1731,29 +1731,38 @@ test "array initialization with function calls" { | ... | @@ -1731,29 +1731,38 @@ test "array initialization with function calls" { |
| 1731 | assert(more_points[4].y == 6); | 1731 | assert(more_points[4].y == 6); |
| 1732 | assert(more_points.len == 10); | 1732 | assert(more_points.len == 10); |
| 1733 | } | 1733 | } |
| | 1734 | {#code_end#} |
| | 1735 | {#see_also|for|Slices#} |
| 1734 | | 1736 | |
| 1735 | // Multidimensional arrays are declared by simply adding another array before the existing array | 1737 | {#header_open|Multidimensional Arrays#} |
| 1736 | var mat4x4 = [4][4]f32{ | 1738 | <p> |
| 1737 | [_]f32{1.0, 0.0, 0.0, 0.0}, | 1739 | Mutlidimensional arrays can be created by nesting arrays: |
| 1738 | [_]f32{0.0, 1.0, 0.0, 1.0}, | 1740 | </p> |
| 1739 | [_]f32{0.0, 0.0, 1.0, 0.0}, | 1741 | {#code_begin|test|multidimensional#} |
| 1740 | [_]f32{0.0, 0.0, 0.0, 1.0} | 1742 | const std = @import("std"); |
| | 1743 | const assert = std.debug.assert; |
| | 1744 | |
| | 1745 | const mat4x4 = [4][4]f32{ |
| | 1746 | [_]f32{ 1.0, 0.0, 0.0, 0.0 }, |
| | 1747 | [_]f32{ 0.0, 1.0, 0.0, 1.0 }, |
| | 1748 | [_]f32{ 0.0, 0.0, 1.0, 0.0 }, |
| | 1749 | [_]f32{ 0.0, 0.0, 0.0, 1.0 }, |
| 1741 | }; | 1750 | }; |
| 1742 | test "multidimensional arrays" { | 1751 | test "multidimensional arrays" { |
| 1743 | // Multidimensional arrays can be accessed as expected from other languages... | 1752 | // Access the 2D array by indexing the outer array, and then the inner array. |
| 1744 | assert(mat4x4[1][1] == 1.0); | 1753 | assert(mat4x4[1][1] == 1.0); |
| 1745 | | 1754 | |
| 1746 | // or iterated over like any other array | 1755 | // Here we iterate with for loops. |
| 1747 | for (mat4x4) |row, rowNum| { | 1756 | for (mat4x4) |row, row_index| { |
| 1748 | for (row) |column, colNum| { | 1757 | for (row) |cell, column_index| { |
| 1749 | if (rowNum == colNum) { | 1758 | if (row_index == column_index) { |
| 1750 | assert(column == 1.0); | 1759 | assert(cell == 1.0); |
| 1751 | } | 1760 | } |
| 1752 | } | 1761 | } |
| 1753 | } | 1762 | } |
| 1754 | } | 1763 | } |
| 1755 | {#code_end#} | 1764 | {#code_end#} |
| 1756 | {#see_also|for|Slices#} | 1765 | {#header_close#} |
| 1757 | {#header_close#} | 1766 | {#header_close#} |
| 1758 | | 1767 | |
| 1759 | {#header_open|Vectors#} | 1768 | {#header_open|Vectors#} |