示例#1
0
def ngon_outline(x, y, r, sides, start_angle = 0.0):
    """
    Draw the outline of a polygon of n sides of equal length.
    
    @param x, y: center position
    @param r: radius
    @param sides: number of sides in the polygon
    @param start_angle: rotation of the entire polygon
    """
    if r < 1.0: return
    
    #This is necessary if you think about it.
    line_spacing = graphics.line_size/2/math.cos(math.pi/sides)
    
    points_inner = list(_iter_ngon(x, y, r-line_spacing, sides, start_angle))
    points_outer = list(_iter_ngon(x, y, r+line_spacing, sides, start_angle))
    
    points_stroke = _concat(_concat(zip(points_inner, points_outer)))
    points_stroke.extend(points_stroke[:4]) # draw the first *two* points again
    points_inner = _concat(points_inner)
    points_outer = _concat(points_outer)
    
    if graphics.line_size > 1:
        pyglet.gl.glLineWidth(1)
        pyglet.graphics.draw(len(points_stroke)/2,
                pyglet.gl.GL_TRIANGLE_STRIP, ('v2f', points_stroke))
        # pyglet.graphics.draw(len(points_inner)/2,
        #         pyglet.gl.GL_LINE_LOOP, ('v2f', points_inner))
    else:    
        graphics.disable_line_smoothing()
        pyglet.graphics.draw(len(points_outer)/2,
                pyglet.gl.GL_LINE_LOOP, ('v2f', points_outer))
        graphics.enable_line_smoothing()
示例#2
0
def line(x1, y1, x2, y2):
    if graphics.line_size <= 1.0:
        graphics.disable_line_smoothing()
        pyglet.graphics.draw(2, pyglet.gl.GL_LINES, ('v2f', (x1, y1, x2, y2)))
        graphics.enable_line_smoothing()
        return
    angle = math.atan2(y2-y1, x2-x1)
    x_add = math.cos(angle+math.pi/2)*graphics.line_size/2
    y_add = math.sin(angle+math.pi/2)*graphics.line_size/2
    rx1, ry1 = x1 + x_add, y1 + y_add
    rx2, ry2 = x2 + x_add, y2 + y_add
    rx3, ry3 = x2 - x_add, y2 - y_add
    rx4, ry4 = x1 - x_add, y1 - y_add
    pyglet.graphics.draw(4, pyglet.gl.GL_QUADS, ('v2f', (rx1,ry1,rx2,ry2,rx3,ry3,rx4,ry4)))
示例#3
0
def ellipse_outline(x1, y1, x2, y2, dashed=False):
    """
    @param x1, y1, x2, y2:  bounding box corners
    @param dashed:          draws only every other segment if enabled
    """
    
    if abs(x2-x1) < 1.0 or abs(y2-y1) < 1.0: return
    w2 = graphics.line_size / 2.0
    x_dir = 1 if x2 > x1 else -1
    y_dir = 1 if y2 > y1 else -1
    
    x1_out = x1 - x_dir * w2
    x1_in = x1 + x_dir * w2
    x2_out = x2 + x_dir * w2
    x2_in = x2 - x_dir * w2
    
    y1_out = y1 - y_dir * w2
    y1_in = y1 + y_dir * w2
    y2_out = y2 + y_dir * w2
    y2_in = y2 - y_dir * w2
    
    points_inner = list(_iter_ellipse(x1_in, y1_in, x2_in, y2_in, da=0.1, dashed=dashed))
    points_outer = list(_iter_ellipse(x1_out, y1_out, x2_out, y2_out, da=0.1, dashed=dashed))
    
    points_stroke = _concat(_concat(zip(points_inner, points_outer)))
    points_stroke.extend(points_stroke[:4]) # draw the first *two* points again
    points_inner = _concat(points_inner)
    points_outer = _concat(points_outer)
    
    if graphics.line_size > 1:
        pyglet.gl.glLineWidth(1)
        if abs(x2_out-x1_out) < graphics.line_size*2 or abs(y2_out-y1_out) < graphics.line_size*2:
            pyglet.graphics.draw(len(points_outer)/2, 
                pyglet.gl.GL_TRIANGLE_FAN, ('v2f', points_outer))
        else:
            pyglet.graphics.draw(len(points_stroke)/2,
                pyglet.gl.GL_TRIANGLE_STRIP, ('v2f', points_stroke))
        # pyglet.graphics.draw(len(points_inner)/2,
        #         pyglet.gl.GL_LINE_LOOP, ('v2f', points_inner))
    else:
        graphics.disable_line_smoothing()
        pyglet.graphics.draw(len(points_outer)/2,
                pyglet.gl.GL_LINE_LOOP, ('v2f', points_outer))
        graphics.enable_line_smoothing()