From b2b1413c3a93f399eed1db67ee15b69a4b7934be Mon Sep 17 00:00:00 2001 From: AlexB Date: Thu, 7 Dec 2023 12:46:14 -0500 Subject: [PATCH 1/5] Added conic construction file --- geometry/braik_mac_construction.py | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 geometry/braik_mac_construction.py diff --git a/geometry/braik_mac_construction.py b/geometry/braik_mac_construction.py new file mode 100644 index 000000000000..84f875c3a872 --- /dev/null +++ b/geometry/braik_mac_construction.py @@ -0,0 +1,10 @@ +from __future__ import annotations +import math +from dataclasses import dataclass, field +from numpy import array, linalg + +# braikenridge_maclaurin_construction +# https://mathworld.wolfram.com/ConicSection.html +# 5 Points define a conic section on a 2D normal +# orthogonal plane using this technique + From 8fdd9f2eb33711a74f7a420e440f5bb576b54e45 Mon Sep 17 00:00:00 2001 From: AlexB Date: Thu, 7 Dec 2023 14:13:58 -0500 Subject: [PATCH 2/5] Conic section code finalized for preliminary review --- geometry/braik_mac_construction.py | 146 ++++++++++++++++++++++++++++- 1 file changed, 145 insertions(+), 1 deletion(-) diff --git a/geometry/braik_mac_construction.py b/geometry/braik_mac_construction.py index 84f875c3a872..86c624bea902 100644 --- a/geometry/braik_mac_construction.py +++ b/geometry/braik_mac_construction.py @@ -1,6 +1,7 @@ from __future__ import annotations -import math + from dataclasses import dataclass, field + from numpy import array, linalg # braikenridge_maclaurin_construction @@ -8,3 +9,146 @@ # 5 Points define a conic section on a 2D normal # orthogonal plane using this technique +@dataclass +class Point: + """ + A point defined by 2 floats representing a length on a normalized + orthogonal coordinate system + default coordinate is the origin + + >>> Point(-1.0, 0.0) + Point(x=-1.0, y=0.0) + + """ + + x: float = 0.0 + y: float = 0.0 + + def __post_init__(self) -> None: + if not isinstance(self.x, (int, float)): + raise TypeError("x must be an int or float numeric value") + if not isinstance(self.y, (int, float)): + raise TypeError("y must be an int or float numeric value") + +@dataclass +class BraikMac: + """ + Given a list of 5 points, determine the corresponding + conic section equation and provide it to the user + + | x**2 xy y**2 x y 1 | + | x1**2 x1y1 y1**2 x1 y1 1 | + | x2**2 x2y2 y2**2 x2 y2 1 | = 0 + | x3**2 x3y3 y3**2 x3 y3 1 | + | x4**2 x4y4 y4**2 x4 y4 1 | + | x5**2 x5y5 y5**2 x5 y5 1 | + + >>> p1 = Point(0.0,0.0) + >>> p2 = Point(5.0,0.0) + >>> p3 = Point(2.0,3.0) + >>> p4 = Point(1.0,10.0) + >>> p5 = Point(6.0,7.0) + >>> BraikMac([p1,p2,p3,p4,p5]) # doctest: +NORMALIZE_WHITESPACE + BraikMac(p_list=[Point(x=0.0, y=0.0), Point(x=5.0, y=0.0), + Point(x=2.0, y=3.0), Point(x=1.0, y=10.0), Point(x=6.0, y=7.0)]) + """ + + p_list : list[float] = field(default_factory=list) + + def __post_init__(self) -> None: + n = 0 + for p in self.p_list: + if not isinstance(p, Point): + raise TypeError("Array must be point objects.") + n += 1 + if n != 5 : + raise TypeError("Array must be 5 point objects.") + + @property + def generate(self) -> None: + x1 = self.p_list[0].x + y1 = self.p_list[0].y + x2 = self.p_list[1].x + y2 = self.p_list[1].y + x3 = self.p_list[2].x + y3 = self.p_list[2].y + x4 = self.p_list[3].x + y4 = self.p_list[3].y + x5 = self.p_list[4].x + y5 = self.p_list[4].y + + x2_matrix = array( + [ + [x1*y1, y1**2, x1, y1, 1], + [x2*y2, y2**2, x2, y2, 1], + [x3*y3, y3**2, x3, y3, 1], + [x4*y4, y4**2, x4, y4, 1], + [x5*y5, y5**2, x5, y5, 1], + ] + ) + + a = linalg.det(x2_matrix) + + xy_matrix = array( + [ + [x1**2, y1**2, x1, y1, 1], + [x2**2, y2**2, x2, y2, 1], + [x3**2, y3**2, x3, y3, 1], + [x4**2, y4**2, x4, y4, 1], + [x5**2, y5**2, x5, y5, 1], + ] + ) + + b = -linalg.det(xy_matrix) + + y2_matrix = array( + [ + [x1**2, x1*y1, x1, y1, 1], + [x2**2, x2*y2, x2, y2, 1], + [x3**2, x3*y3, x3, y3, 1], + [x4**2, x4*y4, x4, y4, 1], + [x5**2, x5*y5, x5, y5, 1], + ] + ) + + c = linalg.det(y2_matrix) + + x_matrix = array( + [ + [x1**2, x1*y1, y1**2, y1, 1], + [x2**2, x2*y2, y2**2, y2, 1], + [x3**2, x3*y3, y3**2, y3, 1], + [x4**2, x4*y4, y4**2, y4, 1], + [x5**2, x5*y5, y5**2, y5, 1], + ] + ) + + d = -linalg.det(x_matrix) + + y_matrix = array( + [ + [x1**2, x1*y1, y1**2, x1, 1], + [x2**2, x2*y2, y2**2, x2, 1], + [x3**2, x3*y3, y3**2, x3, 1], + [x4**2, x4*y4, y4**2, x4, 1], + [x5**2, x5*y5, y5**2, x5, 1], + ] + ) + + e = linalg.det(y_matrix) + + const_matrix = array( + [ + [x1**2, x1*y1, y1**2, x1, y1], + [x2**2, x2*y2, y2**2, x2, y2], + [x3**2, x3*y3, y3**2, x3, y3], + [x4**2, x4*y4, y4**2, x4, y4], + [x5**2, x5*y5, y5**2, x5, y5], + ] + ) + + f = -linalg.det(const_matrix) + + s = (f'0 = {a:+.2} X**2 {b:+.2} XY {c:+.2} Y**2 {d:+.2} X' + f' {e:+.2} Y {f:+.2}') + print(s) From d1f8d6657d26f7a369bd017b49b994b15a221f97 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 7 Dec 2023 19:18:48 +0000 Subject: [PATCH 3/5] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- geometry/braik_mac_construction.py | 59 +++++++++++++++--------------- 1 file changed, 30 insertions(+), 29 deletions(-) diff --git a/geometry/braik_mac_construction.py b/geometry/braik_mac_construction.py index 86c624bea902..0126ef68181f 100644 --- a/geometry/braik_mac_construction.py +++ b/geometry/braik_mac_construction.py @@ -9,6 +9,7 @@ # 5 Points define a conic section on a 2D normal # orthogonal plane using this technique + @dataclass class Point: """ @@ -30,6 +31,7 @@ def __post_init__(self) -> None: if not isinstance(self.y, (int, float)): raise TypeError("y must be an int or float numeric value") + @dataclass class BraikMac: """ @@ -53,7 +55,7 @@ class BraikMac: Point(x=2.0, y=3.0), Point(x=1.0, y=10.0), Point(x=6.0, y=7.0)]) """ - p_list : list[float] = field(default_factory=list) + p_list: list[float] = field(default_factory=list) def __post_init__(self) -> None: n = 0 @@ -61,7 +63,7 @@ def __post_init__(self) -> None: if not isinstance(p, Point): raise TypeError("Array must be point objects.") n += 1 - if n != 5 : + if n != 5: raise TypeError("Array must be 5 point objects.") @property @@ -79,11 +81,11 @@ def generate(self) -> None: x2_matrix = array( [ - [x1*y1, y1**2, x1, y1, 1], - [x2*y2, y2**2, x2, y2, 1], - [x3*y3, y3**2, x3, y3, 1], - [x4*y4, y4**2, x4, y4, 1], - [x5*y5, y5**2, x5, y5, 1], + [x1 * y1, y1**2, x1, y1, 1], + [x2 * y2, y2**2, x2, y2, 1], + [x3 * y3, y3**2, x3, y3, 1], + [x4 * y4, y4**2, x4, y4, 1], + [x5 * y5, y5**2, x5, y5, 1], ] ) @@ -103,11 +105,11 @@ def generate(self) -> None: y2_matrix = array( [ - [x1**2, x1*y1, x1, y1, 1], - [x2**2, x2*y2, x2, y2, 1], - [x3**2, x3*y3, x3, y3, 1], - [x4**2, x4*y4, x4, y4, 1], - [x5**2, x5*y5, x5, y5, 1], + [x1**2, x1 * y1, x1, y1, 1], + [x2**2, x2 * y2, x2, y2, 1], + [x3**2, x3 * y3, x3, y3, 1], + [x4**2, x4 * y4, x4, y4, 1], + [x5**2, x5 * y5, x5, y5, 1], ] ) @@ -115,11 +117,11 @@ def generate(self) -> None: x_matrix = array( [ - [x1**2, x1*y1, y1**2, y1, 1], - [x2**2, x2*y2, y2**2, y2, 1], - [x3**2, x3*y3, y3**2, y3, 1], - [x4**2, x4*y4, y4**2, y4, 1], - [x5**2, x5*y5, y5**2, y5, 1], + [x1**2, x1 * y1, y1**2, y1, 1], + [x2**2, x2 * y2, y2**2, y2, 1], + [x3**2, x3 * y3, y3**2, y3, 1], + [x4**2, x4 * y4, y4**2, y4, 1], + [x5**2, x5 * y5, y5**2, y5, 1], ] ) @@ -127,11 +129,11 @@ def generate(self) -> None: y_matrix = array( [ - [x1**2, x1*y1, y1**2, x1, 1], - [x2**2, x2*y2, y2**2, x2, 1], - [x3**2, x3*y3, y3**2, x3, 1], - [x4**2, x4*y4, y4**2, x4, 1], - [x5**2, x5*y5, y5**2, x5, 1], + [x1**2, x1 * y1, y1**2, x1, 1], + [x2**2, x2 * y2, y2**2, x2, 1], + [x3**2, x3 * y3, y3**2, x3, 1], + [x4**2, x4 * y4, y4**2, x4, 1], + [x5**2, x5 * y5, y5**2, x5, 1], ] ) @@ -139,16 +141,15 @@ def generate(self) -> None: const_matrix = array( [ - [x1**2, x1*y1, y1**2, x1, y1], - [x2**2, x2*y2, y2**2, x2, y2], - [x3**2, x3*y3, y3**2, x3, y3], - [x4**2, x4*y4, y4**2, x4, y4], - [x5**2, x5*y5, y5**2, x5, y5], + [x1**2, x1 * y1, y1**2, x1, y1], + [x2**2, x2 * y2, y2**2, x2, y2], + [x3**2, x3 * y3, y3**2, x3, y3], + [x4**2, x4 * y4, y4**2, x4, y4], + [x5**2, x5 * y5, y5**2, x5, y5], ] ) f = -linalg.det(const_matrix) - s = (f'0 = {a:+.2} X**2 {b:+.2} XY {c:+.2} Y**2 {d:+.2} X' - f' {e:+.2} Y {f:+.2}') + s = f"0 = {a:+.2} X**2 {b:+.2} XY {c:+.2} Y**2 {d:+.2} X" f" {e:+.2} Y {f:+.2}" print(s) From 6b1f8e17e7769c343edd312c359c1a1bde876898 Mon Sep 17 00:00:00 2001 From: AlexB Date: Thu, 7 Dec 2023 14:24:11 -0500 Subject: [PATCH 4/5] Fixing ISC001 issue --- geometry/braik_mac_construction.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/geometry/braik_mac_construction.py b/geometry/braik_mac_construction.py index 86c624bea902..d4db867f9d8f 100644 --- a/geometry/braik_mac_construction.py +++ b/geometry/braik_mac_construction.py @@ -149,6 +149,5 @@ def generate(self) -> None: f = -linalg.det(const_matrix) - s = (f'0 = {a:+.2} X**2 {b:+.2} XY {c:+.2} Y**2 {d:+.2} X' - f' {e:+.2} Y {f:+.2}') + s = f"0 = {a:+.2} X**2 {b:+.2} XY {c:+.2} Y**2 {d:+.2} X {e:+.2} Y {f:+.2}" print(s) From c8535543866ff02cfd5c3920a1714b4c7e864e38 Mon Sep 17 00:00:00 2001 From: AlexB Date: Thu, 7 Dec 2023 14:55:00 -0500 Subject: [PATCH 5/5] Fixing variable declaration --- geometry/braik_mac_construction.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/geometry/braik_mac_construction.py b/geometry/braik_mac_construction.py index 4f29fe7e49e9..3110d9cdf69c 100644 --- a/geometry/braik_mac_construction.py +++ b/geometry/braik_mac_construction.py @@ -55,7 +55,7 @@ class BraikMac: Point(x=2.0, y=3.0), Point(x=1.0, y=10.0), Point(x=6.0, y=7.0)]) """ - p_list: list[float] = field(default_factory=list) + p_list: list[Point] = field(default_factory=list) def __post_init__(self) -> None: n = 0