Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def rect_from_line(line):
""" Creates a rectangle from a line primitive by thickening it
according to the primitive's aperture size.
Treats rectangular apertures as square because otherwise the maths
becomes too hard for my brain.
"""
r = get_aperture_size(line.aperture) / 2.0
start_v = V.from_tuple(line.start)
end_v = V.from_tuple(line.end)
dir_v = end_v - start_v
# normalize direction vector
abs_dir_v = abs(dir_v)
if abs_dir_v:
dir_v = dir_v / abs_dir_v
else:
dir_v = V(0, 0)
# 45 degree angle means the vector pointing to the new rectangle edges has to be sqrt(2)*r long
v_len = math.sqrt(2)*r
# Give the direction vector the appropriate length
dir_v *= v_len
v1 = (start_v + dir_v.rotate(135, as_degrees=True)).as_tuple()
def rotate(self, theta, as_degrees=False):
""" Adapted from https://gist.github.com/mcleonard/5351452.
Rotate this vector by theta in degrees.
"""
if as_degrees:
theta = math.radians(theta)
dc, ds = math.cos(theta), math.sin(theta)
x, y = dc*self.x - ds*self.y, ds*self.x + dc*self.y
return V(x, y)
def consume_tuple(self, other):
if isinstance(other, tuple) or isinstance(other, list):
return V(other[0], other[1])
return other
Treats rectangular apertures as square because otherwise the maths
becomes too hard for my brain.
"""
r = get_aperture_size(line.aperture) / 2.0
start_v = V.from_tuple(line.start)
end_v = V.from_tuple(line.end)
dir_v = end_v - start_v
# normalize direction vector
abs_dir_v = abs(dir_v)
if abs_dir_v:
dir_v = dir_v / abs_dir_v
else:
dir_v = V(0, 0)
# 45 degree angle means the vector pointing to the new rectangle edges has to be sqrt(2)*r long
v_len = math.sqrt(2)*r
# Give the direction vector the appropriate length
dir_v *= v_len
v1 = (start_v + dir_v.rotate(135, as_degrees=True)).as_tuple()
v2 = (start_v + dir_v.rotate(-135, as_degrees=True)).as_tuple()
v3 = (end_v + dir_v.rotate(-45, as_degrees=True)).as_tuple()
v4 = (end_v + dir_v.rotate(45, as_degrees=True)).as_tuple()
return [v1, v2, v3, v4]
def from_tuple(cls, coordinates):
x, y = coordinates
return V(x, y)
def __mul__(self, other):
other = self.consume_tuple(other)
if isinstance(other, V):
return (self.x * other.x + self.y * other.y)
return V(other * self.x, other * self.y)
def __add__(self, other):
other = self.consume_tuple(other)
return V(self.x + other.x, self.y + other.y)
def __mul__(self, other):
other = self.consume_tuple(other)
if isinstance(other, V):
return (self.x * other.x + self.y * other.y)
return V(other * self.x, other * self.y)