Python求两点之间的直线距离(2种实现方法)
作者:Ethan的博客 时间:2021-03-28 19:24:46
方法一:
#导入math包
import math
#定义点的函数
class Point:
def __init__(self,x=0,y=0):
self.x=x
self.y=y
def getx(self):
return self.x
def gety(self):
return self.y
#定义直线函数
class Getlen:
def __init__(self,p1,p2):
self.x=p1.getx()-p2.getx()
self.y=p1.gety()-p2.gety()
#用math.sqrt()求平方根
self.len= math.sqrt((self.x**2)+(self.y**2))
#定义得到直线长度的函数
def getlen(self):
return self.len
#设置点p1的坐标为(0,0)
p1=Point(0,0)
#设置点p2的坐标为(3,4)
p2=Point(3,4)
#定义对象
l=Getlen(p1,p2)
#获取两点之间直线的长度
d=l.getlen())
方法二:
import numpy as np
import math
p1=np.array([0,0])
p2=np.array([1000,2000])
p3=p2-p1
p4=math.hypot(p3[0],p3[1])
print(p4)
来源:https://blog.csdn.net/zsc201825/article/details/81629641
标签:Python,两点,直线,距离
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
如何从Python字符串中删除最后一个分号或者逗号
2023-12-22 07:29:53
![](https://img.aspxhome.com/file/2023/0/79470_0s.png)
在Yii框架中使用PHP模板引擎Twig的例子
2023-11-14 11:30:30
不归路系列:Python入门之旅-一定要注意缩进!!!(推荐)
2021-07-08 07:35:00
![](https://img.aspxhome.com/file/2023/5/78735_0s.png)
python实现图片二值化及灰度处理方式
2022-06-29 23:35:34
python学习print中format的用法示例
2023-06-03 14:21:57
php环境配置 php5 MySQL5 apache2 phpmyadmin安装与配置图文教程
2023-11-14 22:08:47
![](https://img.aspxhome.com/file/2023/7/96257_0s.gif)
python常用函数random()函数详解
2022-08-04 18:17:21
能否推荐一个论坛用的数据库结构?
2009-11-01 18:09:00
WSC脚本部件技术:利用Javascript编写ActiveX控件
2008-05-05 13:13:00
用SQL语句删除重复记录的四种方法
2011-05-03 09:25:00
如何用 Python 子进程关闭 Excel 自动化中的弹窗
2023-10-16 15:47:32
![](https://img.aspxhome.com/file/2023/2/71452_0s.png)
浅谈五大Python Web框架
2023-12-10 07:33:25
![](https://img.aspxhome.com/file/2023/6/77866_0s.png)
一个用Ajax做的用户名验证程序
2007-10-21 20:40:00
使用网际数据库浏览器在线维护Access数据库
2008-05-23 13:05:00
Python使用lambda表达式对字典排序操作示例
2022-12-26 06:27:46
基于python + django + whoosh + jieba 分词器实现站内检索功能
2023-03-05 05:30:56
![](https://img.aspxhome.com/file/2023/3/82513_0s.png)
Python GUI编程 文本弹窗的实例
2022-08-24 02:07:48
Python 中对 XML 文件的编码转换问题
2022-08-19 12:46:53
![](https://img.aspxhome.com/file/2023/2/77862_0s.png)
基于Python2、Python3中reload()的不同用法介绍
2023-10-01 17:59:15
Python 中的集合和字典
2021-03-18 22:53:30