Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hello, I was wondering if it was possible to write a function in python or jytho

ID: 3672704 • Letter: H

Question

Hello,

I was wondering if it was possible to write a function in python or jython that would allow me to mirror any picture diagonally. The image can be any size and can be rectanglar shape. I can do it if it is a square shape with this code but I cannot do a perfect mirror diagonally with rectangle pictures. Here is my code:

def mirror1():
picture=makePicture(pickAFile())
width=getWidth(picture)
height=getHeight(picture)
#needed this if statement so it mirrors from a square shape
if height>getWidth(picture):
height=getWidth(picture)

for x in range(0,width):
for y in range(0,height):
   oldPixel=getPixel(picture,x,y)
   newPixel=getPixel(picture,y,x)
   color=getColor(newPixel)
   setColor(oldPixel,color)
show(picture)

Any help would be greatly appreciated. Thanks.

Explanation / Answer

def flipDiagonal():
file = pickAFile()
pict = makePicture (file)

# get the width, height and midwid, midhgt
wid = getWidth (pict)
hgt = getHeight(pict)
pict1 = makeEmptyPicture(hgt,wid)

# flip along the diagonal
for x in range(1,wid):
for y in range(1,hgt):
originalPixel = getPixel(pict, x, y)
newPixel = getPixel(pict1, y, x)
c = getColor(originalPixel)
setColor(newPixel,c)

show (pict)
show (pict1)

def mirrorit():
file = pickAFile()
pict = makePicture (file)

# get the width, height and midwid, midhgt
wid = getWidth (pict)
hgt = getHeight(pict)
midwid=wid/2
midhgt = hgt / 2
print wid, hgt, midwid, midhgt

# mirror the upper right corner
for y in range(1,midhgt):
for xOffset in range(1,midwid):
pright = getPixel(pict, xOffset+midwid,y)
pleft = getPixel(pict, midwid-xOffset,y)
c = getColor(pleft)
setColor(pright,c)

# mirror the lower left coner
for yOffset in range(1,midhgt):
for x in range(1,midwid):
pbottom = getPixel(pict, x, yOffset+midhgt)
ptop = getPixel(pict, x, midhgt-yOffset)
c = getColor(ptop)
setColor(pbottom,c)

# mirror the lower right coner
for yOffset in range(1,midhgt):
for xOffset in range(1,midwid):
plrc = getPixel(pict, xOffset+midwid, yOffset+midhgt)
pulc = getPixel(pict, midwid-xOffset, midhgt-yOffset)
c = getColor(pulc)
setColor(plrc,c)

show (pict)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote