Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy is my image rotation algorithm not working?
    primarykey
    data
    text
    <h3>Attempts 1 &amp; 2:</h3> <p><em>Note:</em> Removed first attempts to cut down on question size. See community wiki for previous attempts.</p> <h3>Attempt 3:</h3> <p>As per fuzzy-waffle's example, I have implemented the following, which doesn't appear to work correctly. Any ideas what I could be doing wrong?</p> <pre><code>ImageMatrix ImageMatrix::GetRotatedCopy(VDouble angle) { // Copy the specifications of the original. ImageMatrix &amp;source = *this; ImageMatrix &amp;target = CreateEmptyCopy(); double centerX = ((double)(source.GetColumnCount()-1)) / 2; double centerY = ((double)(source.GetRowCount()-1)) / 2; // Remember: row = y, column = x for (VUInt32 y = 0; y &lt; source.GetRowCount(); y++) { for (VUInt32 x = 0; x &lt; source.GetColumnCount(); x++) { double dx = ((double)x) - centerX; double dy = ((double)y) - centerY; double newX = cos(angle) * dx - sin(angle) * dy + centerX; double newY = cos(angle) * dy + sin(angle) * dx + centerY; int ix = (int)round(newX); int iy = (int)round(newY); target[x][y][0] = source[ix][iy][0]; } } return target; } </code></pre> <p>With this prototype matrix...</p> <pre><code>1 2 1 0 0 0 -1 -2 -1 </code></pre> <p>... prototype.GetRotatedCopy(0) (which is correct) ...</p> <pre><code>1 2 1 0 0 0 -1 -2 -1 </code></pre> <p>... prototype.GetRotatedCopy(90) (incorrect) ...</p> <pre><code>-2 0 0 -2 0 2 0 0 2 </code></pre> <p>... prototype.GetRotatedCopy(180) (incorrect - but sort of logical?) ...</p> <pre><code>0 -1 -2 1 0 -1 2 1 0 </code></pre> <p>... prototype.GetRotatedCopy(270) (incorrect - why is this the same as 0 rotation?) ...</p> <pre><code>1 2 1 0 0 0 -1 -2 -1 </code></pre> <h3>Solution:</h3> <p>As pointed out by Mark Ransom, I should be using radians, not degrees; I have adjusted my code as follows:</p> <pre><code>ImageMatrix ImageMatrix::GetRotatedCopy(VDouble degrees) { // Copy the specifications of the original. ImageMatrix &amp;source = *this; ImageMatrix &amp;target = CreateEmptyCopy(); // Convert degree measurement to radians. double angle = degrees / 57.3; // ... rest of code as in attempt #3 ... </code></pre> <p>Thanks for all your help guys!</p> <pre><code>1 2 1 0 0 0 -1 -2 -1 1 2 1 0 0 0 -1 -2 -1 -1 0 1 -2 0 2 -1 0 1 -1 -2 -1 0 0 0 1 2 1 1 0 -1 2 0 -2 1 0 -1 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload