physdes package¶
Submodules¶
physdes.generic module¶
Generic Operations for Physical Design (srcphysdesgeneric.py)
This code defines a set of generic operations that are commonly used in physical design calculations. These operations are designed to work with different types of objects, including simple numbers (scalars) and more complex objects like intervals.
The code contains four main functions: overlap, contain, intersection, and min_dist. Each of these functions takes two inputs, typically referred to as ‘lhs’ (left-hand side) and ‘rhs’ (right-hand side), and performs a specific operation on them.
The ‘overlap’ function checks if two objects overlap or are equal. It returns True if they do overlap, and False otherwise. This function is useful for determining if two physical entities share some common space or value.
The ‘contain’ function checks if one object contains another. It returns True if the left-hand side object contains the right-hand side object, and False otherwise. This can be used to determine if one physical entity is completely within another.
The ‘intersection’ function finds the common part between two objects. If the objects are numbers, it returns the number if they’re equal. For more complex objects, it returns the overlapping region. This is useful for finding where two physical entities meet or share space.
The ‘min_dist’ function calculates the minimum Manhattan distance between two objects. For numbers, it simply calculates the absolute difference. For more complex objects, it uses a special method to determine the closest points between the objects. This can be used to find how far apart two physical entities are.
Each of these functions is designed to work with both simple numbers and more complex objects. They do this by first checking if the input objects have special methods (like ‘overlaps’, ‘contains’, ‘intersect_with’, or ‘min_dist_with’). If these methods exist, the functions use them. If not, they fall back to simpler calculations assuming the inputs are just numbers.
The code also includes many examples (in the form of doctests) to show how each function works with different types of inputs. These examples help illustrate the behavior of the functions and can be used to automatically test that the functions are working correctly.
Overall, this code provides a flexible set of tools for performing common operations in physical design calculations, capable of working with both simple numbers and more complex geometric objects.
- physdes.generic.contain(lhs, rhs) bool[source]¶
The contain function checks if one object contains another object.
- Parameters:
lhs – The lhs parameter represents the left-hand side of the comparison, while the rhs parameter represents the right-hand side of the comparison
rhs – The rhs parameter represents the right-hand side of the comparison. It can be any value or object that you want to check if it is contained within the lhs object
- Returns:
a boolean value.
Examples
>>> contain(1, 1) True >>> contain(1, 3) False >>> from physdes.interval import Interval >>> contain(Interval(1, 4), Interval(2, 3)) True >>> contain(Interval(1, 2), Interval(3, 4)) False >>> contain(Interval(1, 2), 2) True >>> contain(Interval(1, 2), 4) False >>> contain(2, Interval(2, 3)) False >>> contain(1, Interval(3, 4)) False
- physdes.generic.displacement(lhs, rhs)[source]¶
The displacement function calculates the displacement between two objects or scalars.
- Parameters:
lhs – The lhs parameter represents the left-hand side of the displacement operation. It can be either an object that has a displace method or a scalar value
rhs – The rhs parameter represents the displacement value that needs to be subtracted from the lhs parameter
- Returns:
the displacement between lhs and rhs. If lhs has a displace method, it calls that method passing rhs as an argument. Otherwise, it assumes lhs is a scalar and returns the difference between lhs and rhs.
Examples
>>> displacement(1, 1) 0 >>> displacement(1, 3) -2 >>> from physdes.interval import Interval >>> print(displacement(Interval(1, 2), Interval(2, 3))) [-1, -1] >>> print(displacement(Interval(1, 2), Interval(3, 4))) [-2, -2]
- physdes.generic.intersection(lhs, rhs)[source]¶
The intersection function returns the intersection of two objects if they have an intersect_with method, otherwise it returns the objects themselves if they are equal.
- Parameters:
lhs – The lhs parameter represents the left-hand side of the intersection operation, while the rhs parameter represents the right-hand side of the intersection operation
rhs – The rhs parameter is the second input to the intersection function. It represents the right-hand side of the intersection operation
- Returns:
the intersection of lhs and rhs.
Examples
>>> print(intersection(1, 1)) 1 >>> from physdes.interval import Interval >>> print(intersection(Interval(1, 2), Interval(2, 3))) [2, 2] >>> print(intersection(Interval(1, 2), 2)) [2, 2] >>> print(intersection(2, Interval(2, 3))) [2, 2] >>> print(intersection(1, Interval(1, 2))) [1, 1] >>> print(intersection(Interval(1, 2), Interval(1, 2))) [1, 2] >>> print(intersection(Interval(1, 2), Interval(2, 3))) [2, 2] >>> print(intersection(Interval(1, 2), 2)) [2, 2]
- physdes.generic.min_dist(lhs, rhs)[source]¶
The min_dist function calculates the minimum Manhattan distance between two objects, using their min_dist_with method if available, or by subtracting them if they are scalars.
- Parameters:
lhs – The lhs parameter represents the left-hand side value or object that we want to calculate the minimum Manhattan distance with
rhs – The parameter rhs represents the right-hand side value or object that we want to compare with the left-hand side value or object lhs
- Returns:
the minimum Manhattan distance between lhs and rhs.
Examples
>>> min_dist(1, 1) 0 >>> min_dist(1, 3) 2 >>> from physdes.interval import Interval >>> min_dist(Interval(1, 2), Interval(2, 3)) 0 >>> min_dist(Interval(1, 2), Interval(3, 4)) 1 >>> min_dist(Interval(1, 2), 2) 0 >>> min_dist(Interval(1, 2), 4) 2 >>> min_dist(2, Interval(2, 3)) 0 >>> min_dist(1, Interval(3, 4)) 2 >>> min_dist(1, Interval(1, 2)) 0 >>> min_dist(Interval(1, 2), Interval(1, 2)) 0 >>> min_dist(Interval(1, 2), Interval(2, 3)) 0 >>> min_dist(Interval(1, 2), 2) 0 >>> min_dist(2, Interval(2, 3)) 0
- physdes.generic.overlap(lhs, rhs) bool[source]¶
The overlap function checks if two objects have an overlapping property or are equal.
- Parameters:
lhs – The lhs parameter represents the left-hand side object that we want to check for overlap with the rhs parameter
rhs – The parameter rhs is the right-hand side of the comparison. It can be any object that supports the overlaps method or a scalar value
- Returns:
a boolean value.
Examples
>>> overlap(1, 1) True >>> overlap(1, 3) False >>> from physdes.interval import Interval >>> overlap(Interval(1, 2), Interval(2, 3)) True >>> overlap(Interval(1, 2), Interval(3, 4)) False >>> overlap(Interval(1, 2), 2) True >>> overlap(Interval(1, 2), 4) False >>> overlap(2, Interval(2, 3)) True >>> overlap(1, Interval(3, 4)) False >>> overlap(1, Interval(1, 2)) True
physdes.interval module¶
Interval Class
This code defines an Interval class, which represents a range of numbers with a lower bound and an upper bound. The purpose of this class is to provide a way to work with intervals of numbers, allowing various operations and comparisons to be performed on them.
The Interval class takes two inputs when creating an instance: a lower bound (lb) and an upper bound (ub). These can be either integers or floating-point numbers. The class then stores these values and provides methods to access and manipulate them.
The main outputs of this class are the results of various operations on intervals, such as checking if two intervals overlap, finding the intersection between intervals, or calculating the minimum distance between intervals.
The class achieves its purpose by implementing a variety of methods that perform calculations and comparisons on the lower and upper bounds of the intervals. For example, the overlaps method checks if two intervals have any numbers in common, while the contains method determines if a given number or interval is entirely within another interval.
Some important logic flows in this code include:
Comparison operations: The class implements methods like __lt__, __gt__, __le__, and __ge__ to compare intervals with other intervals or single numbers.
Arithmetic operations: Methods like __add__, __sub__, and __mul__ allow intervals to be added, subtracted, or multiplied by scalar values.
Set-like operations: The hull_with method finds the smallest interval that contains both the current interval and another interval or number, while intersect_with finds the overlap between two intervals.
The code also includes utility functions outside the class, such as hull and enlarge, which can work with both Interval objects and scalar values. These functions provide a more flexible way to perform operations on intervals and numbers.
Overall, this Interval class provides a comprehensive set of tools for working with ranges of numbers, which can be useful in various applications such as scheduling, resource allocation, or numerical analysis. It allows programmers to easily manipulate and compare intervals without having to manually handle the lower and upper bounds separately.
- class physdes.interval.Interval(lb: T, ub: T)[source]¶
Bases:
Generic[T]- contains(obj: Interval[T] | T) bool[source]¶
The contains function checks if an object is contained within a given interval.
- Parameters:
obj (Union["Interval[T]", T]) – The obj parameter can be either an instance of the Interval class or an integer
- Returns:
The contains method returns a boolean value indicating whether the given object is contained within the interval.
Examples
>>> a = Interval(3, 8) >>> a.contains(4) True >>> a.contains(Interval(4, 7)) True >>> a.contains(Interval(6, 9)) False
- displace(obj: Interval[T])[source]¶
The displace function takes an object as an argument and returns a new Interval object with the lower and upper bounds displaced by the corresponding bounds of the input object.
- Parameters:
obj ("Interval[T]") – The obj parameter is an object of the same class as the self object. It represents another interval that will be used to displace the current interval
- Returns:
The displace method returns an Interval object.
Examples
>>> a = Interval(3, 5) >>> print(a.displace(Interval(4, 7))) [-1, -2] >>> print(a.displace(Interval(6, 9))) [-3, -4]
- enlarge_with(alpha: T) Interval[T][source]¶
The enlarge_with function takes a value alpha and returns a new instance of the same type with the lower bound decreased by alpha and the upper bound increased by alpha.
- Parameters:
alpha (T) – The parameter “alpha” represents the amount by which the interval should be enlarged
- Returns:
The method enlarge_with returns a new instance of the same class (“Interval[T]”) with the lower bound decreased by alpha and the upper bound increased by alpha.
Examples
>>> a = Interval(3, 5) >>> print(a.enlarge_with(2)) [1, 7]
- hull_with(obj: Interval[T] | T)[source]¶
The hull_with function takes an object (either an Interval or a scalar) and returns a new Interval object that represents the hull (smallest interval that contains both intervals) of the current Interval object and the input object.
- Parameters:
obj (Union["Interval[T]", T]) – The obj parameter can be either an instance of the same class (“Interval[T]”) or a scalar value (T)
- Returns:
The method hull_with returns an Interval object.
Examples
>>> a = Interval(3, 8) >>> print(a.hull_with(Interval(4, 7))) [3, 8] >>> print(a.hull_with(Interval(6, 9))) [3, 9]
- intersect_with(obj: Interval[T] | T)[source]¶
The intersect_with function takes in an object and returns the intersection between the object and the current interval.
- Parameters:
obj (Union["Interval[T]", T]) – The obj parameter can be either an instance of the “Interval[T]” class (which is the same class as self), or it can be of type T, which is a generic type
- Returns:
The intersect_with method returns an Interval object that represents the intersection between the current Interval object (self) and the input object (obj).
Examples
>>> a = Interval(3, 8) >>> print(a.intersect_with(4)) [4, 4] >>> print(a.intersect_with(Interval(4, 7))) [4, 7] >>> print(a.intersect_with(Interval(6, 9))) [6, 8] >>> print(a.intersect_with(Interval(3, 5))) [3, 5] >>> print(a.intersect_with(Interval(5, 7))) [5, 7] >>> print(a.intersect_with(Interval(3, 6))) [3, 6] >>> print(a.intersect_with(Interval(5, 8))) [5, 8] >>> print(a.intersect_with(Interval(3, 7))) [3, 7]
- property lb: T¶
The function lb returns the lower bound of an interval.
- Returns:
The method is returning the lower bound of the interval.
Examples
>>> a = Interval(3, 4) >>> a.lb 3
- length() T[source]¶
The function returns the length of a range defined by the upper bound (ub) and lower bound (lb) attributes.
- Returns:
The length of the object, which is calculated by subtracting the upper bound (ub) from the lower bound (lb).
Examples
>>> a = Interval(3, 4) >>> a.length() 1
- min_dist_with(obj: Interval[T] | T)[source]¶
The function calculates the minimum distance between two objects.
- Parameters:
obj (Union["Interval[T]", T]) – The parameter obj can be of type “Interval[T]” or T
- Returns:
The function min_dist_with returns the minimum distance between the given object obj and the current object self.
Examples
>>> a = Interval(3, 5) >>> print(a.min_dist_with(2)) 1 >>> print(a.min_dist_with(Interval(4, 7))) 0 >>> print(a.min_dist_with(Interval(6, 9))) 1 >>> print(a.min_dist_with(Interval(3, 5))) 0 >>> print(a.min_dist_with(Interval(5, 7))) 0
- overlaps(other: Interval[T] | T) bool[source]¶
The overlaps function checks if two intervals overlap with each other.
- Parameters:
other (Union["Interval[T]", T]) – The parameter “other” is of type Union[“Interval[T]”, T], which means it can accept either an object of the same class as “self” or an object of type “T”
- Returns:
a boolean value, either True or False.
Examples
>>> a = Interval(3, 5) >>> a.overlaps(Interval(4, 9)) True >>> a.overlaps(Interval(6, 9)) False
- property ub: T¶
The function ub returns the upper bound of an interval.
- Returns:
The method is returning the upper bound of the interval.
Examples
>>> a = Interval(3, 4) >>> a.ub 4
- physdes.interval.enlarge(lhs, rhs)[source]¶
The enlarge function takes two arguments, lhs and rhs, and returns the result of enlarging lhs by rhs.
- Parameters:
lhs – The lhs parameter represents the left-hand side of the operation. It can be either an object that has a method enlarge_with, or a scalar value
rhs (T) – The parameter rhs is the value by which the lhs object will be enlarged
- Returns:
an enlarged interval or scalar value.
Examples
>>> a = Interval(3, 5) >>> print(enlarge(a, 2)) [1, 7] >>> print(enlarge(a, -1)) [4, 4]
- physdes.interval.hull(lhs, rhs)[source]¶
The hull function calculates the convex hull of two objects.
- Parameters:
lhs – The lhs parameter represents the left-hand side of the operation, while the rhs parameter represents the right-hand side of the operation
rhs – The rhs parameter is the right-hand side of the operation. It can be any value or object that supports the hull_with method
- Returns:
the hull of the input arguments.
Examples
>>> a = Interval(3, 5) >>> print(hull(a, 4)) [3, 5] >>> print(hull(a, Interval(4, 7))) [3, 7] >>> print(hull(a, Interval(6, 9))) [3, 9]
physdes.merge_obj module¶
MergeObj Class
This code defines a class called MergeObj, which represents a geometric object in a 2D space. The purpose of this class is to handle operations on points, segments, or regions that are rotated 45 degrees. It’s designed to work with different types of coordinates, such as integers, floats, or intervals.
The MergeObj class takes two inputs when creating an object: xcoord and ycoord. These represent the coordinates of the object in the rotated space. The class doesn’t produce a specific output on its own, but it provides various methods to manipulate and interact with these objects.
The class achieves its purpose by storing the coordinates in a Point object and providing methods to perform operations like translation, enlargement, intersection, and merging with other MergeObj instances. It uses a 45-degree rotated coordinate system, which allows for easier calculations in certain geometric operations.
Some important logic flows in this code include:
The constructor (init) creates a Point object with the given coordinates.
The construct method creates a MergeObj from regular x and y coordinates by rotating them 45 degrees.
The translation methods (iadd and isub) move the object by adding or subtracting vector components.
The min_dist_with method calculates the minimum rectilinear distance between two MergeObj instances.
The enlarge_with method creates a new MergeObj with enlarged coordinates.
The intersect_with method finds the intersection point between two MergeObj instances.
The merge_with method combines two MergeObj instances by enlarging them based on their distance and finding their intersection.
These operations allow for complex geometric manipulations, which can be useful in various applications such as computer graphics, game development, or computational geometry. The class provides a high-level interface for working with these rotated geometric objects, abstracting away some of the more complex mathematical calculations.
- class physdes.merge_obj.MergeObj(xcoord: T1, ycoord: T2)[source]¶
Bases:
Generic[T1,T2]Merging point, segment, or region ⛝
A 45 degree rotated point, vertical or horizontal segment, or rectangle
- static construct(xcoord: int, ycoord: int) MergeObj[int, int][source]¶
The function constructs a MergeObj object from the given x and y coordinates.
- Parameters:
- Returns:
an instance of the MergeObj class with the xcoord and ycoord values of the impl object.
Examples
>>> a = MergeObj.construct(4, 5) >>> print(a) /9, -1/
- enlarge_with(alpha: int)[source]¶
The enlarge_with function takes an integer alpha and returns a new MergeObj object with enlarged coordinates.
- Parameters:
alpha (int) – The parameter alpha is an integer that represents the factor by which the coordinates of the MergeObj object should be enlarged
- Returns:
The enlarge_with method is returning a new MergeObj object with the enlarged coordinates.
Examples
>>> a = MergeObj(4 + 5, 4 - 5) >>> r = a.enlarge_with(1) >>> print(r) /[8, 10], [-2, 0]/
- intersect_with(other)[source]¶
The function calculates the intersection point between two MergeObj objects and returns a new MergeObj object with the coordinates of the intersection point.
- Parameters:
other – The “other” parameter is an object of the same class as the current object. It represents another instance of the MergeObj class that we want to find the intersection with
- Returns:
a MergeObj object with the x-coordinate and y-coordinate of the intersection point between the self object and the other object.
Examples
>>> a = MergeObj(4 + 5, 4 - 5) >>> r = a.intersect_with(a) >>> print(r) /9, -1/
- merge_with(other)[source]¶
The merge_with function takes another object as input, calculates the minimum Manhattan distance between the two objects, enlarges the objects based on the calculated distance, finds the intersection of the enlarged objects, and returns a new object with the coordinates of the intersection.
- Parameters:
other – The “other” parameter is an object of the same class as the current object. It represents another instance of the class that we want to merge with the current instance
- Returns:
The merge_with method returns a new MergeObj object with the x-coordinate and y-coordinate of the intersection of the two objects being merged.
Examples
>>> s1 = MergeObj(200 + 600, 200 - 600) >>> s2 = MergeObj(500 + 900, 500 - 900) >>> m1 = s1.merge_with(s2) >>> print(m1) /[1100, 1100], [-700, -100]/
- min_dist_with(other) int[source]¶
The min_dist_with function calculates the minimum rectilinear distance between two objects.
- Parameters:
other – The other parameter represents another object with which you want to calculate the minimum rectilinear distance
- Returns:
the minimum rectilinear distance between the two objects.
Examples
>>> r1 = MergeObj(4 + 5, 4 - 5) >>> r2 = MergeObj(7 + 9, 7 - 9) >>> v = Vector2(5, 6) >>> r1.min_dist_with(r2) 7
physdes.point module¶
Point Class
This code defines a Point class, which represents a point in a 2D coordinate system. The purpose of this class is to provide a way to work with points, perform operations on them, and compare them with other points or geometric shapes.
The Point class takes two inputs when creating a new point: xcoord and ycoord. These represent the x and y coordinates of the point, respectively. The class is designed to be flexible, allowing these coordinates to be integers, floats, intervals, or even other Point objects (for higher-dimensional points).
The class doesn’t produce a specific output on its own, but it provides many methods that can be used to manipulate points or get information about them. For example, you can add or subtract vectors from points, check if points overlap or contain each other, find the distance between points, and more.
The Point class achieves its purpose by storing the x and y coordinates and providing a set of methods to work with these coordinates. It uses operator overloading to make it easy to perform arithmetic operations with points, such as addition and subtraction. It also includes comparison methods to determine the relative positions of points.
Some important operations in the class include:
Adding and subtracting vectors from points
Checking if points overlap or contain each other
Finding the minimum Manhattan distance between points
Creating a hull (bounding box) that contains two points
Finding the intersection of two points or shapes
Enlarging a point to create a rectangle around it
The class uses type hints and generics to make it flexible and usable with different types of coordinates. It also includes many helper methods that use functions from other modules (like generic, interval, and vector2) to perform calculations and comparisons.
Overall, this Point class provides a comprehensive set of tools for working with points in a 2D space, making it easier for programmers to handle geometric calculations and manipulations in their code.
- class physdes.point.Point(xcoord: T1, ycoord: T2)[source]¶
Bases:
Generic[T1,T2]Generic Rectilinear Point class (▪️, ──, │, or 🔲)
- contains(other) bool[source]¶
The function checks if the x and y coordinates of one object are contained within the x and y coordinates of another object.
- Parameters:
other – The “other” parameter is an object of the same class as the current object. It represents another instance of the class that we want to check if it is contained within the current instance
- Returns:
The contains method is returning a boolean value.
Examples
>>> a = Point(3, 4) >>> b = Point(5, 6) >>> print(a.contains(b)) False >>> from physdes.interval import Interval >>> r = Point(Interval(3, 4), Interval(5, 6)) # Rectangle >>> print(r.contains(a)) False
- displace(rhs: Point[T1, T2])[source]¶
The displace function takes a Vector or Point object as an argument and returns a new Vector2 object representing the displacement between the two points.
- Parameters:
rhs ("Point[T1, T2]") – The parameter rhs is of type “Point[T1, T2]”, which means it can be either a Vector2 or a Point object
- Returns:
The displace method is returning a Vector2 object.
Examples
>>> a = Point(3, 4) >>> v = Vector2(5, 6) >>> b = a - v >>> print(b) (-2, -2) >>> print(a.displace(b)) <5, 6>
- enlarge_with(alpha)[source]¶
The enlarge_with function takes a parameter alpha and returns a new instance of the same type with the x and y coordinates enlarged by alpha.
- Parameters:
alpha – The alpha parameter is a value that determines the amount by which the coordinates of the point should be enlarged
- Returns:
The enlarge_with method returns an instance of the same type as self with the enlarged coordinates.
Examples
>>> a = Point(9, -1) >>> r = a.enlarge_with(1) >>> print(r) ([8, 10], [-2, 0]) >>> r = a.enlarge_with(2) >>> print(r) ([7, 11], [-3, 1]) >>> r = a.enlarge_with(3) >>> print(r) ([6, 12], [-4, 2]) >>> r = a.enlarge_with(4) >>> print(r) ([5, 13], [-5, 3])
- flip() Point[T2, T1][source]¶
The flip function returns a new Point object with the x and y coordinates swapped.
- Returns:
The flip() method returns a new Point object with the x and y coordinates swapped.
Examples
>>> a = Point(3, 4) >>> print(a.flip()) (4, 3) >>> from physdes.interval import Interval >>> r = Point(Interval(3, 4), Interval(5, 6)) # Rectangle >>> print(r.flip()) ([5, 6], [3, 4])
- hull_with(other)[source]¶
The hull_with function takes another object and returns a new object with the hull of the x and y coordinates of both objects.
- Parameters:
other – The other parameter is an object of the same type as self. It represents another instance of the class that the hull_with method belongs to
- Returns:
an instance of the same class as self (type T). The instance is created using the hull function, which takes the x-coordinates and y-coordinates of self and other as arguments.
Examples
>>> a = Point(3, 4) >>> b = Point(5, 6) >>> print(a.hull_with(b)) ([3, 5], [4, 6]) >>> from physdes.interval import Interval >>> r = Point(Interval(3, 4), Interval(5, 6)) # Rectangle >>> print(r.hull_with(r)) ([3, 4], [5, 6])
- intersect_with(other)[source]¶
The function intersect_with takes another object as input and returns a new object that represents the intersection of the x and y coordinates of the two objects.
- Parameters:
other – The “other” parameter is an object of the same type as the current object. It represents another instance of the class that has the same attributes and methods
- Returns:
The method intersect_with returns an instance of the same class as self (i.e., type(self)). The instance is created using the T constructor and takes the intersection of the xcoord and ycoord attributes of self and other.
Examples
>>> a = Point(3, 5) >>> b = Point(4, 6) >>> print(a.intersect_with(a)) (3, 5) >>> from physdes.interval import Interval >>> r = Point(Interval(3, 4), Interval(5, 6)) # Rectangle >>> print(r.intersect_with(a)) ([3, 3], [5, 5]) >>> r = Point(Interval(3, 4), Interval(5, 6)) # Rectangle >>> print(r.intersect_with(b)) ([4, 4], [6, 6]) >>> r = Point(Interval(3, 4), Interval(5, 6)) # Rectangle >>> print(r.intersect_with(r)) ([3, 4], [5, 6])
- min_dist_with(other)[source]¶
The function calculates the minimum Manhattan distance between two points using their x and y coordinates.
- Parameters:
other – The “other” parameter represents another object or point with which you want to calculate the minimum Manhattan distance. It is assumed that both the current object (self) and the other object have attributes xcoord and ycoord, which represent their respective x and y coordinates. The function calculates the minimum Manhattan distance between the
- Returns:
the sum of the minimum distances between the x-coordinates and the y-coordinates of two objects.
Examples
>>> a = Point(3, 4) >>> b = Point(5, 6) >>> print(a.min_dist_with(b)) 4 >>> from physdes.interval import Interval >>> r = Point(Interval(3, 4), Interval(5, 6)) # Rectangle >>> print(r.min_dist_with(a)) 1 >>> r = Point(Interval(3, 4), Interval(5, 6)) # Rectangle >>> print(r.min_dist_with(b)) 1 >>> r = Point(Interval(3, 4), Interval(5, 6)) # Rectangle >>> print(r.min_dist_with(r)) 0
- overlaps(other) bool[source]¶
The overlaps function checks if two objects overlap by comparing their x and y coordinates.
- Parameters:
other – The other parameter represents another object that we want to check for overlap with the current object
- Returns:
a boolean value, indicating whether there is an overlap between the coordinates of the two objects.
Examples
>>> a = Point(3, 4) >>> b = Point(5, 6) >>> print(a.overlaps(b)) False >>> from physdes.interval import Interval >>> r = Point(Interval(3, 4), Interval(5, 6)) # Rectangle >>> print(r.overlaps(a)) False
- xcoord: T1¶
- ycoord: T2¶
physdes.polygon module¶
Polygon Module (srcphysdespolygon.py)
This code defines a Polygon class and related functions for working with polygons in a 2D space. The purpose of this module is to provide tools for creating, manipulating, and analyzing polygons.
The Polygon class represents a polygon using a list of points (vertices). It takes a list of Point objects as input when creating a new Polygon. The class provides methods for basic operations like adding or subtracting vectors from the polygon (which moves it), calculating its area, and comparing polygons for equality.
The module also includes several functions for creating special types of polygons:
create_mono_polygon: Creates a monotone polygon, which means the polygon is sorted in a specific direction.
create_ymono_polygon: Creates a y-monotone polygon, sorted based on y-coordinates.
create_xmono_polygon: Creates an x-monotone polygon, sorted based on x-coordinates.
create_test_polygon: Creates a test polygon with a specific shape for testing purposes.
One of the key functions in this module is point_in_polygon, which determines whether a given point is inside a polygon. This function takes a list of points representing the polygon and a single point to check. It returns a boolean value: True if the point is inside the polygon, and False if it’s outside.
The point_in_polygon function uses a clever algorithm called the ray-casting algorithm. It works by imagining a horizontal line (ray) extending from the point to the right. It then counts how many times this line intersects with the edges of the polygon. If the number of intersections is odd, the point is inside the polygon; if it’s even, the point is outside.
Throughout the code, there are several important data transformations happening. For example, when creating a Polygon, the input points are converted into vectors relative to the first point (the origin). This makes it easier to perform calculations and transformations on the polygon.
The module uses generic types (T) for coordinates, allowing it to work with both integer and floating-point coordinates. This flexibility makes the code more versatile and reusable in different contexts.
Overall, this module provides a comprehensive set of tools for working with polygons, from basic creation and manipulation to more complex operations like determining if a point is inside a polygon. It’s designed to be flexible and efficient, making it useful for various applications involving 2D geometry.
- class physdes.polygon.Polygon(origin, vecs)[source]¶
Bases:
Generic[T]- classmethod from_pointset(pointset: List[Point[T, T]])[source]¶
The function initializes an object with a given point set, setting the origin to the first point and creating a list of vectors by displacing each point from the origin.
- Parameters:
pointset (PointSet) – The pointset parameter is of type PointSet. It is a collection of points that represents a set of vertices. The first element of the pointset is considered as the origin point, and the remaining elements are considered as displacement vectors from the origin
Examples
>>> coords = [ ... (0, -4), ... (0, -1), ... (3, -3), ... (5, 1), ... (2, 2), ... (3, 3), ... (1, 4), ... (-2, 4), ... (-2, 2), ... (-4, 3), ... (-5, 1), ... (-6, -2), ... (-3, -3), ... (-3, -4), ... ] ... >>> S = [Point(xcoord, ycoord) for xcoord, ycoord in coords] >>> P = Polygon.from_pointset(S) >>> print(P._origin) (0, -4)
- is_anticlockwise() bool[source]¶
Check if the polygon is clockwise.
- Returns:
True if the polygon is clockwise, False otherwise.
- is_convex(is_anticlockwise=None) bool[source]¶
Check if the polygon is convex.
A polygon is convex if all its interior angles are less than or equal to 180 degrees. This can be determined by checking the cross product of consecutive edges. If all cross products have the same sign, the polygon is convex.
- Returns:
True if the polygon is convex, False otherwise.
- is_rectilinear() bool[source]¶
The is_rectilinear function checks if a polygon is rectilinear.
- Returns:
The is_rectilinear method returns a boolean value.
Examples
>>> coords = [(0, 0), (0, 1), (1, 1), (1, 0)] >>> S = [Point(x, y) for x, y in coords] >>> P = Polygon.from_pointset(S) >>> P.is_rectilinear() True >>> coords = [(0, 0), (0, 1), (1, 1), (1, 0), (0.5, 0.5)] >>> S = [Point(x, y) for x, y in coords] >>> P = Polygon.from_pointset(S) >>> P.is_rectilinear() False
- property signed_area_x2: T¶
The signed_area_x2 function calculates the signed area of a polygon multiplied by 2.
- Returns:
The signed_area_x2 method returns the signed area of the polygon multiplied by 2.
Examples
>>> coords = [ ... (0, -4), ... (0, -1), ... (3, -3), ... (5, 1), ... (2, 2), ... (3, 3), ... (1, 4), ... (-2, 4), ... (-2, 2), ... (-4, 3), ... (-5, 1), ... (-6, -2), ... (-3, -3), ... (-3, -4), ... ] ... >>> S = [Point(xcoord, ycoord) for xcoord, ycoord in coords] >>> P = Polygon.from_pointset(S) >>> P.signed_area_x2 110
- physdes.polygon.create_mono_polygon(lst: List[Point[T, T]], dir: Callable) List[Point[T, T]][source]¶
The create_mono_polygon function creates a monotone polygon for a given point set by partitioning the points based on a direction and sorting them.
- Parameters:
lst (PointSet) – A list of points representing a point set. Each point is represented as a tuple of two integers, (x, y), where x and y are the coordinates of the point
dir (Callable) – The dir parameter is a callable function that determines the direction in which the points are sorted. It takes a point as input and returns a value that represents the direction. The points are sorted based on this direction
- Returns:
The create_mono_polygon function returns a list of points representing a monotone polygon.
- Return type:
PointSet
Examples
>>> coords = [ ... (-2, 2), ... (0, -1), ... (-5, 1), ... (-2, 4), ... (0, -4), ... (-4, 3), ... (-6, -2), ... (5, 1), ... (2, 2), ... (3, -3), ... (-3, -3), ... (3, 3), ... (-3, -4), ... (1, 4), ... (-2, 4), ... (-2, 2), ... (-4, 3), ... (-5, 1), ... (-6, -2), ... (-3, -3), ... (-3, -4), ... (1, 4), ... (-2, 4), ... ] ... >>> S = [Point(xcoord, ycoord) for xcoord, ycoord in coords] >>> _ = create_mono_polygon(S, lambda pt: (pt.ycoord, pt.xcoord))
- physdes.polygon.create_test_polygon(lst: List[Point[T, T]]) List[Point[T, T]][source]¶
Create a test polygon for a given point set.
The create_test_polygon function takes a point set as input and returns a test polygon created from that point set.
- Parameters:
lst (PointSet) – The parameter lst is a PointSet, which is a collection of points. Each point in the PointSet has an xcoord and ycoord attribute, representing its coordinates
- Returns:
The function create_test_polygon returns a PointSet, which is a list of Point objects.
Examples
>>> coords = [ ... (-2, 2), ... (0, -1), ... (-5, 1), ... (-2, 4), ... (0, -4), ... (-4, 3), ... (-6, -2), ... (5, 1), ... (2, 2), ... (3, -3), ... (-3, -3), ... (3, 3), ... (-3, -4), ... (1, 4), ... ] ... >>> S = [Point(xcoord, ycoord) for xcoord, ycoord in coords] >>> S = create_test_polygon(S) >>> for p in S: ... print("{},".format(p)) ... (0, -4), (0, -1), (3, -3), (5, 1), (2, 2), (3, 3), (1, 4), (-2, 4), (-2, 2), (-4, 3), (-5, 1), (-6, -2), (-3, -3), (-3, -4),
- physdes.polygon.create_xmono_polygon(lst: List[Point[T, T]]) List[Point[T, T]][source]¶
The function creates a x-monotone polygon from a given point set.
- Parameters:
lst (PointSet) – The parameter lst is a PointSet, which is a collection of points
- Returns:
The function create_xmono_polygon returns a PointSet object.
Examples
>>> coords = [ ... (-2, 2), ... (0, -1), ... (-5, 1), ... (-2, 4), ... (0, -4), ... (-4, 3), ... (-6, -2), ... (5, 1), ... (2, 2), ... (3, -3), ... (-3, -3), ... (3, 3), ... (-3, -4), ... (1, 4), ... ] ... >>> S = [Point(xcoord, ycoord) for xcoord, ycoord in coords] >>> _ = create_xmono_polygon(S)
- physdes.polygon.create_ymono_polygon(lst: List[Point[T, T]]) List[Point[T, T]][source]¶
The function creates a y-monotone polygon from a given point set.
- Parameters:
lst (PointSet) – The parameter lst is a PointSet, which is a collection of points
- Returns:
The function create_ymono_polygon returns a PointSet object.
Examples
>>> coords = [ ... (-2, 2), ... (0, -1), ... (-5, 1), ... (-2, 4), ... (0, -4), ... (-4, 3), ... (-6, -2), ... (5, 1), ... (2, 2), ... (3, -3), ... (-3, -3), ... (3, 3), ... (-3, -4), ... (1, 4), ... ] ... >>> S = [Point(xcoord, ycoord) for xcoord, ycoord in coords] >>> _ = create_ymono_polygon(S)
- physdes.polygon.partition(pred, iterable)[source]¶
Use a predicate to partition entries into true entries and false entries
- physdes.polygon.point_in_polygon(pointset: List[Point[T, T]], ptq: Point[T, T]) bool[source]¶
The function point_in_polygon determines if a given point is within a polygon.
The code below is from Wm. Randolph Franklin <wrf@ecse.rpi.edu> (see URL below) with some minor modifications for integer. It returns true for strictly interior points, false for strictly exterior, and ub for points on the boundary. The boundary behavior is complex but determined; in particular, for a partition of a region into polygons, each Point is “in” exactly one Polygon. (See p.243 of [O’Rourke (C)] for a discussion of boundary behavior.)
See http://www.faqs.org/faqs/graphics/algorithms-faq/ Subject 2.03
- Parameters:
pointset (PointSet) – The pointset parameter is a list of points that define the vertices of a polygon. Each point in the list is an instance of the Point class, which has xcoord and ycoord attributes representing the x and y coordinates of the point
ptq (Point[T, T]) – ptq is a Point object representing the point to be checked if it is within the polygon
- Returns:
a boolean value indicating whether the given point ptq is within the polygon defined by the pointset.
Examples
>>> coords = [ ... (0, -4), ... (0, -1), ... (3, -3), ... (5, 1), ... (2, 2), ... (3, 3), ... (1, 4), ... (-2, 4), ... (-2, 2), ... (-4, 3), ... (-5, 1), ... (-6, -2), ... (-3, -3), ... (-3, -4), ... ] ... >>> pointset = [Point(xcoord, ycoord) for xcoord, ycoord in coords] >>> point_in_polygon(pointset, Point(0, 1)) True
physdes.rdllist module¶
- class physdes.rdllist.RDllIterator(node: Dllink[int])[source]¶
Bases:
objectThe RobinIterator class is an iterator that iterates over a singly linked list starting from a given node.
physdes.recti module¶
Rectangle and Segment Classes (srcphysdesrecti.py)
This code defines classes for working with rectangles and line segments in a 2D coordinate system. The main purpose is to provide a way to represent and manipulate these geometric shapes in a program.
The code introduces three main classes: Rectangle, VSegment (vertical segment), and HSegment (horizontal segment). These classes are built on top of more basic classes like Point and Interval, which are imported at the beginning of the file.
The Rectangle class represents a rectangular shape. It takes two inputs when created: an x-coordinate interval and a y-coordinate interval. These intervals define the boundaries of the rectangle. For example, you could create a rectangle from x-coordinates 3 to 4 and y-coordinates 5 to 6.
The Rectangle class provides several useful methods. You can get the lower-left and upper-right corners of the rectangle, check if a point or another rectangle is inside it, calculate its width, height, and area, and even flip it (swap its x and y coordinates).
The VSegment and HSegment classes represent vertical and horizontal line segments, respectively. A VSegment is defined by a single x-coordinate and a y-coordinate interval, while an HSegment is defined by an x-coordinate interval and a single y-coordinate.
These segment classes also have methods to check if they contain a point or another segment, and to flip themselves (turning a vertical segment into a horizontal one, or vice versa).
The code achieves its purpose by using object-oriented programming principles. Each class encapsulates the data and behavior related to its specific geometric shape. The classes inherit from a Point class, which allows them to reuse some common functionality.
An important aspect of the logic is how the classes use the Interval class to represent ranges of coordinates. This allows for easy checking of whether one range contains another, which is crucial for the “contains” methods in each class.
The code doesn’t produce any output on its own. Instead, it provides a set of tools (the classes and their methods) that a programmer can use to work with rectangles and line segments in their own programs. For example, you could use these classes to implement a simple drawing program or to solve geometric problems.
Overall, this code provides a foundation for working with basic 2D geometric shapes in a structured and object-oriented way, making it easier to perform common operations on these shapes in more complex programs.
- class physdes.recti.HSegment(xcoord: T1, ycoord: T2)[source]¶
Bases:
Point[Interval[int],int]Represents a HSegment.
- contains(other) bool[source]¶
The contains function checks if a given object is contained within another object based on their coordinates.
- Parameters:
other – The other parameter represents another object that we want to check if it is contained within the current object. It can be either a Point or a HSegment object
- Returns:
The function contains returns a boolean value indicating whether other is contained within self.
Examples
>>> a = HSegment(Interval(30, 40), 5) >>> a.contains(Point(33, 5)) True >>> a.contains(HSegment(Interval(33, 38), 5)) True >>> a.contains(HSegment(Interval(33, 38), 6)) False
- flip() VSegment[source]¶
The flip function returns a VSegment object with the y-coordinate and x-coordinate swapped.
- Returns:
The flip() method is returning a VSegment object.
Note
Overriding the flip method of the Point class.
Examples
>>> a = HSegment(Interval(30, 40), 5) >>> print(a.flip()) (5, [30, 40])
- class physdes.recti.Rectangle(xcoord: Interval, ycoord: Interval)[source]¶
Bases:
Point[Interval[int],Interval[int]]Axis-parallel Rectangle
- area() int[source]¶
The area function calculates the area of a rectangle using the lengths of its x and y coordinates.
- Returns:
The area of the rectangle, which is an integer.
Examples
>>> a = Rectangle(Interval(30, 40), Interval(50, 62)) >>> a.area() 120
- contains(other: Point) bool[source]¶
The contains function checks if a given point is contained within a rectangle.
- Parameters:
other (Point) – The other parameter can be an instance of the Point, VSegment, HSegment, or Rectangle class
- Returns:
The contains method is returning a boolean value, indicating whether the given other object is contained within the current object.
Examples
>>> a = Rectangle(Interval(30, 40), Interval(50, 60)) >>> a.contains(Point(36, 53)) True >>> a.contains(Rectangle(Interval(32, 38), Interval(51, 57))) True >>> a.contains(Rectangle(Interval(32, 38), Interval(51, 67))) False
- flip() Rectangle[source]¶
The flip function returns a new Rectangle object with the x and y coordinates swapped.
- Returns:
The flip method is returning a new Rectangle object with the x and y coordinates swapped.
Note
Overriding the flip function of the Point class. The flip function of the Point class is used to flip the x and y coordinates of a Point object. The flip function of the Rectangle class is used to flip the x and y coordinates of a Rectangle object.
Examples
>>> a = Rectangle(Interval(3, 4), Interval(5, 6)) >>> print(a.flip()) ([5, 6], [3, 4]) >>> a3d = Rectangle(a, Interval(7, 8)) # Rectangle in 3d >>> print(a3d.flip()) ([7, 8], ([3, 4], [5, 6]))
- height() int[source]¶
The height function returns the length of the y-coordinate interval of a rectangle.
- Returns:
The height of the rectangle, which is the length of the y-coordinate interval.
Examples
>>> a = Rectangle(Interval(30, 40), Interval(50, 62)) >>> a.height() 12
- property ll: Point[int, int]¶
The ll function returns the lower left point of a rectangle.
- Returns:
The ll method is returning a Point object with the lower left coordinates of the rectangle.
Examples
>>> a = Rectangle(Interval(3, 4), Interval(5, 6)) >>> print(a.ll) (3, 5)
- class physdes.recti.VSegment(xcoord: T1, ycoord: T2)[source]¶
Bases:
Point[int,Interval[int]]Represents a VSegment.
- contains(other: Point) bool[source]¶
The contains function checks if a given point is contained within a vertical segment.
- Parameters:
other (Point) – The “other” parameter is of type Point. It represents another point that we want to check if it is contained within the current point
- Returns:
a boolean value, indicating whether the given point other is contained within the current point object.
Examples
>>> a = VSegment(5, Interval(30, 40)) >>> a.contains(Point(5, 33)) True >>> a.contains(VSegment(5, Interval(33, 38))) True >>> a.contains(VSegment(6, Interval(33, 38))) False
- flip() HSegment[source]¶
The flip function returns a new HSegment object with the x and y coordinates swapped.
- Returns:
The flip() method is returning an instance of the HSegment class.
Note
Overriding the flip method of the Point class.
Examples
>>> a = VSegment(5, Interval(30, 40)) >>> print(a.flip()) ([30, 40], 5)
physdes.rpolygon module¶
RPolygon Class and Related Functions
This code defines a class called RPolygon (Rectilinear Polygon) and several related functions for working with polygons. The purpose of this code is to provide tools for creating, manipulating, and analyzing rectilinear polygons, which are polygons with sides that are either horizontal or vertical.
The main input for this code is a set of points, typically represented as a list of Point objects. Each Point object has x and y coordinates. The code can take these points and create an RPolygon object, which represents a rectilinear polygon.
The outputs of this code vary depending on which functions are used. Some functions return new polygons, while others return information about existing polygons, such as whether a point is inside the polygon or the signed area of the polygon.
The RPolygon class achieves its purpose by storing the polygon as an origin point and a list of vectors. This representation allows for efficient manipulation and analysis of the polygon. The class includes methods for comparing polygons, moving polygons, and calculating properties like the signed area.
Some important logic flows in this code include:
Creating monotone polygons (polygons that are monotone in either the x or y direction) using the create_mono_rpolygon function. This function sorts the input points and arranges them to form a valid monotone polygon.
Determining if a point is inside a polygon using the point_in_rpolygon function. This function uses a ray-casting algorithm to check if a given point is inside the polygon.
Calculating the signed area of a polygon using the signed_area method. This method uses the Shoelace formula to compute the area, which can be positive or negative depending on the orientation of the polygon.
The code also includes helper functions like partition, which is used to split a list of points based on a given condition. This is useful in creating monotone polygons and in other polygon manipulation tasks.
Overall, this code provides a set of tools for working with rectilinear polygons, allowing programmers to create, analyze, and manipulate these shapes in various ways. It’s designed to be flexible and efficient, making it useful for applications in computational geometry, computer graphics, or any field that requires working with polygonal shapes.
- class physdes.rpolygon.RPolygon(origin, vecs)[source]¶
Bases:
objectRectilinear Polygon
- classmethod from_pointset(pointset: List[Point[int, int]])[source]¶
The function initializes an object with a given point set, setting the origin to the first point and creating a list of vectors by displacing each point from the origin.
- Parameters:
pointset (PointSet) – The pointset parameter is of type PointSet. It represents a collection of points. The __init__ method is a constructor that initializes an instance of a class. In this case, it takes a PointSet as an argument and assigns the first point in the `
Examples
>>> coords = [ ... (0, -4), ... (0, -1), ... (3, -3), ... (5, 1), ... (2, 2), ... (3, 3), ... (1, 4), ... (-2, 4), ... (-2, 2), ... (-4, 3), ... (-5, 1), ... (-6, -2), ... (-3, -3), ... (-3, -4), ... ] ... >>> S = [Point(xcoord, ycoord) for xcoord, ycoord in coords] >>> P = RPolygon.from_pointset(S) >>> print(P._origin) (0, -4)
- is_anticlockwise() bool[source]¶
Check if the polygon is clockwise.
- Returns:
True if the polygon is clockwise, False otherwise.
- property signed_area: int¶
The signed_area function calculates the signed area of a polygon using the Shoelace formula.
- Returns:
The signed_area method returns an integer value, which represents the signed area of a polygon.
Examples
>>> coords = [ ... (0, -4), ... (0, -1), ... (3, -3), ... (5, 1), ... (2, 2), ... (3, 3), ... (1, 4), ... (-2, 4), ... (-2, 2), ... (-4, 3), ... (-5, 1), ... (-6, -2), ... (-3, -3), ... (-3, -4), ... ] ... >>> S = [Point(xcoord, ycoord) for xcoord, ycoord in coords] >>> P = RPolygon.from_pointset(S) >>> P.signed_area 54
- physdes.rpolygon.create_mono_rpolygon(lst: List[Point[int, int]], dir: Callable, cmp: Callable) Tuple[List[Point[int, int]], bool][source]¶
The create_mono_rpolygon function creates a monotone rectilinear polygon for a given point set, where the direction of the polygon depends on the provided direction function.
- Parameters:
lst (PointSet) – A list of points representing a point set
dir (Callable) – The dir parameter is a callable function that determines the direction in which the points are sorted. It can be either an x-first or y-first function
- Returns:
The function create_mono_rpolygon returns a tuple containing two elements: 1. PointSet: This is the list of points that make up the monotone rectilinear polygon. 2. bool: This boolean value indicates whether the polygon is clockwise or anticlockwise, depending on the dir parameter passed to the function.
Examples
>>> coords = [ ... (0, -4), ... (0, -1), ... (3, -3), ... (5, 1), ... (2, 2), ... (3, 3), ... (1, 4), ... (-2, 4), ... (-2, 2), ... (-4, 3), ... (-5, 1), ... (-6, -2), ... (-3, -3), ... (-3, -4), ... ] ... >>> S = [Point(xcoord, ycoord) for xcoord, ycoord in coords] >>> _, is_anticlockwise = create_mono_rpolygon(S, lambda pt: (pt.xcoord, pt.ycoord), lambda a, b: a < b) >>> is_anticlockwise True
- physdes.rpolygon.create_test_rpolygon(lst: List[Point[int, int]]) List[Point[int, int]][source]¶
The create_test_rpolygon function takes a list of points and returns a new list of points that form a non-crossing polygon.
- Parameters:
lst (PointSet) – The parameter lst is a PointSet, which is a collection of points. Each point in the PointSet has an x-coordinate and a y-coordinate
- Returns:
The function create_test_rpolygon returns a PointSet, which is a collection of points.
Examples
>>> coords = [ ... (-2, 2), ... (0, -1), ... (-5, 1), ... (-2, 4), ... (0, -4), ... (-4, 3), ... (-6, -2), ... (5, 1), ... (2, 2), ... (3, -3), ... (-3, -3), ... (3, 3), ... (-3, -4), ... (1, 4), ... ] ... >>> S = [Point(xcoord, ycoord) for xcoord, ycoord in coords] >>> S = create_test_rpolygon(S) >>> for p in S: ... print("{},".format(p)) ... (0, -4), (0, -1), (3, -3), (5, 1), (2, 2), (3, 3), (1, 4), (-2, 4), (-2, 2), (-4, 3), (-5, 1), (-6, -2), (-3, -3), (-3, -4),
- physdes.rpolygon.create_xmono_rpolygon(lst: List[Point[int, int]]) Tuple[List[Point[int, int]], bool][source]¶
The function creates an x-monotone rectilinear polygon for a given point set.
- Parameters:
lst (PointSet) – A point set represented as a list of points. Each point has x and y coordinates
- Returns:
The function create_xmono_rpolygon returns a tuple containing two elements: a PointSet and a boolean value is_anticlockwise <– Note!!!
Examples
>>> coords = [ ... (-2, 2), ... (0, -1), ... (-5, 1), ... (-2, 4), ... (0, -4), ... (-4, 3), ... (-6, -2), ... (5, 1), ... (2, 2), ... (3, -3), ... (-3, -3), ... (3, 3), ... (-3, -4), ... (1, 4), ... ] ... >>> S = [Point(xcoord, ycoord) for xcoord, ycoord in coords] >>> _, is_anticlockwise = create_xmono_rpolygon(S) >>> is_anticlockwise True
- physdes.rpolygon.create_ymono_rpolygon(lst: List[Point[int, int]]) Tuple[List[Point[int, int]], bool][source]¶
The function creates a y-monotone rectilinear polygon for a given point set.
- Parameters:
lst (PointSet) – A point set represented as a list of points. Each point has x and y coordinates
- Returns:
The function create_ymono_rpolygon returns a tuple containing two elements: a PointSet and a boolean value is_clockwise <– Note!!!
Examples
>>> coords = [ ... (-2, 2), ... (0, -1), ... (-5, 1), ... (-2, 4), ... (0, -4), ... (-4, 3), ... (-6, -2), ... (5, 1), ... (2, 2), ... (3, -3), ... (-3, -3), ... (3, 3), ... (-3, -4), ... (1, 4), ... ] ... >>> S = [Point(xcoord, ycoord) for xcoord, ycoord in coords] >>> _, is_clockwise = create_ymono_rpolygon(S) >>> is_clockwise False
- physdes.rpolygon.partition(pred, iterable)[source]¶
Use a predicate to partition entries into true entries and false entries
- physdes.rpolygon.point_in_rpolygon(pointset: List[Point[int, int]], ptq: Point[int, int]) bool[source]¶
The function point_in_rpolygon determines if a given point is within a given RPolygon.
The code below is from Wm. Randolph Franklin <wrf@ecse.rpi.edu> (see URL below) with some minor modifications for rectilinear. It returns true for strictly interior points, false for strictly exterior, and ub for points on the boundary. The boundary behavior is complex but determined; in particular, for a partition of a region into polygons, each Point is “in” exactly one Polygon. (See p.243 of [O’Rourke (C)] for a discussion of boundary behavior.)
See http://www.faqs.org/faqs/graphics/algorithms-faq/ Subject 2.03
- Parameters:
pointset (PointSet) – The pointset parameter is a list of points that define the vertices of the RPolygon. Each point in the list is represented as a Point object, which has xcoord and ycoord attributes representing the x and y coordinates of the point, respectively
ptq (Point[int, int]) – ptq is a Point object representing the query point. It has two attributes: xcoord and ycoord, which represent the x and y coordinates of the point, respectively
- Returns:
a boolean value indicating whether the given point ptq is within the given RPolygon defined by the pointset.
Examples
>>> coords = [ ... (0, -4), ... (0, -1), ... (3, -3), ... (5, 1), ... (2, 2), ... (3, 3), ... (1, 4), ... (-2, 4), ... (-2, 2), ... (-4, 3), ... (-5, 1), ... (-6, -2), ... (-3, -3), ... (-3, -4), ... ] ... >>> S = [Point(xcoord, ycoord) for xcoord, ycoord in coords] >>> point_in_rpolygon(S, Point(0, 1)) False
- physdes.rpolygon.rpolygon_cut_convex(lst: List[Point[int, int]], is_anticlockwise: bool) List[List[Point[int, int]]][source]¶
- physdes.rpolygon.rpolygon_cut_convex_recur(v1: Dllink[int], lst: List[Point[int, int]], is_anticlockwise: bool, rdll: RDllist) List[List[int]][source]¶
- physdes.rpolygon.rpolygon_make_convex_hull(pointset: List[Point[int, int]], is_anticlockwise: bool) List[Point[int, int]][source]¶
physdes.skeleton module¶
This is a skeleton file that can serve as a starting point for a Python
console script. To run this script uncomment the following lines in the
[options.entry_points] section in setup.cfg:
console_scripts =
fibonacci = physdes.skeleton:run
Then run pip install . (or pip install -e . for editable mode)
which will install the command fibonacci inside your current environment.
Besides console scripts, the header (i.e. until _logger…) of this file can
also be used as template for Python modules.
Note
This file can be renamed depending on your needs or safely removed if not needed.
References
- physdes.skeleton.main(args)[source]¶
Wrapper allowing
fib()to be called with string arguments in a CLI fashionInstead of returning the value from
fib(), it prints the result to thestdoutin a nicely formatted message.- Parameters:
args (List[str]) – command line parameters as list of strings (for example
["--verbose", "42"]).
- physdes.skeleton.parse_args(args)[source]¶
Parse command line parameters
- Parameters:
args (List[str]) – command line parameters as list of strings (for example
["--help"]).- Returns:
command line parameters namespace
- Return type:
physdes.vector2 module¶
Vector2 Class
This code defines a Vector2 class, which represents a two-dimensional vector in mathematics or physics. A vector is an object that has both magnitude and direction, typically represented by x and y coordinates in a 2D space.
The purpose of this code is to provide a reusable structure for working with 2D vectors, along with various operations that can be performed on them. This class can be used in applications like game development, physics simulations, or any scenario where 2D vector calculations are needed.
The Vector2 class takes two inputs when creating a new instance: x and y coordinates. These can be integers, floats, or even other Vector2 objects, allowing for flexible use in different contexts.
The class produces Vector2 objects as outputs, which can be printed, compared, or used in further calculations. It also provides methods for common vector operations like addition, subtraction, multiplication by a scalar, and division by a scalar.
To achieve its purpose, the Vector2 class uses Python’s object-oriented programming features. It defines several methods that overload standard operators (like +, -, *, and /), allowing vector objects to be manipulated intuitively. For example, you can add two vectors simply by using the + operator between them.
The class includes important logic flows for vector operations. Addition and subtraction are performed component-wise, meaning the x and y values are added or subtracted separately. Multiplication and division by a scalar apply the operation to both x and y components. There’s also a cross product method, which calculates a special type of multiplication between two vectors.
The Vector2 class also implements comparison operations, allowing vectors to be checked for equality. It provides a string representation of the vector for easy printing and debugging.
An interesting feature of this class is its use of generic types, allowing it to work with different numeric types (like integers or floats) or even nested Vector2 objects. This makes the class very flexible and usable in a wide range of scenarios.
Overall, this Vector2 class provides a comprehensive toolkit for working with 2D vectors, encapsulating the mathematical concepts and operations into an easy-to-use Python class. It’s designed to be intuitive for beginners while also offering advanced features for more complex use cases.
- class physdes.vector2.Vector2(x, y)[source]¶
Bases:
Generic[T1,T2]- cross(rhs)[source]¶
The cross function calculates the cross product of two vectors.
- Parameters:
rhs – The parameter rhs stands for “right-hand side” and represents another vector that we want to perform the cross product with
- Returns:
The cross product of the two vectors.
Examples
>>> v = Vector2(3, 4) >>> w = Vector2(5, 6) >>> v.cross(w) -2
- property x: T1¶
The function returns the x-coordinate of a vector.
- Returns:
The method x is returning the value of the attribute x_.
Examples
>>> v = Vector2(3, 4) >>> v.x 3 >>> v3d = Vector2(v, 5) # vector in 3d >>> print(v3d.x) <3, 4>
- x_: T1¶
- property y: T2¶
The function returns the y-coordinate of a vector.
- Returns:
The method y is returning the value of the y_ attribute.
Examples
>>> v = Vector2(3, 4) >>> v.y 4 >>> v3d = Vector2(v, 5) # vector in 3d >>> print(v3d.y) 5
- y_: T2¶