int numRects(m, n) { if (m == 0 || n == 0) { return 0; } assert(m < MAX_DIMENSIONS && n < MAX_DIMENSIONS); if (m > n) { return numRects(n, m); } if (table[m][n] != 0) { return table[m][n]; } table[m][n] = (m * n) + numRects(m-1, n ) + numRects(m , n-1) - numRects(m-1, n-1); return table[m][n]; }
int result() { int closest = 0; int mClosest = 0; int nClosest = 0; for (int m = 1; m < MAX_DIMENSIONS; m++) { for (int n = 1; n < MAX_DIMENSIONS; n++) { int value = numRects(m, n); // printf("%d, %d: %d\n", m, n, value); if (abs(value - TARGET) < abs(closest - TARGET)) { closest = value; mClosest = m; nClosest = n; // printf("%d, %d: %d\n", m, n, value); } if (value > TARGET) { // We've looked at one value past the target, so // we don't need to keep looking in this row. break; } } } return mClosest * nClosest; }
CompRect::vector CompRegion::rects () const { CompRect::vector rv; if (!numRects ()) return rv; if (!priv->region) { rv.push_back (CompRect (priv->box.extents.x1, priv->box.extents.y1, priv->box.extents.x2 - priv->box.extents.x1, priv->box.extents.y2 - priv->box.extents.y1)); return rv; } BOX b; for (int i = 0; i < priv->region->numRects; i++) { b = priv->region->rects[i]; rv.push_back (CompRect (b.x1, b.y1, b.x2 - b.x1, b.y2 - b.y1)); } return rv; }
void PadConfiguration::drawPicture() { if(_picture == nullptr) { drawMapping(); return; } uint8_t* pointer = _picture; uint16_t current(0); #define READ_8 current = *pointer; ++pointer; #define READ_16 current = *((uint16_t*)pointer); ++pointer; ++pointer; READ_16; TFTscreen.fillScreen(current); READ_8; uint16_t numColors(current); for(uint16_t currentColor = 0; currentColor < numColors; ++currentColor) { READ_16; uint16_t color = current; READ_16; uint32_t numRects(current); for(uint16_t currentRect = 0; currentRect < numRects; ++currentRect) { READ_8; int16_t x(current); READ_8; int16_t y(current); READ_8; int16_t w(current); READ_8; int16_t h(current); TFTscreen.fillRect(x, y, w, h, color); } READ_16; uint16_t numVLines(current); for(uint16_t currentVLine = 0; currentVLine < numVLines; ++currentVLine) { READ_8; int16_t x(current); READ_8; int16_t y(current); READ_8; int16_t l(current); TFTscreen.drawFastVLine(x, y, l, color); } READ_16; uint16_t numHLines(current); for(uint16_t currentHLine = 0; currentHLine < numHLines; ++currentHLine) { READ_8; int16_t x(current); READ_8; int16_t y(current); READ_8; int16_t l(current); TFTscreen.drawFastHLine(x, y, l, color); } READ_16; uint16_t numPix(current); for(uint16_t currentPix = 0; currentPix < numPix; ++currentPix) { READ_8; int16_t x(current); READ_8; int16_t y(current); TFTscreen.drawPixel(x, y, color); } } #undef READ_8 #undef READ_16 }