Search code examples
pythonpython-3.xzelle-graphics

Rotating shapes with zelle (in python)


I have done a code (using John Zelles's graphics module and python 3.10.0) and it works just fine, but the problem is that it needs to rotate 90 degrees anti-clockwise, and I don't know how to do it. I have posted a picture of the final result I got vs what it should look like.

enter image description here

On the left is what I did, and on the right is what it should look like.

Here is my code

win= GraphWin("",100,100)
p1=Point(0,100)
p2=Point(100,0)



for i in range (0,100,10):

    x1=int(p1.getX())
    y1=int(p1.getY())
    x2=int(p2.getX())
    y2=int(p2.getY())
    s= Rectangle(Point(x1,y1-i), Point(x2-i,y2))

    if (i==90 or i==70 or i==50 or i==30 or i==10):
        s.setFill("red")
        s.draw(win)
        s.setOutline("red")

    else:
        s.setFill("white")
        s.draw(win)
        s.setOutline("white")

`


Solution

  • You don't need to rotate it, but flip it upwards. All you need is changing the y axis direction, or use negative y axis values.

    So,

    ...
        y1=-int(p1.getY())
        x2=int(p2.getX())
        y2=-int(p2.getY())
    ...
    

    should do the trick. Not sure which library you use, there may be a way to specify y axis direction (in the call to GraphWin maybe?), or revert y axis diraction as per Reverse Y-Axis in PyPlot.

    Screen coordinates most of the time start at the upper left hand corner of the screen, x increments towards the right, and y increments towards the bottom.