void approximateWithTriangles(const Picture& src, Picture& dst, size_t x1, size_t y1, size_t x2, size_t y2) { size_t rgbDst1[3] = {0,0,0}, rgbDst2[3] = {0,0,0}; size_t tr1Pixels = 0, tr2Pixels = 0; for(size_t x = x1; x < x2; ++x) { for(size_t y = y1; y < y2; ++y) { Color color = src.color(x,y); if(y - y1 >= x - x1) ++tr1Pixels; else ++tr2Pixels; for(size_t i = 0; i < 3; ++i) { if(y - y1 >= x - x1) rgbDst1[i] += color[i]; else rgbDst2[i] += color[i]; } } } for(size_t i = 0; i < 3; ++i) { if(tr1Pixels > 0) rgbDst1[i] /= tr1Pixels; if(tr2Pixels > 0) rgbDst2[i] /= tr2Pixels; } if(tr1Pixels > 0) { int v1[] = {x1, y1, x1, y2 - 1, x2 - 1, y2 - 1}; Triangle t1(v1, Color(rgbDst1[0], rgbDst1[1], rgbDst1[2], 255)); dst.paintTriangle(t1); triangles.push_back(t1); } if(tr2Pixels > 0) { int v2[] = {x1 + 1, y1, x2-1, y1, x2-1, y2 - 2}; Triangle t2(v2, Color(rgbDst2[0], rgbDst2[1], rgbDst2[2], 255)); dst.paintTriangle(t2); triangles.push_back(t2); } }
double Picture::distance(const Picture& pic) const { assert(m_w == pic.m_w && m_h == pic.m_h); double dsum = 0; for(size_t x = 0; x < m_w; ++x) { for(size_t y = 0; y < m_h; ++y) { Color c1 = color(x,y), c2 = pic.color(x,y); double tmp = 0; for(size_t i = 0; i < 3; ++i) { double p = c1[i] - (double)c2[i]; tmp += p*p; } dsum += sqrt(tmp); } } return dsum; }