{"id":23332,"date":"2026-07-14T21:28:20","date_gmt":"2026-07-14T15:28:20","guid":{"rendered":"https:\/\/ruap.net\/ruap\/%e4%bf%84%e7%bd%97%e6%96%af%e6%96%b9%e5%9d%97%e6%97%8b%e8%bd%ac%e7%ae%97%e6%b3%95-baeldung%e4%b8%ad%e6%96%87%e7%bd%91\/"},"modified":"2026-07-14T21:28:20","modified_gmt":"2026-07-14T15:28:20","slug":"%e4%bf%84%e7%bd%97%e6%96%af%e6%96%b9%e5%9d%97%e6%97%8b%e8%bd%ac%e7%ae%97%e6%b3%95-baeldung%e4%b8%ad%e6%96%87%e7%bd%91","status":"publish","type":"post","link":"https:\/\/ruap.net\/ruap\/%e4%bf%84%e7%bd%97%e6%96%af%e6%96%b9%e5%9d%97%e6%97%8b%e8%bd%ac%e7%ae%97%e6%b3%95-baeldung%e4%b8%ad%e6%96%87%e7%bd%91\/","title":{"rendered":"\u4fc4\u7f57\u65af\u65b9\u5757\u65cb\u8f6c\u7b97\u6cd5 Baeldung\u4e2d\u6587\u7f51"},"content":{"rendered":"<p><a href=\"https:\/\/www.blackcurve.com\/casino-non-gamstop\/reviews\/betzino\/\">\u514d\u8d39\u4fc4\u7f57\u65af\u8272\u60c5<\/a>, https:\/\/www.blackcurve.com\/casino-non-gamstop\/reviews\/betzino\/. <\/p>\n<h1>\u4fc4\u7f57\u65af\u65b9\u5757\u65cb\u8f6c\u7b97\u6cd5 Baeldung\u4e2d\u6587\u7f51<\/h1>\n<\/p>\n<p><h2>1. Overview<\/h2>\n<\/p>\n<p>In this tutorial, we\u2019ll discuss the algorithm behind rotating Tetris pieces. We\u2019ll start by discussing the equation for rotating a shape in general. Then, we\u2019ll define a data structure that can be used to manipulate the Tetris pieces\u2019 rotations.<\/p>\n<p>Finally, we\u2019ll present the algorithm to rotate a Tetris piece and finish with a quick conclusion.<\/p>\n<p><h2>2. Rotating a Shape<\/h2>\n<\/p>\n<p>First, let\u2019s define the problem from a logical and mathematical point of view. Then, we can extract the general equations to rotate a point in the cartesian coordinate system.<\/p>\n<p>The Tetris game consists of the following 7 main shapes:<\/p>\n<p><img decoding=\"async\" src=\"\/wp-content\/uploads\/sites\/4\/2023\/05\/Drawing4-1-1024x257.png\" \/><\/p>\n<p>Each shape can be rotated and then put on top of others to form straight lines. <strong>The allowed rotations for each piece are 90, 180, 270, and 360 degrees, which is the original orientation of the shape.<\/strong>\u00a0This tutorial discusses the general algorithm to rotate any of the mentioned pieces.<\/p>\n<p><strong>The algorithm should rotate a given Tetris piece by 90 degrees.<\/strong> Calling the algorithm multiple times will generate all the possible rotations for the given piece. As a start, let\u2019s discuss the general rotation equations and then see how to apply them in the intended algorithm.<\/p>\n<p>Since each Tetris piece is a polygon, rotating each of the polygon\u2019s corners alone should give us the resulting rotated piece. Therefore, in this section, we\u2019ll discuss the equations to rotate a point around the origin of the cartesian coordinate axis.<\/p>\n<p>Let\u2019s take a look at the following figure:<\/p>\n<p><img decoding=\"async\" src=\"\/wp-content\/uploads\/sites\/4\/2023\/05\/Drawing2-1024x825.png\" \/><\/p>\n<p>In the coordinates above, we have point <img decoding=\"async\" src=\"\/wp-content\/ql-cache\/quicklatex.com-b6856ac0ab0200ef2ba6287c85773661_l3.svg\"> forming an angle <img decoding=\"async\" src=\"\/wp-content\/ql-cache\/quicklatex.com-5f44d9bbc8046069be4aa2989bff19aa_l3.svg\"> with the x-axis. For simplicity, we\u2019ll consider the point as a vector with magnitude <img decoding=\"async\" src=\"\/wp-content\/ql-cache\/quicklatex.com-01bcf7e9e043561da78fecf715c8a46e_l3.svg\">. Using this representation, we can define equations for <img decoding=\"async\" src=\"\/wp-content\/ql-cache\/quicklatex.com-7e5fbfa0bbbd9f3051cd156a0f1b5e31_l3.svg\"> and <img decoding=\"async\" src=\"\/wp-content\/ql-cache\/quicklatex.com-38461fc041e953482219abf5d4cce1cb_l3.svg\"> as follows:<\/img><\/img><\/img><\/img><\/img><\/p>\n<p>\u00a0 \u00a0 <img decoding=\"async\" src=\"\/wp-content\/ql-cache\/quicklatex.com-5db8fd594df578c9c768b6d2c03db5ef_l3.svg\" \/><\/p>\n<p>\u00a0 \u00a0 <img decoding=\"async\" src=\"\/wp-content\/ql-cache\/quicklatex.com-4a878e5b1659c780ebc19f1ce1c073ab_l3.svg\" \/><\/p>\n<p>After defining the equations to represent a point in the coordinate axis, let\u2019s rotate the point <img decoding=\"async\" src=\"\/wp-content\/ql-cache\/quicklatex.com-b6856ac0ab0200ef2ba6287c85773661_l3.svg\"> to new coordinates <img decoding=\"async\" src=\"\/wp-content\/ql-cache\/quicklatex.com-59f11e327864bfd0dfb602e430e5bdf0_l3.svg\">. The rotation is shown in the following shape:<\/img><\/img><\/p>\n<p><img decoding=\"async\" src=\"\/wp-content\/uploads\/sites\/4\/2023\/05\/Drawing1-1024x825.png\" \/><\/p>\n<p>The original point was rotated by <img decoding=\"async\" src=\"\/wp-content\/ql-cache\/quicklatex.com-0f39b655b53423e80558c68b8c2ae1c3_l3.svg\"> degrees to the new coordinates <img decoding=\"async\" src=\"\/wp-content\/ql-cache\/quicklatex.com-59f11e327864bfd0dfb602e430e5bdf0_l3.svg\"> having a new magnitude of <img decoding=\"async\" src=\"\/wp-content\/ql-cache\/quicklatex.com-ebee390253a45105959c2bc4c4ad9d27_l3.svg\">. We can see that the new point forms an angle <img decoding=\"async\" src=\"\/wp-content\/ql-cache\/quicklatex.com-b7847fbf11b2aa11799e33624950ca23_l3.svg\"> with the x-axis. Likewise, we can define equations for <img decoding=\"async\" src=\"\/wp-content\/ql-cache\/quicklatex.com-59f11e327864bfd0dfb602e430e5bdf0_l3.svg\"> as follows:<\/img><\/img><\/img><\/img><\/img><\/p>\n<p>\u00a0 \u00a0 <img decoding=\"async\" src=\"\/wp-content\/ql-cache\/quicklatex.com-17e19de9822aa541042581683ce850e9_l3.svg\" \/><\/p>\n<p>\u00a0 \u00a0 <img decoding=\"async\" src=\"\/wp-content\/ql-cache\/quicklatex.com-f419abceb484246c1d72f1517e3e2b4a_l3.svg\" \/><\/p>\n<p><strong>To find the rotation equations, we need to find <img decoding=\"async\" src=\"\/wp-content\/ql-cache\/quicklatex.com-7e9240d4beef863c91299bf5edf7a375_l3.svg\"> and <img decoding=\"async\" src=\"\/wp-content\/ql-cache\/quicklatex.com-64a0a8e8f9cf320aa10cf6e5c9f0499c_l3.svg\"> as functions to the original coordinates <img decoding=\"async\" src=\"\/wp-content\/ql-cache\/quicklatex.com-2234c4dc1f3099131d36c7e7f02b1d84_l3.svg\"> and <img decoding=\"async\" src=\"\/wp-content\/ql-cache\/quicklatex.com-4149db711c9e681cd65bbdb5264c4234_l3.svg\">.<\/img><\/img><\/img><\/img><\/strong>\u00a0To do this, we\u2019ll use the following known equations of <img decoding=\"async\" src=\"\/wp-content\/ql-cache\/quicklatex.com-f9be6c4b03749d6dff440419db4cfa18_l3.svg\"> and <img decoding=\"async\" src=\"\/wp-content\/ql-cache\/quicklatex.com-d3762ae196ab67f79666c057ed48cd79_l3.svg\"> for the sum of two angles:<\/img><\/img><\/p>\n<p>\u00a0 \u00a0 <img decoding=\"async\" src=\"\/wp-content\/ql-cache\/quicklatex.com-89a753a6da38fbbd2305d29c3da03fcd_l3.svg\" \/><\/p>\n<p>\u00a0 \u00a0 <img decoding=\"async\" src=\"\/wp-content\/ql-cache\/quicklatex.com-c2fa76ce228dfda73d160505d976252b_l3.svg\" \/><\/p>\n<p>Additionally, rotating a vector doesn\u2019t change its magnitude. Therefore, the following equation applies:<\/p>\n<p>\u00a0 \u00a0 <img decoding=\"async\" src=\"\/wp-content\/ql-cache\/quicklatex.com-9ca1d85754543ceb277253df80847117_l3.svg\" \/><\/p>\n<p>Now, we can use the above equations to rewrite our formula for <img decoding=\"async\" src=\"\/wp-content\/ql-cache\/quicklatex.com-b4a147f08046ded14d94e9286029b2a7_l3.svg\"> and <img decoding=\"async\" src=\"\/wp-content\/ql-cache\/quicklatex.com-e3860fc68d5fca7cb5a4fdd0e4d3410d_l3.svg\">:<\/img><\/img><\/p>\n<p>\u00a0 \u00a0 <img decoding=\"async\" src=\"\/wp-content\/ql-cache\/quicklatex.com-46b968e7c6df26ccf1897638638f1308_l3.svg\" \/><\/p>\n<p>\u00a0 \u00a0 <img decoding=\"async\" src=\"\/wp-content\/ql-cache\/quicklatex.com-656347a9fb7b527d62491a62afa491bc_l3.svg\" \/><\/p>\n<p>We can simplify them using the equations we defined in section 2.2 to the following ones:<\/p>\n<p>\u00a0 \u00a0 <img decoding=\"async\" src=\"\/wp-content\/ql-cache\/quicklatex.com-4d7ebddbf7165f776e7f1f533c2320bb_l3.svg\" \/><\/p>\n<p>\u00a0 \u00a0 <img decoding=\"async\" src=\"\/wp-content\/ql-cache\/quicklatex.com-78a9edd83e9a4718937dbfb61865d722_l3.svg\" \/><\/p>\n<p>Now, we can use these equations to create an algorithm that can rotate a Tetris piece. Let\u2019s start by defining the data structure to store the pieces.<\/p>\n<p><h2>3. Structure Definition<\/h2>\n<\/p>\n<p>We need to define the structure that will store the Tetris pieces. To do that, we can define each shape as a set of points which are the corners of the shape. In addition, the structure must support rotating the Tetris pieces.<\/p>\n<p>It\u2019s worth noting that Tetris pieces are rotated around their origin. Therefore, we need to define the center point for each shape as well. Take a look at the following figure that shows the different rotations of each shape along with its origin:<\/p>\n<p><img decoding=\"async\" src=\"\/wp-content\/uploads\/sites\/4\/2023\/05\/Drawing3-1-1024x666.png\" \/><\/p>\n<p>Therefore, for each shape, we\u2019ll have an array <img decoding=\"async\" src=\"\/wp-content\/ql-cache\/quicklatex.com-639d86127b094ce8bbe81128a8e04c3a_l3.svg\"> containing the shape\u2019s corner points and a variable <img decoding=\"async\" src=\"\/wp-content\/ql-cache\/quicklatex.com-d450e5d51eb316725457ede5e86e5bb9_l3.svg\"> containing the shape\u2019s origin. Now that we defined the structure, we can move into implementing the rotation algorithm.<\/img><\/img><\/p>\n<p><h2>4. Algorithm<\/h2>\n<\/p>\n<p>To implement a rotation algorithm, we only need one function called <img decoding=\"async\" src=\"\/wp-content\/ql-cache\/quicklatex.com-8f1b273ba5e45c0a34affba1531d83ca_l3.svg\">, which rotates the given shape by the given angle. Note that the piece is not initially located at the coordinated axis\u2019s center. Therefore, the algorithm must shift the shape to the center of the coordinate axis, perform the rotations, and then shift the point back.<\/img><\/p>\n<p>Let\u2019s take a look at the algorithm:<\/p>\n<p>The function takes the array of points, the origin of the shape, and the rotation angle <img decoding=\"async\" src=\"\/wp-content\/ql-cache\/quicklatex.com-349fdc9f5f41a6ecbf5e500a90fb7a73_l3.svg\"> as input. We start by defining <img decoding=\"async\" src=\"\/wp-content\/ql-cache\/quicklatex.com-2795d6cdf89a6089128d4299f7974a6b_l3.svg\">, which will hold the resulting rotated points. Note that the origin stays the same after the rotation, so we don\u2019t need to return it.<\/img><\/img><\/p>\n<p>Next, we iterate over all the points of the shape. We need to shift each point as if the shape\u2019s origin is moved to the centre of the coordinate axis. If the origin is moved to the centre, then it shifts to point <img decoding=\"async\" src=\"\/wp-content\/ql-cache\/quicklatex.com-e6f462232b138f5ab691a1230c3f31e1_l3.svg\">. At this time, the point will also shift with the same amount. Therefore, we shift the point by <img decoding=\"async\" src=\"\/wp-content\/ql-cache\/quicklatex.com-0ac9845abea641290db727c478b2177d_l3.svg\">. As a result, we define <img decoding=\"async\" src=\"\/wp-content\/ql-cache\/quicklatex.com-e6e1c3611728e9db0e52bb6485e06068_l3.svg\"> and <img decoding=\"async\" src=\"\/wp-content\/ql-cache\/quicklatex.com-2b1ce6fe7664ef728c0428024c69737b_l3.svg\">, which are the shifted coordinates.<\/img><\/img><\/img><\/img><\/p>\n<p>Then, we perform the rotation by defining <img decoding=\"async\" src=\"\/wp-content\/ql-cache\/quicklatex.com-606a5fa3feb8ab19bd3c2da653ad7952_l3.svg\"> and <img decoding=\"async\" src=\"\/wp-content\/ql-cache\/quicklatex.com-4e8d5da324a650dffbf976bdfeb7d006_l3.svg\"> which are the coordinates for the rotated point, by applying the equations from section 2. After that, we shift the new points <img decoding=\"async\" src=\"\/wp-content\/ql-cache\/quicklatex.com-606a5fa3feb8ab19bd3c2da653ad7952_l3.svg\"> and <img decoding=\"async\" src=\"\/wp-content\/ql-cache\/quicklatex.com-4e8d5da324a650dffbf976bdfeb7d006_l3.svg\"> back away from the centre of the coordinate axis. Finally, we add the new points to the list of rotated points we created.<\/img><\/img><\/img><\/img><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/burf.co\/about.php\" style=\"max-width:400px;float:right;padding:10px 0px 10px 10px;border:0px\"><\/p>\n<p>In the end, we return the resulting <img decoding=\"async\" src=\"\/wp-content\/ql-cache\/quicklatex.com-2795d6cdf89a6089128d4299f7974a6b_l3.svg\"> as the new shape corder points after performing the rotation.<\/img><\/p>\n<p><h2>5. Conclusion<\/h2>\n<\/p>\n<p>In this article, we discussed the algorithm for rotating Tetris pieces. We started by discussing the rotation problem in general and then moved to define the structure that stores the Tetris pieces and the algorithm for rotating them.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u514d\u8d39\u4fc4\u7f57\u65af\u8272\u60c5, https:\/\/www.blackcurve.com\/casino-non-gamstop\/reviews\/betzino\/. \u4fc4\u7f57\u65af\u65b9\u5757\u65cb\u8f6c\u7b97\u6cd5 Baeldung\u4e2d\u6587\u7f51 1. Overview In this tutorial, we\u2019ll discuss the algorithm behind rotating Tetris pieces. We\u2019ll start by discussing the equation for rotating a shape in general. Then, we\u2019ll define a data structure that can be used to manipulate the Tetris pieces\u2019 rotations. Finally, we\u2019ll present the algorithm to rotate a Tetris piece &#8230;<\/p>\n","protected":false},"author":2001,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":true,"template":"","format":"standard","meta":{"_rtcl_gb_attr":""},"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/ruap.net\/ruap\/wp-json\/wp\/v2\/posts\/23332"}],"collection":[{"href":"https:\/\/ruap.net\/ruap\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ruap.net\/ruap\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ruap.net\/ruap\/wp-json\/wp\/v2\/users\/2001"}],"replies":[{"embeddable":true,"href":"https:\/\/ruap.net\/ruap\/wp-json\/wp\/v2\/comments?post=23332"}],"version-history":[{"count":0,"href":"https:\/\/ruap.net\/ruap\/wp-json\/wp\/v2\/posts\/23332\/revisions"}],"wp:attachment":[{"href":"https:\/\/ruap.net\/ruap\/wp-json\/wp\/v2\/media?parent=23332"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ruap.net\/ruap\/wp-json\/wp\/v2\/categories?post=23332"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ruap.net\/ruap\/wp-json\/wp\/v2\/tags?post=23332"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}