Peine
Peine

Reputation: 101

python draw pie shapes with colour filled

I am trying to draw a pie shape with filled colour. I've tried to do this in different ways. Here's the code:

ball = pygame.draw.circle(self.screen, self.pink, self.pos, self.r, 0)
pygame.gfxdraw.pie(self.screen, 60,60, 40, 0, 90,(0,255,0))
pygame.gfxdraw.arc(self.screen, 60,60, 40, 180, 270,(0,255,255))
pygame.draw.arc(self.screen, (255,0,255),ball,0, math.pi/4, ball.width/2)

The output image is like:
enter image description here

I want the pie shapes filled with colour, as the magenta coloured shape does. I used the arc function and set the line with = the radius to achieve this (4th line in the code). However, the colour isn't evenly filled. I also tried to draw a pie shape (2nd line in the code) However, I cannot find a way to fill the colour...

Thank you very much for your help!

Upvotes: 2

Views: 4092

Answers (1)

Graipher
Graipher

Reputation: 7186

You can just draw a sufficiently fine polygon (e.g in one degree intervals):

import math
import pygame

# Center and radius of pie chart
cx, cy, r = 100, 320, 75

# Background circle
pygame.draw.circle(screen, (17, 153, 255), (cx, cy), r)

# Calculate the angle in degrees
angle = val*360/total

# Start list of polygon points
p = [(cx, cy)]

# Get points on arc
for n in range(0,angle):
    x = cx + int(r*math.cos(n*math.pi/180))
    y = cy+int(r*math.sin(n*math.pi/180))
    p.append((x, y))
p.append((cx, cy))

# Draw pie segment
if len(p) > 2:
    pygame.draw.polygon(screen, (0, 0, 0), p)

Upvotes: 5

Related Questions