0
votes

I'm trying to draw any regular polygon, so from triangles to polygons with so many corners, it looks like a circle. to make it easier, they must be regular, so a normal pentagon/hexagon/octagon etc. I want to be able to rotate them. What ive tried is to draw a circle and divide 360 by the amount of points i want then, create a point every nth degrees around the circle, putting these points in pygame.draw.polygon() then creates the shape i want, the problem is it isn't the right size, i also want to be able to have stretch the shapes, so have a different width and height.

def regular_polygon(hapi, x, y, w, h, n, rotation, angle_offset = 0):
    #angle_offset is the starting angle in the circle where rotation is rotating the circle
    #so when its an oval, rotation rotates the oval and angle_offset is where on the oval to start from
    if n < 3:
        n = 3

    midpoint = pygame.Vector2(x + w//2, y + h//2)
    r = sqrt(w**2 + h**2)
    #if angle_offset != 0:
        #w = (w//2)//cos(angle_offset)
    #if angle_offset != 90:
        #h = (h//2)//sin(angle_offset)
    w,h = r,r

    points = []

    for angle in range(0, 360, 360//n):
        angle = radians(angle + angle_offset)
        d = pygame.Vector2(-sin(angle)*w//2, -cos(angle)*h//2).rotate(rotation) #the negative sign is because it was drawing upside down

        points.append(midpoint + d)
    #draws the circle for debugging
    for angle in range(0, 360, 1):
        angle = radians(angle + angle_offset)
        d = pygame.Vector2(-sin(angle)*w//2, -cos(angle)*h//2).rotate(rotation)
        pygame.draw.rect(screen, (0,255,0), (midpoint[0] + d[0], midpoint[1] + d[1], 5, 5))

    pygame.draw.polygon(screen,(255,0,0),points)

enter image description here

the red square is the the function above is making, the blue one behind is what it should be

as you can see the circle does line up with the edges of the rect, but because the angles of the circle are not even, the rectangle the func makes is not right.

i think i need to change the circle to an oval but cannot find out how to find the width radius and height radius of it. currently ive found the radius by using pythag.

this is what happens when i dont change the width or height

enter image description here

1
I am not entirely sure what you are trying to do, but I think that maybe this answer might address your question, or some part of it: stackoverflow.com/questions/55842453/… It does not address rotation, but it calculates a list of the vertices of regular polygons; these can be rotated about the center of the circumcenter, to rotate the polygon. This rotation is a vanilla translate center to origin, multiply by rotation matrix, translate back to where it should be.Reblochon Masque
@ReblochonMasque that question is definitely on the right track, but as you can see in the answer, the pentagon and triangle are both in the circle, but are different sizes, as in, if you wanted to draw a triangle and pentagon with the same width, using that/this method will will create the same circle, but different triangle and pentagon widthsThe Big Kahuna
It is not clear what you call the "size" of a polygon. Is it the side length, the area, the diameter of the circumcircle, some other measure...? What you call the "width" is also not clear. Maybe you could define these two attributes better.Reblochon Masque
@ReblochonMasque to simplify things, lets say the polygon is a rectangle (like above), the width would be the width of the rectangle (nothing to do with circle), size is also the width and the height of the rectangle. not accounting for rotation as that would change thingsThe Big Kahuna
A rectangle is not a regular polygon; I am not sure I understand what you want to do.Reblochon Masque

1 Answers

0
votes

I found the solution

doing w *= math.sqrt(2) and h *= math.sqrt(2) works perfectly. dont quite understand the math, but after trial and error, this works. You can probable find the maths here but i just multiplied the width and height by a number and printed that number when it lined up which was very close to the sqrt(2)