2015-05-16 286 views
0

我試圖用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

任何人都可以指出我做錯了什麼嗎?

+2

三角函數通常是弧度,而不是學位。 –

+0

@ YvesDaoust哦...是的,它現在有效。謝謝! –

回答

1

你需要改變的程度,弧度......(度/ 180)* pi是你所需要的