python距离公式坐标平面误差

时间:2015-11-29 22:07:09

标签: python pygame distance

我的目标是在pygame中使用直线制作圆形,使用围绕圆的边缘的随机端点和恒定的起点(圆的中间)。所以我决定将pygame.draw.line函数:screen,aRandomColor,startingPosition和endingPosition作为参数。结束位置是包含随机生成的x值的元组,辅助函数将根据圆的半径计算y值。我的第一个函数计算y值,如下所示:

import math
import random

def findY(pos1, pos2, distance, bothValues=False):
    p1 =pos1
    p2 = pos2
    x1 = float(p1[0])
    y1 = float(p1[1])
    x2 = float(p2[0])
    d = float(distance)

    y2 = y1 - math.sqrt(d**2 - (x1-x2)**2)
    _y2 = y1 + math.sqrt(d**2 - (x1-x2)**2)
    if bothValues==True:
        return y2, _y2
    else:
        return y2

和线条抽屉:

width = 500
height = 500

def randLine(surface, color=rand, start=rand, end=rand,length=rand):
    if start==rand:
        start = randPos()
    if end==rand:
        end = randPos()
    if color==rand:
        color = randColor()
    if length != rand:

        end_x = float(random.randint(0,width))
        end_pos = (end_x, "y")
        y2=findMissing(start_pos, end_pos,l,bothValues=True)
        a = random.randint(0,1)
        if a==0:
            y2 = float(y2[0])
        else:
            y2 = float(y2[1])
        lst = list(end_pos)
        lst[1] = y2
        end_pos = tuple(lst)
    pygame.draw.line(surface, color, start_pos, end_pos)

然后:

drawRandLine(screen,start=(200,200),lenght=100)

(那些称为randPos的其他功能不是问题)。由于某种原因,这会产生一个错误,我诊断为math.sqrt()中的值是负数。但这种情况不可能发生,因为那里的每一个价值都提高到2的幂,这就是我所困惑的。所以我将math.sqrt()中的值更改为其绝对值。这使得该函数不会引起任何错误,但绘制的圆圈看起来像这样:

wacky inverted circloid

我知道pygame的坐标平面值是颠倒的,但这应该有所不同吗?

2 个答案:

答案 0 :(得分:3)

获得均匀角度分布的一种方法是在theta0之间生成随机角度2 * math.pi,并使用三角函数来查找终点的坐标该行:

def drawRandLineTrig(start_pos, length):
    theta = random.rand() * 2 * math.pi
    end_pos = (start_pos[0] + length*math.cos(theta), start_pos[1] + length*math.sin(theta))
    # ...

答案 1 :(得分:0)

我能做到:

def randPos(origin=(0,0), xdis=width, ydis=height):
    randx = random.randint(float(origin[0])-xdis,float(origin[0])+xdis)
    randy = random.randint(float(origin[1])-ydis,float(origin[1])+ydis)
    return (randx, randy)

def drawRandLine(surface, color=random, start_pos=random, end_pos=random,l=random):
    if start_pos==random:
        start_pos = randPos()
    if end_pos==random:
        end_pos = randPos()
    if color==random:
        color = randColor()
    if l != random:
        b = random.randint(0,1)
        l=float(l)
        origin = start_pos
        dis = l
        if b==0:
            end_x = float(random.randint((origin[0]-dis),(origin[0]+dis)))
            end_pos = (end_x, "y")
            y2=findMissing(start_pos, end_pos,l,bothValues=True)
            a = random.randint(0,1)
            if a==0:
                y2 = float(y2[0])
            else:
                y2 = float(y2[1])
            lst = list(end_pos)
            lst[1] = y2
            end_pos = tuple(lst)
        else:
            end_y = float(random.randint((origin[1]-dis),(origin[1]+dis)))
            end_pos = ("x", end_y)
            x2=findMissing(start_pos, end_pos,l,bothValues=True)
            a = random.randint(0,1)
            if a==0:
                x2 = float(x2[0])
            else:
                x2 = float(x2[1])
            lst = list(end_pos)
            lst[0] = x2
            end_pos = tuple(lst)

    pygame.draw.line(surface, color, start_pos, end_pos)

修正了负面问题的sqrt,并通过计算x或y来设置均匀的线分布,并将其限制设置为指定的长度,以避免某些线的奇怪长度。