From 20829e613310808118fe417036087c38b3c6e2ab Mon Sep 17 00:00:00 2001 From: Lex Neva Date: Fri, 14 Apr 2023 12:51:40 -0400 Subject: new way betterer satin algo (#2178) * significantly faster length calculation * way betterer satin algo --- lib/utils/geometry.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) (limited to 'lib/utils/geometry.py') diff --git a/lib/utils/geometry.py b/lib/utils/geometry.py index 8f34c467..7434ae27 100644 --- a/lib/utils/geometry.py +++ b/lib/utils/geometry.py @@ -4,7 +4,9 @@ # Licensed under the GNU GPL version 3.0 or later. See the file LICENSE for details. import math +import typing +import numpy from shapely.geometry import LineString, LinearRing, MultiLineString, Polygon, MultiPolygon, MultiPoint, GeometryCollection from shapely.geometry import Point as ShapelyPoint @@ -148,9 +150,9 @@ def cut_path(points, length): class Point: - def __init__(self, x: float, y: float): - self.x = x - self.y = y + def __init__(self, x: typing.Union[float, numpy.float64], y: typing.Union[float, numpy.float64]): + self.x = float(x) + self.y = float(y) @classmethod def from_shapely_point(cls, point): @@ -203,13 +205,14 @@ class Point: return "%s(%s,%s)" % (type(self), self.x, self.y) def length(self): - return math.sqrt(math.pow(self.x, 2.0) + math.pow(self.y, 2.0)) + return (self.x ** 2 + self.y ** 2) ** 0.5 def distance(self, other): return (other - self).length() def unit(self): - return self.mul(1.0 / self.length()) + length = self.length() + return self.__class__(self.x / length, self.y / length) def angle(self): return math.atan2(self.y, self.x) -- cgit v1.2.3