Coverage for pyTooling / Cartesian2D / Shapes.py: 77%
46 statements
« prev ^ index » next coverage.py v7.12.0, created at 2025-11-21 22:22 +0000
« prev ^ index » next coverage.py v7.12.0, created at 2025-11-21 22:22 +0000
1# ==================================================================================================================== #
2# _____ _ _ ____ _ _ ____ ____ #
3# _ __ _ |_ _|__ ___ | (_)_ __ __ _ / ___|__ _ _ __| |_ ___ ___(_) __ _ _ __ |___ \| _ \ #
4# | '_ \| | | || |/ _ \ / _ \| | | '_ \ / _` || | / _` | '__| __/ _ \/ __| |/ _` | '_ \ __) | | | | #
5# | |_) | |_| || | (_) | (_) | | | | | | (_| || |__| (_| | | | || __/\__ \ | (_| | | | |/ __/| |_| | #
6# | .__/ \__, ||_|\___/ \___/|_|_|_| |_|\__, (_)____\__,_|_| \__\___||___/_|\__,_|_| |_|_____|____/ #
7# |_| |___/ |___/ #
8# ==================================================================================================================== #
9# Authors: #
10# Patrick Lehmann #
11# #
12# License: #
13# ==================================================================================================================== #
14# Copyright 2025-2025 Patrick Lehmann - Bötzingen, Germany #
15# #
16# Licensed under the Apache License, Version 2.0 (the "License"); #
17# you may not use this file except in compliance with the License. #
18# You may obtain a copy of the License at #
19# #
20# http://www.apache.org/licenses/LICENSE-2.0 #
21# #
22# Unless required by applicable law or agreed to in writing, software #
23# distributed under the License is distributed on an "AS IS" BASIS, #
24# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
25# See the License for the specific language governing permissions and #
26# limitations under the License. #
27# #
28# SPDX-License-Identifier: Apache-2.0 #
29# ==================================================================================================================== #
30#
31"""An implementation of 2D cartesian shapes for Python."""
33from typing import Generic, Tuple, Optional as Nullable
35try:
36 from pyTooling.Decorators import readonly, export
37 from pyTooling.Exceptions import ToolingException
38 from pyTooling.MetaClasses import ExtendedType
39 from pyTooling.Common import getFullyQualifiedName
40 from pyTooling.Cartesian2D import Coordinate, Point2D, LineSegment2D
41except (ImportError, ModuleNotFoundError): # pragma: no cover
42 print("[pyTooling.Cartesian2D] Could not import from 'pyTooling.*'!")
44 try:
45 from Decorators import readonly, export
46 from Exceptions import ToolingException
47 from MetaClasses import ExtendedType
48 from Common import getFullyQualifiedName
49 from Cartesian2D import Coordinate, Point2D, LineSegment2D
50 except (ImportError, ModuleNotFoundError) as ex: # pragma: no cover
51 print("[pyTooling.Cartesian2D] Could not import directly!")
52 raise ex
55@export
56class Shape(Generic[Coordinate]):
57 """Base-class for all 2D cartesian shapes."""
60@export
61class Trapezium(Shape[Coordinate], Generic[Coordinate]):
62 """
63 A Trapezium is a four-sided polygon, having four edges (sides) and four corners (vertices).
64 """
65 points: Tuple[Point2D[Coordinate], ...] #: A tuple of 2D-points describing the trapezium.
66 segments: Tuple[LineSegment2D[Coordinate], ...] #: A tuple of 2D line segments describing the trapezium.
68 def __init__(self, p00: Point2D[Coordinate], p01: Point2D[Coordinate], p11: Point2D[Coordinate], p10: Point2D[Coordinate]) -> None:
69 """
70 Initializes a trapezium with 4 corners.
72 :param p00: First corner.
73 :param p01: Second corner.
74 :param p11: Third corner.
75 :param p10: Forth corner
76 """
77 if not isinstance(p00, Point2D):
78 ex = TypeError(f"Parameter 'p00' is not of type Point2D.")
79 ex.add_note(f"Got type '{getFullyQualifiedName(p00)}'.")
80 raise ex
81 if not isinstance(p01, Point2D):
82 ex = TypeError(f"Parameter 'p01' is not of type Point2D.")
83 ex.add_note(f"Got type '{getFullyQualifiedName(p01)}'.")
84 raise ex
85 if not isinstance(p11, Point2D):
86 ex = TypeError(f"Parameter 'p11' is not of type Point2D.")
87 ex.add_note(f"Got type '{getFullyQualifiedName(p11)}'.")
88 raise ex
89 if not isinstance(p10, Point2D):
90 ex = TypeError(f"Parameter 'p10' is not of type Point2D.")
91 ex.add_note(f"Got type '{getFullyQualifiedName(p10)}'.")
92 raise ex
94 self.points = (
95 _p00 := p00.Copy(),
96 _p01 := p01.Copy(),
97 _p11 := p11.Copy(),
98 _p10 := p10.Copy(),
99 )
101 self.segments = (
102 LineSegment2D(_p00, _p01, copyPoints=False),
103 LineSegment2D(_p01, _p11, copyPoints=False),
104 LineSegment2D(_p11, _p10, copyPoints=False),
105 LineSegment2D(_p10, _p00, copyPoints=False)
106 )
109@export
110class Rectangle(Trapezium[Coordinate]):
111 """
112 A rectangle is a trapezium, where opposite edges a parallel to each other and all inner angels are 90°.
113 """
115 def __init__(self, p00: Point2D[Coordinate], p01: Point2D[Coordinate], p11: Point2D[Coordinate], p10: Point2D[Coordinate]) -> None:
116 """
117 Initializes a rectangle with 4 corners.
119 :param p00: First corner.
120 :param p01: Second corner.
121 :param p11: Third corner.
122 :param p10: Forth corner
123 """
124 super().__init__(p00, p01, p11, p10)
126 if self.segments[0].Length != self.segments[2].Length or self.segments[1].Length != self.segments[3].Length:
127 raise ValueError(f"Line segments (edges) of opposite edges different lengths.")
129 if (self.segments[0].AngleTo(self.segments[1]) == 0.0 and self.segments[1].AngleTo(self.segments[2]) == 0.0
130 and self.segments[2].AngleTo(self.segments[3]) == 0.0 and self.segments[3].AngleTo(self.segments[0]) == 0.0):
131 raise ValueError(f"Line segments (edges) have no 90° angles.")
134@export
135class Square(Rectangle[Coordinate]):
136 """
137 A square is a rectangle, where all edges have the same length and all inner angels are 90°.
138 """
140 def __init__(self, p00: Point2D[Coordinate], p01: Point2D[Coordinate], p11: Point2D[Coordinate], p10: Point2D[Coordinate]) -> None:
141 """
142 Initializes a square with 4 corners.
144 :param p00: First corner.
145 :param p01: Second corner.
146 :param p11: Third corner.
147 :param p10: Forth corner
148 """
149 super().__init__(p00, p01, p11, p10)
151 if self.segments[0].Length != self.segments[1].Length:
152 raise ValueError(f"Line segments (edges) between corners have different lengths.")