用三角形扇形画一个圆圈

时间:2015-05-16 11:05:31

标签: python opengl geometry trigonometry pyglet

我试图用Pyglet绘制一个圆圈但我失败了。但是,结果却是美丽而意想不到的。

我已经计算出数学:

The math

我已经实施了这个方法:

""" Pyglet utilities. Designed to ease drawing of primitives with Pyglet. """

# Dependencies
import pyglet
from math import sin, cos

def circle(x, y, r, p, c, b): # p is number of points in circle EXCLUDING center
    """ Adds a vertex list of circle polygon to batch and returns it. """
    deg = 360 / p
    P = x, y # P for POINTS
    for i in range(p):
        n = deg * i
        P += int(r * cos(n)) + x, int(r * sin(n)) + y
    return b.add(p+1, pyglet.gl.GL_TRIANGLE_FAN, None, ('v2i', P), ('c3B', (c)))

我写了一个测试:

""" This file tests the circle method of utilities module. """

# Dependencies
import pyglet
from utilities import circle

# Constants
WIN = 800, 800, 'TEST', False, 'tool' # x, y, caption, resizable, style
CENTER = WIN[0] // 2, WIN[1] // 2
RADIUS = 300
MAGENTA = (255, 0, 255)
WHITE = (255, 255, 255)
SPEED = 0.5 # in seconds

# Variables
win = pyglet.window.Window(*WIN)
batch = pyglet.graphics.Batch()
points = 1 # excluding center

def on_step(dt):
    """ Logic performed every frame. """
    global batch, points
    batch = pyglet.graphics.Batch()
    points += 1 # 2, 3, 4...
    print (points + 1) # total number of points
    circle(CENTER[0], CENTER[1], RADIUS, points, WHITE+MAGENTA*(points), batch)

@win.event
def on_draw():
    """ Drawing perfomed every frame. """
    win.clear()
    batch.draw()

pyglet.clock.schedule_interval(on_step, SPEED)
pyglet.app.run()

这就是我所拥有的:

Result video at YouTube

有人能指出我做错了吗?

1 个答案:

答案 0 :(得分:1)

您需要将度数更改为弧度...(度/ 180)* pi是您需要的