python距离公式坐标平面误差
python distance formula coordinate plane error
我的目标是用 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() 中的值更改为其绝对值。这使得函数不会引发任何错误,但绘制的圆看起来像这样:
我知道 pygame 的坐标平面的 y 值是颠倒的,但这会有什么不同吗?
获得均匀分布角度的一种方法是在 0
和 2 * math.pi
之间生成一个随机角度 theta
,然后使用三角函数找到角度的坐标线的终点:
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))
# ...
我能做到:
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)
它修复了负问题的平方根,并通过计算 x 或 y 并将它们的限制设置为指定长度来均匀分布线,以避免某些线的奇怪长度。
我的目标是用 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() 中的值更改为其绝对值。这使得函数不会引发任何错误,但绘制的圆看起来像这样:
我知道 pygame 的坐标平面的 y 值是颠倒的,但这会有什么不同吗?
获得均匀分布角度的一种方法是在 0
和 2 * math.pi
之间生成一个随机角度 theta
,然后使用三角函数找到角度的坐标线的终点:
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))
# ...
我能做到:
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)
它修复了负问题的平方根,并通过计算 x 或 y 并将它们的限制设置为指定长度来均匀分布线,以避免某些线的奇怪长度。