1

Like this

https://i.stack.imgur.com/NinbE.jpg

This is what I have but it's not working and I'm not sure why.

def mirrorDiagonal(picture):
  for sourceX in range(0,getWidth(picture)):
    for sourceY in range (0,getHeight(picture)):
      pex=getPixel(picture,sourceY,sourceX)
      pix=getPixel(picture, sourceX,sourceY)
      color=getColor(pix)
      setColor(pex,color)
John Calzone
  • 41
  • 2
  • 5
  • "it's not working" is very ambiguous. It seems to me from the code that you are rewriting the source buffer with the result data. You need a separate buffer for the input and the output, or swap the pixels (using a temporary variable). If you are going to do an in-place swap, be sure to not swap each pair twice. – John Dvorak Oct 31 '12 at 20:53
  • A reference here: http://stackoverflow.com/a/17138245/1715716 – Gauthier Boaglio Jun 29 '13 at 08:33

2 Answers2

2

Edited this post since I was wrong.

As Jan said, if you want to keep half of the picture, you have to think about not overwriting all of the pixels.

It should be something like this:

def mirrorDiagonal(picture):
    for sourceX in range(0,getWidth(picture)):
        for sourceY in range (sourceX,getHeight(picture)):
            pex=getPixel(picture,sourceY,sourceX)
            pix=getPixel(picture, sourceX,sourceY)
            color=getColor(pix)
            setColor(pex,color)

Still, you might want to think what diagonal you would like to flip on.

gabga
  • 178
  • 1
  • 10
  • Don't you want to get and set both colors rather than one? – Gene Oct 31 '12 at 22:43
  • Well, that would be a good optimization if you didn't want to keep half of the picture. The example picture is simply mirrored in the diagonal. – gabga Oct 31 '12 at 22:57
  • Gah, still can't get it to work. It's hopeless. Ha. – John Calzone Nov 01 '12 at 03:27
  • Maybe you could give a more detailed description on why it doesn't work. It could be that the problem is that width and height are not equal, or anything else. You have to give some more info. – gabga Nov 01 '12 at 11:43
1

Let's say you're in position (x,y)=(7,8) of color blue, the algorithm turns the (8,7) (green) pixel to blue. When your loop arrive at point (8,7) it will find it blue from the prior assignment and you don't have anymore the original color (green). As a result, you end up with the top right diagonal unchanged and its reflection on the bottom left.

In order to restore the original values of the bottom left, you need to store them somewhere and use them for when x > y (bottom left section of the picture)

[EDIT] Note that depending on the geometry of your picture, the transformation (x,y) -> (y,x) may point to a point that doesn't exist. Consider a 50px x 100px picture, when you're at point (50,100) you're calling the pixel (100,50) to change its color while it does not exist in your original picture

yechabbi
  • 1,881
  • 17
  • 14