I have two solutions (in Jython) to take two pictures and copy and paste alternate sections of one picture to the other, like so:
for x in range (0,w):
for y in range (0,h):
sourcePx=getPixel(pic1,x,y)
targetPx=getPixel(pic2,x,y)
if (y>=0 and y<20) or (y>=40 and y<60)or (y>=80 and y<=100):
setColor(targetPx, getColor(sourcePx))
repaint (pic2)
for y in range(0,h):
leaf = (y>=0 and y<20) or (y>=40 and y<60)or (y>=80 and y<=100):
if leaf:
for x in range(width):
sourcePx = getPixel(pic1, x, y)
targetPx = getPixel(pic2, x, y)
setColor(targetPx, getColor(sourcePx))
repaint(pic2)
I have been trying to work this out and have two questions about this:
- Which is the more efficient way to loop through the pixels and why?
- Does it make any difference to efficiency (and if so how), creating the variable leaf and then using that in the
if
statement?
(y>=0 and y<20)
to0 <= y < 20
. \$\endgroup\$