34580

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: <img src="https://i.stack.imgur.com/sr991.png" alt="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!

Answer1:

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)

Recommend

  • Alternating colors in GPX-Tracks
  • WP: Previous/next post as link in -tag
  • Change form color in ASP.NET
  • How to set the color of JButtons in an array in java?
  • Convert raster images to vector graphics using OpenCV?
  • How do I create a bitmap image as spritefont with multiple character regions?
  • How to add an integer unique id to query results - __efficiently__?
  • drawing ellipses/hyperbolas in R
  • How to keep elements aligned regardless of browser zoom ratio?
  • Why are Google search results in UIWebView not triggering webViewDidFinishLoad?
  • as3 addChild into a generated triangle
  • TextDecorations not being applied to TextBlock during a TreeViewItem Style Trigger
  • interpolation in 3d computer graphics
  • Using VBA to select a dynamic range of cells and create a chart
  • How can I use a custom function within an expression using the eval dataframe method?
  • Two columns in subquery in where clause
  • Why do GeoJSON features appear like a negative photo of the features themselves?
  • I don't get what's the difference between format() and … (python)
  • JavaScript IE rotation transform maths
  • Zoom in and out of jPanel
  • Using a canvas object in a thread to do simple animations - Java
  • How can I enlarge video fullscreen without the affected interface project in as3?
  • OpenGL 3.3 on Mac OSX El Capitan with LWJGL
  • Python urlparse: small issue
  • FileReader+canvas image loading problem
  • Modifying destination and filename of gulp-svg-sprite
  • Deserializing XML into class C#
  • How to show dropdown in excel using jrxml (jasper api)?
  • Function pointer “assignment from incompatible pointer type” only when using vararg ellipsis
  • NSLayoutConstraint that would pin a view to the bottom edge of a superview
  • Acquiring multiple attributes from .xml file in c#
  • How get height of the a view with gone visibility and height defined as wrap_content in xml?
  • How can I remove ASP.NET Designer.cs files?
  • Is there any way to bind data to data.frame by some index?
  • Binding checkboxes to object values in AngularJs
  • How can i traverse a binary tree from right to left in java?
  • How to Embed XSL into XML
  • Net Present Value in Excel for Grouped Recurring CF
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize
  • How to load view controller without button in storyboard?