# How to use the vector.V function in vector

## To help you get started, weβve selected a few vector examples, based on popular ways it is used in public projects.

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()
kirberich / gerber_to_scad / vector.py View on Github
def rotate(self, theta, as_degrees=False):
Rotate this vector by theta in degrees.
"""
if as_degrees:

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)
kirberich / gerber_to_scad / vector.py View on Github
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]

kirberich / gerber_to_scad / vector.py View on Github
def from_tuple(cls, coordinates):
x, y = coordinates
return V(x, y)
kirberich / gerber_to_scad / vector.py View on Github
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)
kirberich / gerber_to_scad / vector.py View on Github
def __add__(self, other):
other = self.consume_tuple(other)
return V(self.x + other.x, self.y + other.y)
kirberich / gerber_to_scad / vector.py View on Github
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)

## vector

Vector classes and utilities

BSD-3-Clause