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

43 statements  

« prev     ^ index     » next       coverage.py v7.8.0, created at 2025-04-25 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.""" 

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 if version_info >= (3, 11): # pragma: no cover 

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

82 raise ex 

83 if not isinstance(p01, Point2D): 

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

85 if version_info >= (3, 11): # pragma: no cover 

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

87 raise ex 

88 if not isinstance(p11, Point2D): 

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

90 if version_info >= (3, 11): # pragma: no cover 

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

92 raise ex 

93 if not isinstance(p10, Point2D): 

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

95 if version_info >= (3, 11): # pragma: no cover 

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

97 raise ex 

98 

99 self.points = ( 

100 _p00 := p00.Copy(), 

101 _p01 := p01.Copy(), 

102 _p11 := p11.Copy(), 

103 _p10 := p10.Copy(), 

104 ) 

105 

106 self.segments = ( 

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

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

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

110 LineSegment2D(_p10, _p00, copyPoints=False) 

111 ) 

112 

113 

114@export 

115class Rectangle(Trapezium[Coordinate]): 

116 """ 

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

118 """ 

119 

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

121 """ 

122 Initializes a rectangle with 4 corners. 

123 

124 :param p00: First corner. 

125 :param p01: Second corner. 

126 :param p11: Third corner. 

127 :param p10: Forth corner 

128 """ 

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

130 

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

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

133 

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

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

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

137 

138 

139@export 

140class Square(Rectangle[Coordinate]): 

141 """ 

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

143 """ 

144 

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

146 """ 

147 Initializes a square with 4 corners. 

148 

149 :param p00: First corner. 

150 :param p01: Second corner. 

151 :param p11: Third corner. 

152 :param p10: Forth corner 

153 """ 

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

155 

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

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