<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                # 將圖像旋轉 90 度 > 原文: [https://www.geeksforgeeks.org/turn-an-image-by-90-degree/](https://www.geeksforgeeks.org/turn-an-image-by-90-degree/) 給定圖像,如何將其旋轉 90 度? 一個模糊的問題。 最小化瀏覽器,然后嘗試進一步解決方案。 圖像可以視為 2D 矩陣,可以存儲在緩沖區中。 我們提供了矩陣尺寸及其基地址。 我們怎么能轉呢? 例如,見下圖, ``` * * * ^ * * * * * * | * * * * * * | * * * * * * | * * * ``` 向右旋轉后,它會出現(觀察箭頭方向) ``` * * * * * * * * * * * * -- - - > * * * * * * * * * * * * ``` 這個想法很簡單。 將源矩陣的每一行轉換為最終圖像所需的列。 我們將使用輔助緩沖區來轉換圖像。 從上圖可以看出 ``` first row of source ------> last column of destination second row of source ------> last but-one column of destination so ... on last row of source?------> first column of destination ``` 以圖片的形式,我們可以表示(m x n)矩陣到(n x m)矩陣的上述轉換, ![](https://img.kancloud.cn/d0/ad/d0adb58af69d2e3cf7856aaf5d5c7c40_541x223.png) 轉變 如果您尚未嘗試,請立即嘗試使用偽代碼。 編寫偽代碼很容易。 在 C/C++ 中,我們通常會按行主要順序遍歷矩陣。 每行轉換為最終圖像的不同列。 我們需要構造最終圖像的列。 請參閱以下算法(轉換) ``` for (r = 0; r < m; r++) { ?? for (c = 0; c < n; c++) ?? { ?? ? ?// Hint:?Map each source element indices into // indices of destination matrix element. ?? ? ? dest_buffer [ c ] [ m - r - 1 ] = source_buffer [ r ] [ c ]; ?? } } ``` 請注意,有多種方法可以基于矩陣,行主要或列主要順序的遍歷來實現算法。 我們有兩個矩陣和兩種方式(行和列主行)遍歷每個矩陣。 因此,至少有 4 種不同的方式將源矩陣轉換為最終矩陣。 ## C++ ```cpp // C++ program to turn an? // image by 90 Degree? #include <bits/stdc++.h> using namespace std; void displayMatrix(unsigned int const *p,? ????????????????????unsigned int row,? ???????????????????unsigned int col);? void rotate(unsigned int *pS,? ????????????unsigned int *pD,? ????????????unsigned int row,? ????????????unsigned int col);? void displayMatrix(unsigned int const *p,? ???????????????????unsigned int r,? ???????????????????unsigned int c)? {? ????unsigned int row, col;? ????cout << "\n\n";? ????for (row = 0; row < r; row++)? ????{? ????????for (col = 0; col < c; col++)? ????????????cout << * (p + row * c + col) << "\t";? ????????cout << "\n";? ????}? ????cout << "\n\n";? }? void rotate(unsigned int *pS,? ????????????unsigned int *pD,? ????????????unsigned int row,? ????????????unsigned int col)? {? ????unsigned int r, c;? ????for (r = 0; r < row; r++)? ????{? ????????for (c = 0; c < col; c++)? ????????{? ????????????*(pD + c * row + (row - r - 1)) =? ????????????????????????*(pS + r * col + c);? ????????}? ????}? }? // Driver Code? int main()? {? ????// declarations? ????unsigned int image[][4] = {{1, 2, 3, 4},? ???????????????????????????????{5, 6, 7, 8},? ???????????????????????????????{9, 10, 11, 12}};? ????unsigned int *pSource;? ????unsigned int *pDestination;? ????unsigned int m, n;? ????// setting initial values? ????// and memory allocation? ????m = 3, n = 4, pSource = (unsigned int *)image;? ????pDestination = (unsigned int *)malloc ???????????????????(sizeof(int) * m * n);? ????// process each buffer? ????displayMatrix(pSource, m, n);? ????rotate(pSource, pDestination, m, n);? ????displayMatrix(pDestination, n, m);? ????free(pDestination);? ????return 0;? }? // This code is contributed by rathbhupendra ``` ## C ``` // C program to turn an? // image by 90 Degree #include <stdio.h> #include <stdlib.h> void displayMatrix(unsigned int const *p,? ???????????????????unsigned int row,? ???????????????????unsigned int col); void rotate(unsigned int *pS,? ????????????unsigned int *pD,? ????????????unsigned int row,? ????????????unsigned int col); void displayMatrix(unsigned int const *p,? ???????????????????unsigned int r,? ???????????????????unsigned int c)? { ????unsigned int row, col; ????printf("\n\n"); ????for (row = 0; row < r; row++) ????{ ????????for (col = 0; col < c; col++) ????????????printf("%d\t", * (p + row * c + col)); ????????printf("\n"); ????} ????printf("\n\n"); } void rotate(unsigned int *pS,? ????????????unsigned int *pD, ????????????unsigned int row,? ????????????unsigned int col) { ????unsigned int r, c; ????for (r = 0; r < row; r++) ????{ ????????for (c = 0; c < col; c++) ????????{ ????????????*(pD + c * row + (row - r - 1)) =? ????????????????????????????*(pS + r * col + c); ????????} ????} } // Driver Code int main() { ????// declarations ????unsigned int image[][4] = {{1,2,3,4},? ???????????????????????????????{5,6,7,8}, ???????????????????????????????{9,10,11,12}}; ????unsigned int *pSource; ????unsigned int *pDestination; ????unsigned int m, n; ????// setting initial values ????// and memory allocation ????m = 3, n = 4, pSource = (unsigned int *)image; ????pDestination =? ????????(unsigned int *)malloc? ????????(sizeof(int) * m * n); ????// process each buffer ????displayMatrix(pSource, m, n); ????rotate(pSource, pDestination, m, n); ????displayMatrix(pDestination, n, m); ????free(pDestination); ????getchar(); ????return 0; } ``` 輸出: ``` 1 2 3 4 5 6 7 8 9 10 11 12 9 5 1 10 6 2 11 7 3 12 8 4 ``` [將方形矩陣旋轉 90 度](https://www.geeksforgeeks.org/inplace-rotate-square-matrix-by-90-degrees/) 由 [**Venki**](https://www.geeksforgeeks.org/?page_id=2) 編譯。 如果發現任何不正確的地方,或者想分享有關上述主題的更多信息,請寫評論。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看