Coverage for pyTooling/Cartesian2D/Shapes.py: 77%

47 statements  

« prev     ^ index     » next       coverage.py v7.11.1, created at 2025-11-07 22:21 +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.""" 

32from sys import version_info 

33 

34from typing import Generic, Tuple, Optional as Nullable 

35 

36try: 

37 from pyTooling.Decorators import readonly, export 

38 from pyTooling.Exceptions import ToolingException 

39 from pyTooling.MetaClasses import ExtendedType 

40 from pyTooling.Common import getFullyQualifiedName 

41 from pyTooling.Cartesian2D import Coordinate, Point2D, LineSegment2D 

42except (ImportError, ModuleNotFoundError): # pragma: no cover 

43 print("[pyTooling.Cartesian2D] Could not import from 'pyTooling.*'!") 

44 

45 try: 

46 from Decorators import readonly, export 

47 from Exceptions import ToolingException 

48 from MetaClasses import ExtendedType 

49 from Common import getFullyQualifiedName 

50 from Cartesian2D import Coordinate, Point2D, LineSegment2D 

51 except (ImportError, ModuleNotFoundError) as ex: # pragma: no cover 

52 print("[pyTooling.Cartesian2D] Could not import directly!") 

53 raise ex 

54 

55 

56@export 

57class Shape(Generic[Coordinate]): 

58 """Base-class for all 2D cartesian shapes.""" 

59 

60 

61@export 

62class Trapezium(Shape[Coordinate], Generic[Coordinate]): 

63 """ 

64 A Trapezium is a four-sided polygon, having four edges (sides) and four corners (vertices). 

65 """ 

66 points: Tuple[Point2D[Coordinate], ...] #: A tuple of 2D-points describing the trapezium. 

67 segments: Tuple[LineSegment2D[Coordinate], ...] #: A tuple of 2D line segments describing the trapezium. 

68 

69 def __init__(self, p00: Point2D[Coordinate], p01: Point2D[Coordinate], p11: Point2D[Coordinate], p10: Point2D[Coordinate]) -> None: 

70 """ 

71 Initializes a trapezium with 4 corners. 

72 

73 :param p00: First corner. 

74 :param p01: Second corner. 

75 :param p11: Third corner. 

76 :param p10: Forth corner 

77 """ 

78 if not isinstance(p00, Point2D): 

79 ex = TypeError(f"Parameter 'p00' is not of type Point2D.") 

80 ex.add_note(f"Got type '{getFullyQualifiedName(p00)}'.") 

81 raise ex 

82 if not isinstance(p01, Point2D): 

83 ex = TypeError(f"Parameter 'p01' is not of type Point2D.") 

84 ex.add_note(f"Got type '{getFullyQualifiedName(p01)}'.") 

85 raise ex 

86 if not isinstance(p11, Point2D): 

87 ex = TypeError(f"Parameter 'p11' is not of type Point2D.") 

88 ex.add_note(f"Got type '{getFullyQualifiedName(p11)}'.") 

89 raise ex 

90 if not isinstance(p10, Point2D): 

91 ex = TypeError(f"Parameter 'p10' is not of type Point2D.") 

92 ex.add_note(f"Got type '{getFullyQualifiedName(p10)}'.") 

93 raise ex 

94 

95 self.points = ( 

96 _p00 := p00.Copy(), 

97 _p01 := p01.Copy(), 

98 _p11 := p11.Copy(), 

99 _p10 := p10.Copy(), 

100 ) 

101 

102 self.segments = ( 

103 LineSegment2D(_p00, _p01, copyPoints=False), 

104 LineSegment2D(_p01, _p11, copyPoints=False), 

105 LineSegment2D(_p11, _p10, copyPoints=False), 

106 LineSegment2D(_p10, _p00, copyPoints=False) 

107 ) 

108 

109 

110@export 

111class Rectangle(Trapezium[Coordinate]): 

112 """ 

113 A rectangle is a trapezium, where opposite edges a parallel to each other and all inner angels are 90°. 

114 """ 

115 

116 def __init__(self, p00: Point2D[Coordinate], p01: Point2D[Coordinate], p11: Point2D[Coordinate], p10: Point2D[Coordinate]) -> None: 

117 """ 

118 Initializes a rectangle with 4 corners. 

119 

120 :param p00: First corner. 

121 :param p01: Second corner. 

122 :param p11: Third corner. 

123 :param p10: Forth corner 

124 """ 

125 super().__init__(p00, p01, p11, p10) 

126 

127 if self.segments[0].Length != self.segments[2].Length or self.segments[1].Length != self.segments[3].Length: 

128 raise ValueError(f"Line segments (edges) of opposite edges different lengths.") 

129 

130 if (self.segments[0].AngleTo(self.segments[1]) == 0.0 and self.segments[1].AngleTo(self.segments[2]) == 0.0 

131 and self.segments[2].AngleTo(self.segments[3]) == 0.0 and self.segments[3].AngleTo(self.segments[0]) == 0.0): 

132 raise ValueError(f"Line segments (edges) have no 90° angles.") 

133 

134 

135@export 

136class Square(Rectangle[Coordinate]): 

137 """ 

138 A square is a rectangle, where all edges have the same length and all inner angels are 90°. 

139 """ 

140 

141 def __init__(self, p00: Point2D[Coordinate], p01: Point2D[Coordinate], p11: Point2D[Coordinate], p10: Point2D[Coordinate]) -> None: 

142 """ 

143 Initializes a square with 4 corners. 

144 

145 :param p00: First corner. 

146 :param p01: Second corner. 

147 :param p11: Third corner. 

148 :param p10: Forth corner 

149 """ 

150 super().__init__(p00, p01, p11, p10) 

151 

152 if self.segments[0].Length != self.segments[1].Length: 

153 raise ValueError(f"Line segments (edges) between corners have different lengths.")