|
1 | 1 | import cython |
2 | | -from math import sqrt |
| 2 | +from cython.cimports.libc.math import sqrt |
| 3 | + |
| 4 | + |
| 5 | +@cython.cfunc |
| 6 | +def _min_max_distance(points: cython.list) -> cython.tuple[cython.double, cython.double]: |
| 7 | + min_distance : cython.double = 2.0 |
| 8 | + max_distance : cython.double = 0.0 |
| 9 | + for i in range(len(points)): |
| 10 | + p1 : Point = points[i] |
| 11 | + for j in range(i+1, len(points)): |
| 12 | + p2 : Point = points[j] |
| 13 | + distance = sqrt((p1._x - p2._x)*(p1._x - p2._x) + (p1._y - p2._y)*(p1._y - p2._y)) |
| 14 | + if distance < min_distance: |
| 15 | + min_distance = distance |
| 16 | + if distance > max_distance: |
| 17 | + max_distance = distance |
| 18 | + return min_distance, max_distance |
| 19 | + |
3 | 20 |
|
4 | 21 | @cython.cclass |
5 | 22 | class Point: |
6 | 23 |
|
7 | | - x: cython.float |
8 | | - y: cython.float |
| 24 | + _x: cython.double |
| 25 | + _y: cython.double |
9 | 26 |
|
10 | | - def __init__(self, x: cython.float, y: cython.float) -> None: |
11 | | - self.x = x |
12 | | - self.y = y |
| 27 | + def __init__(self, x: cython.double, y: cython.double) -> None: |
| 28 | + self._x = x |
| 29 | + self._y = y |
13 | 30 |
|
14 | | - def distance(self) -> cython.float: |
15 | | - return sqrt(self.x**2 + self.y**2) |
| 31 | + @cython.ccall |
| 32 | + def distance(self, other: Point) -> cython.double: |
| 33 | + return sqrt((self._x - other._x)*(self._x - other._x) + (self._y - other._y)*(self._y - other._y)) |
16 | 34 |
|
| 35 | + @staticmethod |
| 36 | + def min_max_distance(points: list) -> tuple[float, float]: |
| 37 | + return _min_max_distance(points) |
| 38 | + |
17 | 39 | @property |
18 | | - def x(self) -> cython.float: |
19 | | - return self.x |
| 40 | + def x(self) -> cython.double: |
| 41 | + return self._x |
20 | 42 |
|
21 | 43 | @x.setter |
22 | | - def x(self, value: cython.float) -> None: |
23 | | - self.x = float(value) |
| 44 | + def x(self, value: cython.double) -> None: |
| 45 | + self._x = float(value) |
24 | 46 |
|
25 | 47 | @property |
26 | | - def y(self) -> cython.float: |
27 | | - return self.y |
| 48 | + def y(self) -> cython.double: |
| 49 | + return self._y |
28 | 50 |
|
29 | 51 | @y.setter |
30 | | - def y(self, value: cython.float) -> None: |
31 | | - self.y = float(value) |
| 52 | + def y(self, value: cython.double) -> None: |
| 53 | + self._y = float(value) |
32 | 54 |
|
33 | 55 | def __str__(self) -> str: |
34 | 56 | return f"Point({self.x}, {self.y})" |
|
0 commit comments