Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Upon closer look, your code looks correct to me; which makes one wonder whether the original authors had an off-by-one bug. I guess someone ought to look at how OpenCV implements it!</p> <p>Nonetheless, one suggestion to make it easier to understand is to flip the order of the <em>for</em> loops by going over all sizes first, then looping over the possible locations given the size:</p> <pre><code>#include &lt;stdio.h&gt; int main() { int i, x, y, sizeX, sizeY, width, height, count, c; /* All five shape types */ const int features = 5; const int feature[][2] = {{2,1}, {1,2}, {3,1}, {1,3}, {2,2}}; const int frameSize = 24; count = 0; /* Each shape */ for (i = 0; i &lt; features; i++) { sizeX = feature[i][0]; sizeY = feature[i][1]; printf("%dx%d shapes:\n", sizeX, sizeY); /* each size (multiples of basic shapes) */ for (width = sizeX; width &lt;= frameSize; width+=sizeX) { for (height = sizeY; height &lt;= frameSize; height+=sizeY) { printf("\tsize: %dx%d =&gt; ", width, height); c=count; /* each possible position given size */ for (x = 0; x &lt;= frameSize-width; x++) { for (y = 0; y &lt;= frameSize-height; y++) { count++; } } printf("count: %d\n", count-c); } } } printf("%d\n", count); return 0; } </code></pre> <p>with the same results as the previous <code>162336</code></p> <hr> <p>To verify it, I tested the case of a 4x4 window and manually checked all cases (easy to count since 1x2/2x1 and 1x3/3x1 shapes are the same only 90 degrees rotated):</p> <pre><code>2x1 shapes: size: 2x1 =&gt; count: 12 size: 2x2 =&gt; count: 9 size: 2x3 =&gt; count: 6 size: 2x4 =&gt; count: 3 size: 4x1 =&gt; count: 4 size: 4x2 =&gt; count: 3 size: 4x3 =&gt; count: 2 size: 4x4 =&gt; count: 1 1x2 shapes: size: 1x2 =&gt; count: 12 +-----------------------+ size: 1x4 =&gt; count: 4 | | | | | size: 2x2 =&gt; count: 9 | | | | | size: 2x4 =&gt; count: 3 +-----+-----+-----+-----+ size: 3x2 =&gt; count: 6 | | | | | size: 3x4 =&gt; count: 2 | | | | | size: 4x2 =&gt; count: 3 +-----+-----+-----+-----+ size: 4x4 =&gt; count: 1 | | | | | 3x1 shapes: | | | | | size: 3x1 =&gt; count: 8 +-----+-----+-----+-----+ size: 3x2 =&gt; count: 6 | | | | | size: 3x3 =&gt; count: 4 | | | | | size: 3x4 =&gt; count: 2 +-----------------------+ 1x3 shapes: size: 1x3 =&gt; count: 8 Total Count = 136 size: 2x3 =&gt; count: 6 size: 3x3 =&gt; count: 4 size: 4x3 =&gt; count: 2 2x2 shapes: size: 2x2 =&gt; count: 9 size: 2x4 =&gt; count: 3 size: 4x2 =&gt; count: 3 size: 4x4 =&gt; count: 1 </code></pre>
 

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