Drawing rects and polygons

Asked by Ryan Hope

What is the recommended way of drawing filled rects and polygons?

Question information

Language:
English Edit question
Status:
Answered
For:
PyGL3Display Edit question
Assignee:
No assignee Edit question
Last query:
Last reply:
Revision history for this message
David Griffin (habilain) said :
#1

For now, you have to use Triangles to draw an n-gon. I do plan a n-gon primitive in future, but it isn't yet implemented. So for a rect, the code would be something like:

tl, tr, bl, br = (some list of coordinates)

rect = Triangles(vertices=[tl, tr, bl, bl, br, tr]) # draw the two triangles of a rectangle

For an arbitary n-gon, you're better of using a fan. So code could go something like this:

import itertools
center = (0, 0)
coords = [list of some coordinates]
length = len(coords)

ngon = Triangle(vertices=list(itertools.chain([(coords[x], coords[(x+1) % length], center) for x in xrange(length)]))

Note: neither piece of code is tested, but I'm pretty confident both should work (perhaps with minor modification)

Revision history for this message
David Griffin (habilain) said :
#2

Just noticed I changed terminology from the original question. If it isn't familiar to anyone, an n-gon is an n sided polygon.

Can you help with this problem?

Provide an answer of your own, or ask Ryan Hope for more information if necessary.

To post a message you must log in.