# Python math.tau() Examples

The following are 30 code examples of math.tau(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module , or try the search function .
Example #1
```def try_numerical_answer(question: str) -> Optional[Tuple[str, str]]:
if question.endswith('?') or question.endswith('.') or question.endswith('!'):
question = question[:-1]
words = NON_WORD_CHARS.split(question)
starting_idx = None
for idx, word in enumerate(words):
if NUMERICAL_EXPRESSION_STARTER.match(word):
starting_idx = idx
break
if starting_idx is None:
return
words = words[starting_idx:]
length = len(words)
ending_idx = None
for idx, word in enumerate(reversed(words)):
if NUMERICAL_EXPRESSION_ENDER.match("".join(reversed(word))):
ending_idx = length - idx
break
expression = " ".join(words[:ending_idx])
try:
result = numexpr.evaluate(expression, local_dict={'pi': math.pi, 'tau': math.tau, 'e': math.e}, global_dict={})
except Exception:
return
return expression, str(result) ```
Example #2
```def cubic_bezier_from_arc(
center: Vector = (0, 0), radius: float = 1, start_angle: float = 0, end_angle: float = 360,
segments: int = 1) -> Iterable[Bezier4P]:
"""
Returns an approximation for a circular 2D arc by multiple cubic Bézier curves.

Args:
center: circle center as :class:`Vector` compatible object
start_angle: start angle in degrees
end_angle: end angle in degrees
segments: count of spline segments, at least one segment for each quarter (90 deg), ``1`` for as few as needed.

"""
center = Vector(center)
for control_points in cubic_bezier_arc_parameters(start_angle, end_angle, segments):
defpoints = [center + (p * radius) for p in control_points]
yield Bezier4P(defpoints) ```
Example #3
```def cubic_bezier_from_ellipse(ellipse: 'ConstructionEllipse', segments: int = 1) -> Iterable[Bezier4P]:
"""
Returns an approximation for an elliptic arc by multiple cubic Bézier curves.

Args:
ellipse: ellipse parameters as :class:`~ezdxf.math.ConstructionEllipse` object
segments: count of spline segments, at least one segment for each quarter (pi/2), ``1`` for as few as needed.

"""
from ezdxf.math import param_to_angle
start_angle = param_to_angle(ellipse.ratio, ellipse.start_param) % math.tau
end_angle = param_to_angle(ellipse.ratio, ellipse.end_param) % math.tau

def transform(points: Iterable[Vector]) -> Iterable[Vector]:
center = Vector(ellipse.center)
x_axis = ellipse.major_axis
y_axis = ellipse.minor_axis
for p in points:
yield center + x_axis * p.x + y_axis * p.y

for defpoints in cubic_bezier_arc_parameters(start_angle, end_angle, segments):
yield Bezier4P(tuple(transform(defpoints))) ```
Example #4
```def test_swap_axis_arbitrary_params():
random_tests_count = 100
random.seed(0)

for _ in range(random_tests_count):
ellipse = ConstructionEllipse(
# avoid (0, 0, 0) as major axis
major_axis=(non_zero_random(), non_zero_random(), 0),
ratio=2,
start_param=random.uniform(0, math.tau),
end_param=random.uniform(0, math.tau),
extrusion=(0, 0, random.choice((1, -1))),
)

# Test if coordinates of start- and end point stay at the same location
# before and after swapping axis.
start_point = ellipse.start_point
end_point = ellipse.end_point
minor_axis = ellipse.minor_axis
ellipse.swap_axis()
assert ellipse.major_axis.isclose(minor_axis, abs_tol=1e-9)
assert ellipse.start_point.isclose(start_point, abs_tol=1e-9)
assert ellipse.end_point.isclose(end_point, abs_tol=1e-9) ```
Example #5
```def test_params_from_vertices_random():
center = Vector.random(5)
major_axis = Vector.random(5)
extrusion = Vector.random()
ratio = 0.75
e = ConstructionEllipse(center, major_axis, extrusion, ratio)

params = [random.uniform(0.0001, math.tau - 0.0001) for _ in range(20)]
vertices = e.vertices(params)
new_params = e.params_from_vertices(vertices)
for expected, param in zip(params, new_params):
assert math.isclose(expected, param)

# This creates the same vertex as v1 and v2
v1, v2 = e.vertices([0, math.tau])
assert v1.isclose(v2)

# This should create the same param for v1 and v2, but
# floating point inaccuracy produces unpredictable results:
p1, p2 = e.params_from_vertices((v1, v2))

assert math.isclose(p1, 0, abs_tol=1e-9) or math.isclose(p1, math.tau, abs_tol=1e-9)
assert math.isclose(p2, 0, abs_tol=1e-9) or math.isclose(p2, math.tau, abs_tol=1e-9) ```
Example #6
```def test_angle_about():
extrusion = Vector(0, 0, 1)
a = Vector(1, 0, 0)
b = Vector(1, 1, 0)
assert math.isclose(a.angle_between(b), math.pi / 4)
assert math.isclose(extrusion.angle_about(a, b), math.pi / 4)

extrusion = Vector(0, 0, -1)
assert math.isclose(a.angle_between(b), math.pi / 4)
assert math.isclose(extrusion.angle_about(a, b), (-math.pi / 4) % math.tau)

extrusion = Vector(0, 0, 1)
a = Vector(1, 1, 0)
b = Vector(1, 1, 0)
assert math.isclose(a.angle_between(b), 0, abs_tol=1e-5)

extrusion = Vector(0, 1, 0)
a = Vector(1, 1, 0)
b = Vector(0, 1, -1)
assert math.isclose(a.angle_between(b), math.pi / 3, abs_tol=1e-5)
c = a.cross(b)
assert math.isclose(extrusion.angle_about(a, b), math.pi / 2) ```
Example #7
```def circle(num_t, radius=1.0, phase=0.0):
"""Returns x and y positions of `num_t` points regularly placed around a circle

Parameters
----------
num_t : int
the number of points around the circle
phase : float, default 0.0
angle shift w/r to the x axis in radians

Returns
-------
points : np.Ndarray of shape (num_t, 2), the x, y positions of the points

"""
if not num_t:
return np.zeros((1, 2))

theta = np.arange(0, tau, tau / num_t)
return np.vstack([radius * np.cos(theta + phase), radius * np.sin(theta + phase)]).T ```
Example #8
```def hexa_disk(num_t, radius=1):
"""Returns an arrays of x, y positions of points evenly spread on
a disk with num_t points on the periphery.

Parameters
----------
num_t : int
the number of poitns on the disk periphery, the rest of the disk is
filled automaticaly

"""

n_circles = int(np.ceil(num_t / tau) + 1)
if not n_circles:
return np.zeros((1, 2))

num_ts = np.linspace(num_t, 0, n_circles, dtype=int)
phases = np.pi * num_ts / num_t
return np.concatenate(
[circle(n, r, phi) for n, r, phi in zip(num_ts, rads, phases)]
) ```
Example #9
```def __init__(self, seed):
self.rnd = np.random.RandomState(seed)
self.size = 256
self.indices = np.arange(self.size, dtype = np.int16)
self.rnd.shuffle(self.indices)
theta = np.linspace(0, math.tau, self.size, endpoint=False)
Example #10
```def groupDelay(data: List[Datapoint], index: int) -> float:
idx0 = clamp_value(index - 1, 0, len(data) - 1)
idx1 = clamp_value(index + 1, 0, len(data) - 1)
delta_angle = data[idx1].phase - data[idx0].phase
delta_freq = data[idx1].freq - data[idx0].freq
if delta_freq == 0:
return 0
if abs(delta_angle) > math.tau:
if delta_angle > 0:
delta_angle = delta_angle % math.tau
else:
delta_angle = -1 * (delta_angle % math.tau)
val = -delta_angle / math.tau / delta_freq
return val ```
Example #11
```def screen_shake(self, dist=25):
"""Trigger a screen shake effect.

The camera will be offset from ``.pos`` by ``dist`` in a random
direction; then steady itself in a damped harmonic motion.

"""
theta = np.random.uniform(0, math.tau)
basis = np.array([theta, + math.pi * 0.5])
self._cam_offset[:] = dist * np.sin(basis)
self._xform[-1][:2] = self._cam_offset - self._pos
Example #12
```def girdle_coords(radius, mat):
angle = tau / 64

return tuple(
(
mat @ Vector(
(
0.0,
)
)
).freeze()
for i in range(64)
) ```
Example #13
```def random_angle():
return random.uniform(0, math.tau) ```
Example #14
```def ellipse(major_axis=(1, 0), ratio: float = 0.5, start: float = 0, end: float = math.tau, count: int = 8):
major_axis = Vector(major_axis).replace(z=0)
ellipse_ = Ellipse.new(dxfattribs={
'center': (0, 0, 0),
'major_axis': major_axis,
'ratio': min(max(ratio, 1e-6), 1),
'start_param': start,
'end_param': end
}, doc=doc)
control_vertices = list(ellipse_.vertices(ellipse_.params(count)))
axis_vertices = list(ellipse_.vertices([0, math.pi / 2, math.pi, math.pi * 1.5]))
return ellipse_, control_vertices, axis_vertices ```
Example #15
```def sine_wave(count: int, scale: float = 1.0):
for t in linspace(0, math.tau, count):
yield Vector(t * scale, math.sin(t) * scale) ```
Example #16
```def sine_wave(count: int, scale: float = 1.0) -> Iterable[Vector]:
for t in linspace(0, math.tau, count):
yield Vector(t * scale, math.sin(t) * scale) ```
Example #17
```def rational_spline_from_arc(
center: Vector = (0, 0), radius: float = 1, start_angle: float = 0, end_angle: float = 360,
segments: int = 1) -> BSpline:
"""
Returns a rational B-splines for a circular 2D arc.

Args:
center: circle center as :class:`Vector` compatible object
start_angle: start angle in degrees
end_angle: end angle in degrees
segments: count of spline segments, at least one segment for each quarter (90 deg), ``1`` for as few as needed.

"""
center = Vector(center)
control_points, weights, knots = nurbs_arc_parameters(start_angle, end_angle, segments)
return BSpline(
control_points=(center + (p * radius) for p in control_points),
weights=weights,
knots=knots,
order=3,
) ```
Example #18
```def enclosing_angles(angle, start_angle, end_angle, ccw=True, abs_tol=TOLERANCE):
isclose = partial(math.isclose, abs_tol=abs_tol)

s = start_angle % math.tau
e = end_angle % math.tau
a = angle % math.tau
if isclose(s, e):
return isclose(s, a)

if s < e:
r = s < a < e
else:
r = not (e < a < s)
return r if ccw else not r ```
Example #19
```def __init__(self, center: 'Vertex' = NULLVEC, major_axis: 'Vertex' = X_AXIS, extrusion: 'Vertex' = Z_AXIS,
ratio: float = 1, start_param: float = 0, end_param: float = math.tau, ccw: bool = True):
self.center = Vector(center)
self.major_axis = Vector(major_axis)
self.extrusion = Vector(extrusion)
self.ratio = float(ratio)
self.start_param = float(start_param)
self.end_param = float(end_param)
if not ccw:
self.start_param, self.end_param = self.end_param, self.start_param
self.minor_axis = minor_axis(self.major_axis, self.extrusion, self.ratio) ```
Example #20
```def param_span(self) -> float:
""" Returns params span of ellipse from start- to end param. """
end = self.end_param
if end < self.start_param:
end += math.tau
return end - self.start_param ```
Example #21
```def swap_axis(self) -> None:
""" Swap axis and adjust start- and end parameter. """
self.major_axis = self.minor_axis
ratio = 1.0 / self.ratio
self.ratio = max(ratio, 1e-6)
self.minor_axis = minor_axis(self.major_axis, self.extrusion, self.ratio)

start_param = self.start_param
end_param = self.end_param
if math.isclose(start_param, 0) and math.isclose(end_param, math.tau):
return
self.start_param = (start_param - HALF_PI) % math.tau
self.end_param = (end_param - HALF_PI) % math.tau ```
Example #22
```def mid_param(start: float, end: float) -> float:
if end < start:
end += math.tau
return (start + end) / 2.0 ```
Example #23
```def ellipse_edges_to_spline_edges(self, num: int = 32) -> None:
"""
Convert all ellipse edges to spline edges (approximation).

Args:
num: count of control points for a **full** ellipse, partial ellipses have proportional fewer control points
but at least 3.

"""

def to_spline_edge(e: EllipseEdge) -> SplineEdge:
# No OCS transformation needed, source ellipse and target spline reside in the same OCS.
# ezdxf stores angles always in counter-clockwise orientation.

ellipse = ConstructionEllipse(
center=e.center, major_axis=e.major_axis, ratio=e.ratio,
start_param=e.start_param, end_param=e.end_param,
)
count = max(int(float(num) * ellipse.param_span / math.tau), 3)
tool = BSpline.ellipse_approximation(ellipse, count)
spline = SplineEdge()
spline.degree = tool.degree
if not e.ccw:
tool = tool.reverse()

spline.control_points = Vec2.list(tool.control_points)
spline.knot_values = tool.knots()
spline.weights = tool.weights()
return spline

for path_index, path in enumerate(self.paths):
if path.PATH_TYPE == 'EdgePath':
edges = path.edges
for edge_index, edge in enumerate(edges):
if edge.EDGE_TYPE == 'EllipseEdge':
edges[edge_index] = to_spline_edge(edge) ```
Example #24
```def ellipse_edges_to_line_edges(self, num: int = 64) -> None:
""" Convert all ellipse edges to line edges (approximation).

Args:
num: count of control points for a **full** ellipse, partial ellipses have proportional fewer control points
but at least 3.

"""

def to_line_edges(edge):
ellipse = ConstructionEllipse(
center=edge.center,
major_axis=edge.major_axis,
ratio=edge.ratio,
start_param=edge.start_param,
end_param=edge.end_param,
)
segment_count = max(int(float(num) * ellipse.param_span / math.tau), 3)
params = ellipse.params(segment_count + 1)
if not edge.ccw:
params = reversed(list(params))
vertices = list(ellipse.vertices(params))
for v1, v2 in zip(vertices[:-1], vertices[1:]):
line = LineEdge()
line.start = v1.vec2
line.end = v2.vec2
yield line

for path in self.paths:
if path.PATH_TYPE == 'EdgePath':
new_edges = []
for edge in path.edges:
if edge.EDGE_TYPE == 'EllipseEdge':
new_edges.extend(to_line_edges(edge))
else:
new_edges.append(edge)
path.edges = new_edges ```
Example #25
```def params(self, num: int) -> Iterable[float]:
""" Returns `num` params from start- to end param in counter clockwise order.

All params are normalized in the range from [0, 2pi).

"""
start = self.dxf.start_param % math.tau
end = self.dxf.end_param % math.tau
yield from ellipse.get_params(start, end, num) ```
Example #26
```def draw_elliptic_arc_entity_3d(self, entity: DXFGraphic) -> None:
dxf, dxftype = entity.dxf, entity.dxftype()
properties = self._resolve_properties(entity)

if dxftype in {'CIRCLE', 'ARC'}:
center = dxf.center  # ocs transformation in .from_arc()
if dxftype == 'CIRCLE':
start_angle = 0
end_angle = 360
else:
start_angle = dxf.start_angle
end_angle = dxf.end_angle
e = ConstructionEllipse.from_arc(center, radius, dxf.extrusion, start_angle, end_angle)
elif dxftype == 'ELLIPSE':
e = cast(Ellipse, entity).construction_tool()
else:
raise TypeError(dxftype)

# Approximate as 3D polyline
segments = int((e.end_param - e.start_param) / math.tau * self.circle_approximation_count)
points = list(e.vertices(linspace(e.start_param, e.end_param, max(4, segments + 1))))
self.out.start_path()
for a, b in zip(points, points[1:]):
self.out.draw_line(a, b, properties)
self.out.end_path() ```
Example #27
```def _draw_angles_to_start_and_span(draw_angles: Tuple[float, float]) -> Tuple[float, float]:
a, b = draw_angles
if b < a:  # arc crosses the discontinuity at n*360
b += math.tau
start_angle = -math.degrees(a)
span_angle = -math.degrees(b - a)
return start_angle, span_angle ```
Example #28
```def test_from_arc():
ellipse = ConstructionEllipse.from_arc(center=(2, 2, 2), radius=3)
assert ellipse.center == (2, 2, 2)
assert ellipse.major_axis == (3, 0, 0)
assert ellipse.ratio == 1
assert ellipse.start_param == 0
assert math.isclose(ellipse.end_param, math.tau) ```
Example #29
```def test_angle_to_param():
random_tests_count = 100
random.seed(0)

angle = 1.23
assert math.isclose(angle_to_param(1.0, angle), angle)

angle = 1.23 + math.pi / 2
assert math.isclose(angle_to_param(1.0, angle), angle)

angle = 1.23 + math.pi
assert math.isclose(angle_to_param(1.0, angle), angle)

angle = 1.23 + 3 * math.pi / 2
assert math.isclose(angle_to_param(1.0, angle), angle)

angle = math.pi / 2 + 1e-15
assert math.isclose(angle_to_param(1.0, angle), angle)

for _ in range(random_tests_count):
ratio = random.uniform(1e-6, 1)
angle = random.uniform(0, math.tau)
param = angle_to_param(ratio, angle)
ellipse = ConstructionEllipse(
# avoid (0, 0, 0) as major axis
major_axis=(non_zero_random(), non_zero_random(), 0),
ratio=ratio,
start_param=0,
end_param=param,
extrusion=(0, 0, random.choice((1, -1))),
)
calculated_angle_without_direction = ellipse.major_axis.angle_between(ellipse.end_point)
assert math.isclose(calculated_angle, angle, abs_tol=1e-9)
assert (math.isclose(calculated_angle, calculated_angle_without_direction) or
math.isclose(math.tau - calculated_angle, calculated_angle_without_direction)) ```
Example #30
```def test_cubic_bezier_arc_parameters():
parts = list(cubic_bezier_arc_parameters(0, math.tau))
assert len(parts) == 4

chk = 4.0 * (math.sqrt(2) - 1.0) / 3.0
sp, cp1, cp2, ep = parts[0]
assert sp.isclose((1, 0))
assert cp1.isclose((1, chk))
assert cp2.isclose((chk, 1))
assert ep.isclose((0, 1))

sp, cp1, cp2, ep = parts[1]
assert sp.isclose((0, 1))
assert cp1.isclose((-chk, 1))
assert cp2.isclose((-1, chk))
assert ep.isclose((-1, 0))

sp, cp1, cp2, ep = parts[2]
assert sp.isclose((-1, 0))
assert cp1.isclose((-1, -chk))
assert cp2.isclose((-chk, -1))
assert ep.isclose((0, -1))

sp, cp1, cp2, ep = parts[3]
assert sp.isclose((0, -1))
assert cp1.isclose((chk, -1))
assert cp2.isclose((1, -chk))
assert ep.isclose((1, 0)) ```