Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Rotating a rectangle
#14
I am confused. I write a response that shows how to do the math and you ask for 4 lines of pygame code. I provide a solution that easily accomplishes what you want using 4 lines of pygame code and you want to do math. Make up your mind please.

Here is one last tkinter example showing how a rotating image can do what you originally requested. I already wrote it so I'm not throwing it away.
from PIL import Image, ImageDraw, ImageTk
import tkinter as tk

angle = 0
image = None
tkimage = None
sail = None

def rotate_sail(delta):
    global angle, tkimage, sail
    angle += delta
    # Create rotated image from original image
    tkimage = ImageTk.PhotoImage(image.rotate(angle))
    if sail:
        canvas.delete(sail)
    sail = canvas.create_image(75, 75, anchor=tk.CENTER, image=tkimage)
    canvas.after(10, rotate_sail, delta)

# Make an image of sail and mast
image = Image.new(mode='RGBA', size=(100, 100), color=0)
draw = ImageDraw.Draw(image)
draw.ellipse((45, 45, 55, 55), fill='black')
draw.line((50, 50, 100, 50), width=5, fill='white')

root = tk.Tk()
canvas = tk.Canvas(root, width=150, height=150, bg='blue')
canvas.pack()
canvas.create_rectangle(55, 50, 95, 100, fill='brown')
rotate_sail(-1)

root.mainloop()
PIL likes angles in degrees. Surprised it wasn't something else like revolutions. Your last example is still using degrees with the math library. You need to use radians. You should also use center when you position the image. It is much easier than trying to position the corner. Remember that rotating the image/surface changes the size, and this changes the location of the corner relative to the center of the image.
Reply


Messages In This Thread
Rotating a rectangle - by CompleteNewb - Aug-23-2021, 09:20 PM
RE: Rotating a rectangle - by deanhystad - Aug-24-2021, 09:54 AM
RE: Rotating a rectangle - by CompleteNewb - Aug-24-2021, 04:15 PM
RE: Rotating a rectangle - by deanhystad - Aug-24-2021, 05:49 PM
RE: Rotating a rectangle - by CompleteNewb - Aug-24-2021, 06:19 PM
RE: Rotating a rectangle - by deanhystad - Aug-24-2021, 08:22 PM
RE: Rotating a rectangle - by BashBedlam - Aug-24-2021, 09:46 PM
RE: Rotating a rectangle - by CompleteNewb - Aug-24-2021, 11:30 PM
RE: Rotating a rectangle - by Windspar - Aug-25-2021, 04:15 AM
RE: Rotating a rectangle - by deanhystad - Aug-25-2021, 03:47 PM
RE: Rotating a rectangle - by CompleteNewb - Aug-25-2021, 03:55 PM
RE: Rotating a rectangle - by deanhystad - Aug-25-2021, 04:34 PM
RE: Rotating a rectangle - by CompleteNewb - Aug-25-2021, 05:27 PM
RE: Rotating a rectangle - by deanhystad - Aug-25-2021, 06:44 PM
RE: Rotating a rectangle - by CompleteNewb - Aug-25-2021, 07:13 PM
RE: Rotating a rectangle - by deanhystad - Aug-25-2021, 07:32 PM
RE: Rotating a rectangle - by deanhystad - Aug-27-2021, 09:39 AM
RE: Rotating a rectangle - by deanhystad - Aug-27-2021, 11:42 AM
RE: Rotating a rectangle - by Windspar - Aug-27-2021, 05:18 PM
RE: Rotating a rectangle - by CompleteNewb - Aug-28-2021, 03:23 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyGame] Surface and rectangle in pygame Fabrizio_fg 6 2,508 May-27-2023, 09:15 AM
Last Post: Fabrizio_fg
  Make rectangle snap to other rectangle corners Pedroski55 2 4,433 Aug-13-2021, 09:59 PM
Last Post: Pedroski55
  [PyGame] Rotating image issue Evoluxman 8 6,300 Oct-12-2019, 12:54 PM
Last Post: Evoluxman
  [PyGame] Rectangle keeps teleporting? keyfive 1 3,255 Jun-27-2018, 11:49 PM
Last Post: sonnekrieger7

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020