python画一条竖直的直线_在另一直线的一点上画一条固定长度的垂直线

您可能应该使用向量来计算点的位置。在创建vector AB

计算其normalized perpendicular

加上或减去3倍于B

在一个简单、可重用的Vector class的帮助下,计算非常简单,读起来像英语:

在距点B的距离3处找到垂直于AB的点:

P1 = B + (B-A).perp().normalized() * 3P2 = B + (B-A).perp().normalized() * 3class Vector:

def __init__(self, x, y):

self.x = x

self.y = y

def __sub__(self, other):

return Vector(self.x - other.x, self.y - other.y)

def __add__(self, other):

return Vector(self.x + other.x, self.y + other.y)

def dot(self, other):

return self.x * other.x + self.y * other.y

def norm(self):

return self.dot(self)**0.5

def normalized(self):

norm = self.norm()

return Vector(self.x / norm, self.y / norm)

def perp(self):

return Vector(1, -self.x / self.y)

def __mul__(self, scalar):

return Vector(self.x * scalar, self.y * scalar)

def __str__(self):

return f'({self.x}, {self.y})'

A = Vector(10, 20)

B = Vector(15, 30)

AB = B - A

AB_perp_normed = AB.perp().normalized()

P1 = B + AB_perp_normed * 3

P2 = B - AB_perp_normed * 3

print(f'Point{P1}, and Point{P2}')

输出:

^{pr2}$

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值