用三角扇画圆

Drawing a circle with a triangle fan

我尝试用 Pyglet 画一个圆,但失败了。不过,结果很漂亮,出乎意料。

我算出来了:

我已经实现了方法:

""" 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

谁能指出我做错了什么?

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