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
math
, or try the search function
.
Example #1
Source File: utils.py From cape-webservices with Apache License 2.0 | 6 votes |
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
Source File: hexagonal_grids.py From tyssue with GNU General Public License v3.0 | 6 votes |
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 radius : float, default 1. the radius of the disk """ 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) rads = radius * num_ts / num_t 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 #3
Source File: hexagonal_grids.py From tyssue with GNU General Public License v3.0 | 6 votes |
def circle(num_t, radius=1.0, phase=0.0): """Returns x and y positions of `num_t` points regularly placed around a circle of radius `radius`, shifted by `phase` radians. Parameters ---------- num_t : int the number of points around the circle radius : float, default 1. the radius of 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 #4
Source File: test_648_construction_ellipse.py From ezdxf with MIT License | 6 votes |
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
Source File: bezier4p.py From ezdxf with MIT License | 6 votes |
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. .. versionadded:: 0.13 """ 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 #6
Source File: bezier4p.py From ezdxf with MIT License | 6 votes |
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 radius: circle radius 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. .. versionadded:: 0.13 """ center = Vector(center) radius = float(radius) start_angle = math.radians(start_angle) % math.tau end_angle = math.radians(end_angle) % math.tau 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 #7
Source File: test_648_construction_ellipse.py From ezdxf with MIT License | 6 votes |
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 #8
Source File: test_602_vector.py From ezdxf with MIT License | 6 votes |
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) assert math.isclose(extrusion.angle_about(a, b), 0) 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(a.angle_between(b), c.angle_about(a, b)) assert math.isclose(extrusion.angle_about(a, b), math.pi / 2)
Example #9
Source File: test_242_random_transform.py From ezdxf with MIT License | 5 votes |
def test_random_circle_transformation(sx, sy, sz): # testing only uniform scaling, for non uniform scaling # the circle has to be converted to an ellipse vertex_count = 8 def build(): circle = Circle() vertices = list(circle.vertices(linspace(0, 360, vertex_count, endpoint=False))) m = Matrix44.chain( Matrix44.axis_rotate(axis=Vector.random(), angle=random.uniform(0, math.tau)), Matrix44.translate(dx=random.uniform(-2, 2), dy=random.uniform(-2, 2), dz=random.uniform(-2, 2)), ) return synced_transformation(circle, vertices, m) def check(circle, vertices): # Vertex(angle=0) of old_ocs is not the vertex(angle=0) of the new OCS # because of the arbitrary Axis algorithm. # Checking center point: ocs = circle.ocs() wcs_circle_center = ocs.to_wcs(circle.dxf.center) vertices_center = vertices[0].lerp(vertices[int(vertex_count / 2)]) assert wcs_circle_center.isclose(vertices_center, abs_tol=1e-9) # Check distance of vertices from circle center point: radius = circle.dxf.radius for vtx in vertices: assert math.isclose((vtx - wcs_circle_center).magnitude, radius, abs_tol=1e-9) # Check for parallel plane orientation vertices_extrusion = (vertices[0] - vertices_center).cross((vertices[1] - vertices_center)) assert vertices_extrusion.is_parallel(circle.dxf.extrusion, abs_tol=1e-9) # test transformed circle against transformed WCS vertices of the circle for _ in range(10): circle0, vertices0 = build() check(circle0, vertices0) check(*synced_scaling(circle0, vertices0, sx, sy, sz))
Example #10
Source File: pyqt_backend.py From ezdxf with MIT License | 5 votes |
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 #11
Source File: test_648_construction_ellipse.py From ezdxf with MIT License | 5 votes |
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 #12
Source File: test_648_construction_ellipse.py From ezdxf with MIT License | 5 votes |
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 = ellipse.extrusion.angle_about(ellipse.major_axis, ellipse.end_point) 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 #13
Source File: test_630_bezier4p.py From ezdxf with MIT License | 5 votes |
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))
Example #14
Source File: test_221_ellipse.py From ezdxf with MIT License | 5 votes |
def test_from_arc(): from ezdxf.entities.arc import Arc arc = Arc.new(dxfattribs={'center': (2, 2, 2), 'radius': 3}) ellipse = Ellipse.from_arc(arc) assert ellipse.dxf.center == (2, 2, 2) assert ellipse.dxf.major_axis == (3, 0, 0) assert ellipse.dxf.ratio == 1 assert ellipse.dxf.start_param == 0 assert math.isclose(ellipse.dxf.end_param, math.tau) # tests for swap_axis() are done in test_648_construction_ellipse.py # tests for params() are done in test_648_construction_ellipse.py
Example #15
Source File: ellipse.py From ezdxf with MIT License | 5 votes |
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 #16
Source File: test_242_random_transform.py From ezdxf with MIT License | 5 votes |
def test_random_ellipse_transformation(sx, sy, sz, start, end): vertex_count = 8 def build(): ellipse = Ellipse.new(dxfattribs={ 'start_param': start, 'end_param': end, }) vertices = list(ellipse.vertices(ellipse.params(vertex_count))) m = Matrix44.chain( Matrix44.axis_rotate(axis=Vector.random(), angle=random.uniform(0, math.tau)), Matrix44.translate(dx=random.uniform(-2, 2), dy=random.uniform(-2, 2), dz=random.uniform(-2, 2)), ) return synced_transformation(ellipse, vertices, m) def check(ellipse, vertices): ellipse_vertices = list(ellipse.vertices(ellipse.params(vertex_count))) # Ellipse vertices may appear in reverse order if not vertices[0].isclose(ellipse_vertices[0], abs_tol=1e-9): ellipse_vertices.reverse() for vtx, chk in zip(ellipse_vertices, vertices): assert vtx.isclose(chk, abs_tol=1e-9) for _ in range(10): ellipse0, vertices0 = build() check(ellipse0, vertices0) check(*synced_scaling(ellipse0, vertices0, sx, sy, sz))
Example #17
Source File: test_242_random_transform.py From ezdxf with MIT License | 5 votes |
def test_random_block_reference_transformation(sx, sy, sz, doc1: 'Drawing'): def insert(): return Insert.new(dxfattribs={ 'name': 'AXIS', 'insert': (0, 0, 0), 'xscale': 1, 'yscale': 1, 'zscale': 1, 'rotation': 0, 'layer': 'insert', }, doc=doc1), [Vector(0, 0, 0), X_AXIS, Y_AXIS, Z_AXIS] def check(lines, chk): origin, x, y, z = chk l1, l2, l3 = lines assert origin.isclose(l1.dxf.start) assert x.isclose(l1.dxf.end) assert origin.isclose(l2.dxf.start) assert y.isclose(l2.dxf.end) assert origin.isclose(l3.dxf.start) assert z.isclose(l3.dxf.end) entity0, vertices0 = insert() entity0, vertices0 = synced_scaling(entity0, vertices0, 1, 2, 3) m = Matrix44.chain( # Transformation order is important: scale - rotate - translate # Because scaling after rotation leads to a non orthogonal # coordinate system, which can not represented by the # INSERT entity. Matrix44.scale(sx, sy, sz), Matrix44.axis_rotate(axis=Vector.random(), angle=random.uniform(0, math.tau)), Matrix44.translate(dx=random.uniform(-2, 2), dy=random.uniform(-2, 2), dz=random.uniform(-2, 2)), ) entity, vertices = synced_transformation(entity0, vertices0, m) lines = list(entity.virtual_entities()) check(lines, vertices)
Example #18
Source File: test_414_explode_blockrefs.py From ezdxf with MIT License | 5 votes |
def test_03_explode_polyline_bulge(doc, msp): blk = doc.blocks.new('Test03') blk.add_lwpolyline([(0, 0), (3, 0, 0.5), (6, 0), (9, 0)], format='xyb') block_ref = msp.add_blockref('Test03', insert=(0, 0), dxfattribs={ 'yscale': 0.5, }) entities = list(block_ref.virtual_entities()) assert len(entities) == 3 e = entities[0] assert e.dxftype() == 'LINE' assert e.dxf.start == (0, 0) assert e.dxf.end == (3, 0) e = entities[1] e = cast(Ellipse, e) assert e.dxftype() == 'ELLIPSE' assert e.dxf.center.isclose((4.5, 0.5625, 0)) assert e.dxf.major_axis.isclose((1.875, 0.0, 0)) assert e.dxf.ratio == 0.5 assert math.isclose(e.dxf.start_param, -2.498091544796509 % math.tau) assert math.isclose(e.dxf.end_param, -0.6435011087932843 % math.tau) assert e.start_point.isclose(Vector(3, 0, 0)) assert e.end_point.isclose(Vector(6, 0, 0), abs_tol=1e-5) e = entities[2] assert e.dxftype() == 'LINE' assert e.dxf.start == (6, 0) assert e.dxf.end == (9, 0)
Example #19
Source File: createEllipse.py From qgis-shapetools-plugin with GNU General Public License v2.0 | 5 votes |
def geodesicEllipse(geod, lat, lon, sma, smi, orient, segments): segments = int(math.ceil(segments / 2)) if smi < 0.0001: smi = 0.0001 if sma < 0.0001: sma = 0.0001 if sma < smi: temp = sma sma = smi smi = temp orient += 90 ab = sma * smi step = 18.0 * smi / sma if step < 1.0: minimum = step else: minimum = 1.0 maxang = math.pi / 6 * minimum delta = ab * math.pi / segments pts = [] azi = 0 while azi < math.tau: cos_azi = math.cos(azi) sin_azi = math.sin(azi) rad = ab / math.sqrt(sma * sma * sin_azi * sin_azi + smi * smi * cos_azi * cos_azi) g = geod.Direct(lat, lon, math.degrees(azi) + orient, rad, Geodesic.LATITUDE | Geodesic.LONGITUDE) pts.append(QgsPointXY(g['lon2'], g['lat2'])) delo = delta / (rad * rad) if maxang < delo: delo = maxang azi += delo # Append the starting point to close the shape pts.append(pts[0]) return(pts)
Example #20
Source File: test_hexgrid.py From tyssue with GNU General Public License v3.0 | 5 votes |
def test_circle(): points = circle(6, 3, tau / 3) assert points.shape == (6, 2) rho = np.linalg.norm(points, axis=1) np.testing.assert_array_almost_equal(rho, np.ones(6) * 3) np.testing.assert_array_almost_equal(circle(0), [[0.0, 0.0]])
Example #21
Source File: test_math.py From android_universal with MIT License | 5 votes |
def testConstants(self): # Ref: Abramowitz & Stegun (Dover, 1965) self.ftest('pi', math.pi, 3.141592653589793238462643) self.ftest('e', math.e, 2.718281828459045235360287) self.assertEqual(math.tau, 2*math.pi)
Example #22
Source File: spline_interpolation_of_sine_wave.py From ezdxf with MIT License | 5 votes |
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 #23
Source File: RFTools.py From nanovna-saver with GNU General Public License v3.0 | 5 votes |
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 #24
Source File: scene.py From wasabi2d with GNU Lesser General Public License v3.0 | 5 votes |
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 clock.schedule_unique(self._steady_cam, 0.01)
Example #25
Source File: asset.py From jewelcraft with GNU General Public License v3.0 | 5 votes |
def girdle_coords(radius, mat): angle = tau / 64 return tuple( ( mat @ Vector( ( sin(i * angle) * radius, cos(i * angle) * radius, 0.0, ) ) ).freeze() for i in range(64) )
Example #26
Source File: transformation_workbench.py From ezdxf with MIT License | 5 votes |
def random_angle(): return random.uniform(0, math.tau)
Example #27
Source File: transformation_workbench.py From ezdxf with MIT License | 5 votes |
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 #28
Source File: spline-end-tangents-estimation.py From ezdxf with MIT License | 5 votes |
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 #29
Source File: frontend.py From ezdxf with MIT License | 5 votes |
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() radius = dxf.radius 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 #30
Source File: bspline.py From ezdxf with MIT License | 5 votes |
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 radius: circle radius 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. .. versionadded:: 0.13 """ center = Vector(center) radius = float(radius) start_angle = math.radians(start_angle) % math.tau end_angle = math.radians(end_angle) % math.tau 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, )