python计算平面两点距离公式_python距离公式坐标平面误差

本文介绍了在pygame中使用自定义函数`findY`计算平面两点距离来绘制圆形线条的问题。作者遇到数学平方根计算中出现负数导致的错误,并通过取绝对值解决这个问题。然而,最终绘制的线条由于pygame坐标系统的y轴方向相反,导致形状不正确。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我的目标是使用围绕圆的边缘和恒定起点(圆圈的中间)的随机端点在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 =

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值