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

41 statements  

« prev     ^ index     » next       coverage.py v7.16.2, created at 2026-10-03 23:02 +0000

1# ==================================================================================================================== # 

2# _____ _ _ ____ _ _ ____ ____ # 

3# _ __ _ |_ _|__ ___ | (_)_ __ __ _ / ___|__ _ _ __| |_ ___ ___(_) __ _ _ __ |___ \| _ \ # 

4# | '_ \| | | || |/ _ \ / _ \| | | '_ \ / _` || | / _` | '__| __/ _ \/ __| |/ _` | '_ \ __) | | | | # 

5# | |_) | |_| || | (_) | (_) | | | | | | (_| || |__| (_| | | | || __/\__ \ | (_| | | | |/ __/| |_| | # 

6# | .__/ \__, ||_|\___/ \___/|_|_|_| |_|\__, (_)____\__,_|_| \__\___||___/_|\__,_|_| |_|_____|____/ # 

7# |_| |___/ |___/ # 

8# ==================================================================================================================== # 

9# Authors: # 

10# Patrick Lehmann # 

11# # 

12# License: # 

13# ==================================================================================================================== # 

14# Copyright 2025-2026 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.""" 

32 

33from typing import Generic 

34from pyTooling.Decorators import export 

35from pyTooling.Common import getFullyQualifiedName 

36from pyTooling.Cartesian2D import Coordinate, Point2D, LineSegment2D 

37 

38 

39@export 

40class Shape(Generic[Coordinate]): 

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

42 

43 

44@export 

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

46 """ 

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

48 """ 

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

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

51 

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

53 """ 

54 Initializes a trapezium with 4 corners. 

55 

56 :param p00: First corner. 

57 :param p01: Second corner. 

58 :param p11: Third corner. 

59 :param p10: Forth corner 

60 :raises TypeError: If a given point is not of type :class:`~pyTooling.Cartesian2D.Point2D`. 

61 """ 

62 if not isinstance(p00, Point2D): 

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

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

65 raise ex 

66 

67 if not isinstance(p01, Point2D): 

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

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

70 raise ex 

71 

72 if not isinstance(p11, Point2D): 

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

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

75 raise ex 

76 

77 if not isinstance(p10, Point2D): 

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

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

80 raise ex 

81 

82 self.points = ( 

83 _p00 := p00.Copy(), 

84 _p01 := p01.Copy(), 

85 _p11 := p11.Copy(), 

86 _p10 := p10.Copy(), 

87 ) 

88 

89 self.segments = ( 

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

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

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

93 LineSegment2D(_p10, _p00, copyPoints=False) 

94 ) 

95 

96 

97@export 

98class Rectangle(Trapezium[Coordinate]): 

99 """ 

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

101 """ 

102 

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

104 """ 

105 Initializes a rectangle with 4 corners. 

106 

107 :param p00: First corner. 

108 :param p01: Second corner. 

109 :param p11: Third corner. 

110 :param p10: Forth corner 

111 :raises ValueError: If the given points don't describe a rectangle. 

112 """ 

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

114 

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

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

117 

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

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

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

121 

122 

123@export 

124class Square(Rectangle[Coordinate]): 

125 """ 

126 A square is a rectangle, where all edges have the same length and all inner angels are 90 |degree| . 

127 """ 

128 

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

130 """ 

131 Initializes a square with 4 corners. 

132 

133 :param p00: First corner. 

134 :param p01: Second corner. 

135 :param p11: Third corner. 

136 :param p10: Forth corner 

137 :raises ValueError: If the given points don't describe a square. 

138 """ 

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

140 

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

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