You probably should use vectors to calculate the position of the points.
- create the
vector AB
- calculate its
normalized perpendicular
- add or subtract 3 times this to
B
with the help of a simple, reusable Vector class
, the calculation is trivial, and reads like English:
Find the points perpendicular to AB
at distance 3
from point B
:
P1 = B + (B-A).perp().normalized() * 3
P2 = B + (B-A).perp().normalized() * 3
class 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}')
output:
Point(17.683281572999746, 28.658359213500127), and Point(12.316718427000252, 31.341640786499873)
C
andD
? – Unhair