tags:

views:

55

answers:

1

So I am to loop through copyFrom.pixelData and copy it into pixelData.

I realize that I need to check the conditions of i and j, and have them not copy past the boundaries of pixelData[x][y],

I need another 2 loops for that? I tried this, but was getting segmentation fault.. Is this the right approach?

void Image::insert(int xoff, int yoff, const Image& copyFrom, Color notCopy)
{
    for (int x = xoff; x < xoff+copyFrom.width; x++) {
        for (int y = yoff; y < yoff+copyFrom.height; y++) {
            for (int i = 0; i<width; i++) {
                for (int j = 0; j<height; j++) {
                    if (copyFrom.pixelData[i][j].colorDistance(notCopy) > 20)
                        pixelData[x][y]=copyFrom.pixelData[i][j];
                    }
                }
            }
        }
    }
}
A: 

No, you don't need more loops for that.

Instead try:

int copywidth = std::min(width, copyFrom.width-xoff);
// likewise copyheight = ...
for( int x = 0; x < copywidth; x++ ) {
  // likewise for( int y ...
    pixelData[x][y] = copyFrom.pixelData[x+xoff][y+yoff];
}
Ben Voigt
Could you explain 'int copywidth = std::min(width, copyFrom.width-xoff);'
codefail
Which part of that are you confused about?
Ben Voigt
i dont understand what std::min is giving me there..and why you had used copyFrom? Is that just pseudo code for me to alter, because if using copyFrom..it needs to be copyFrom.pixelData[][]
codefail
it is pseudo-code in that I left some blanks for you to fill in, being tagged `homework` and all. But yes, that was supposed to be reading from `copyFrom.pixelData[x+xoff][y+yoff]`. `std::min` is part of the C++ standard library, and I would encourage you to google and try to understand it before asking on SO. You'll find an answer a lot faster and learn more in the process by using a search engine.
Ben Voigt
Yes, sorry..I did look it up.I tihnk i understand how it works.finally played around with the pseudo code and got it working.had to reverse some things to have it read the copyFrom in correctly.'copyFrom.width+xoff' things like that..
codefail
Yeah I realized afterward that `(xoff, yoff)` was the destination offset, not the source offset. But I'm glad it got you moving toward a solution.
Ben Voigt