【问题标题】:Trying to code the Recaman Sequence, but issue with the parameters I pass for drawing an arc尝试对 Recaman 序列进行编码,但我为绘制弧线传递的参数出现问题
【发布时间】:2019-01-27 01:48:01
【问题描述】:

这个问题有两个部分:

  1. 逻辑 - 构造 Recaman 序列的半圆的酷模式,我的逻辑/代码是否有效?

  2. Pygame 特定问题。 运行代码时出现以下错误:

    pg.draw.arc(screen, black, [xi,yi,dx,dy], 180,0,1) ValueError: 宽度大于椭圆半径

我不知道宽度最终是如何 > 椭圆的半径......我已经研究了大约两个小时,我真的需要一些外部帮助。谢谢!

我的代码:

def recaman():

    sequence = [0]
    limit = 10
    def genNums(n):
        index = 0
        for i in range(1,n):
            if index-i>0 and index-i not in sequence:
                index-=i
                sequence.append(index)
            else:
                index+=i
                sequence.append(index)

    screen = pg.display.set_mode((display_width,display_height),pg.FULLSCREEN)
    tick = 0
    genNums(limit)
    while tick < 1000000:

        zx = display_width//limit
        zy = (display_height/2)//limit
        xi = 0
        yi = 0
        xf = 0
        yf = 0
        curX = 0
        curY = 540
        dx = xf - xi
        dy = yf - yi

        for event in pg.event.get():
            if event.type == pg.QUIT:
                tick = 1000
        screen.fill(white)

        direction = 'down'
        for n in range(0,len(sequence)-1):
            yi = 540
            if n % 2 == 0:
                direction = 'down'
            else:
                direction = 'up'

            if sequence[n+1] > sequence[n]:
                curX += n * zx
                xf = curX
                xi = curX - (n * zx)
            else:
                curX -= n * zx
                xf = curX
                xi = curX + (n*zx)

            if direction == 'up':
                yf = curY - (n*zy)

            if direction == 'down':
                yf = curY + (n*zy)

            p1 = (xi,yi)
            p2 = (xf,yf)
            pg.draw.arc(screen, black, [xi,yi,dx,dy], 180,0,1)

        pg.display.update()
        tick += 1

recaman()

【问题讨论】:

    标签: python pygame logic


    【解决方案1】:

    pygame.draw.arc() 的角度必须设置为弧度。此外,矩形区域的宽度和高度必须大于 0。

    Recaman 序列的部分是半圆。计算一个零件的直径,并计算该零件对应直径的圆周围正方形的左下角原点。绘制上半圆(从0math.pi)或下半圆(从math.pi2*math.pi)。

    import math
    
    line_width = 5
    diameter   = abs(dx) + line_width
    px         = xi if dx > 0 else xi + dx
    py         = display_height/2 - diameter/2.0
    
    if diameter > line_width*2: 
        start_ang = 0 if dy < 0 else math.pi
        end_ang   = start_ang + math.pi + 0.01
        pg.draw.arc(screen, black, [px, py, diameter, diameter], start_ang, end_ang, line_width)
    

    例如曲线与limit = 40:

    完整代码:

    import pygame as pg
    import math
    
    pg.init()
    
    white = (255,255,255)
    black = (  0,  0,  0)
    display_width, display_height = (800,600)
    
    def recaman():
    
        sequence = [0]
        limit = 40
        def genNums(n):
            index = 0
            for i in range(1,n):
                if index-i>0 and index-i not in sequence:
                    index-=i
                    sequence.append(index)
                else:
                    index+=i
                    sequence.append(index)
    
        #screen = pg.display.set_mode((display_width,display_height),pg.FULLSCREEN)
        screen = pg.display.set_mode((display_width,display_height))
        tick = 0
        genNums(limit)
    
        done = False
        while tick < 1000000 and not done:
    
            for event in pg.event.get():
                if event.type == pg.QUIT:
                    done = True
    
            zx = display_width/limit
            zy = (display_height/2)/limit
            xi = 0
            yi = 0
            xf = 0
            yf = 0
            curX = 0
            curY = 500
    
            screen.fill(white)
    
            direction = 'down'
            for n in range(0,len(sequence)-1):
                yi = 500
                if n % 2 == 0:
                    direction = 'down'
                else:
                    direction = 'up'
    
                if sequence[n+1] > sequence[n]:
                    curX += n * zx
                    xf = curX
                    xi = curX - (n * zx)
                else:
                    curX -= n * zx
                    xf = curX
                    xi = curX + (n*zx)
    
                if direction == 'up':
                    yf = curY - (n*zy)
    
                if direction == 'down':
                    yf = curY + (n*zy)
    
                p1 = (xi,yi)
                p2 = (xf,yf)
    
                dx = xf - xi
                dy = yf - yi
    
                line_width = 5
                diameter   = abs(dx) + line_width
                px         = xi if dx > 0 else xi + dx
                py         = display_height/2 - diameter/2.0
    
                if diameter > line_width*2: 
                    start_ang = 0 if dy < 0 else math.pi
                    end_ang   = start_ang + math.pi + 0.01
                    pg.draw.arc(screen, black, [px, py, diameter, diameter], start_ang, end_ang, line_width)
    
            pg.display.update()
            tick += 1
    
    recaman()
    

    【讨论】:

    • 谢谢,你解释得很好!把我的头发拉出来试图让逻辑正确!
    猜你喜欢
    • 2021-05-17
    • 2021-12-17
    • 1970-01-01
    • 2021-02-02
    • 2013-01-03
    • 1970-01-01
    • 2011-06-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多