Skip to content

Commit

Permalink
fix bugs and updated code
Browse files Browse the repository at this point in the history
bug intersection rect2d
bug init polygon
bug size_2d is_valid float
update vector2d hash
update dist in segment2d
  • Loading branch information
mroa4 committed Feb 22, 2023
1 parent a35c9d1 commit 594a950
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 30 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "hatchling.build"

[project]
name = "pyrusgeom"
version = "0.1.1"
version = "0.1.2"
authors = [
{ name="Cyrus 2D Team", email="info@cyrus2d.com" },
]
Expand Down
6 changes: 3 additions & 3 deletions src/pyrusgeom/convex_hull.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ def compute_direct_method(self):
point_first:Vector2D = self._input_points[i]
for j in range(i + 1, point_size):
point_second:Vector2D = self._input_points[j]
rel = point_second - point_first
rel:Vector2D = point_second - point_first

valid = True
last_value = 0.0
Expand All @@ -262,7 +262,7 @@ def compute_direct_method(self):
continue

point_third = self._input_points[k]
outer_prod = rel.outerProduct(point_third - point_first)
outer_prod = rel.outer_product(point_third - point_first)

if math.fabs(outer_prod) < EPSILON:
# point is on the line
Expand All @@ -272,7 +272,7 @@ def compute_direct_method(self):
break
# to check if other_prod and last_value don't have a same
# sign means one of the points exists in the opposite side
if outer_prod ^ last_value < 0.0:
if outer_prod * last_value < 0.0: # change ^ to *
valid = False
break

Expand Down
7 changes: 5 additions & 2 deletions src/pyrusgeom/polygon_2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,12 +143,15 @@ def __init__(self, *args):
"""
super().__init__()
self._vertices:list[Vector2D] = []
if len(args) == 0:
self._vertices = [Vector2D()]
elif len(args[0]) > 0:
elif isinstance(args[0], list):
self._vertices = args[0].copy()
else:
self._vertices = args # TODO:Check this for object init change
for item in args:
if isinstance(item,Vector2D):
self._vertices.append(item.copy())

def clear(self) -> None:
"""clear all data.
Expand Down
39 changes: 19 additions & 20 deletions src/pyrusgeom/rect_2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -709,8 +709,6 @@ def intersection(self, other:Union[Line2D,Ray2D,Segment2D]) -> list[Vector2D]:
Ray2D: considered ray line.
Segment2D: considered line segment.
TODO: check this full
Returns:
list[Vector2D]: intersection Points
"""
Expand All @@ -721,18 +719,20 @@ def intersection(self, other:Union[Line2D,Ray2D,Segment2D]) -> list[Vector2D]:
right_x = self.right()
top_y = self.top()
bottom_y = self.bottom()
t_sol[n_sol] = self.left_edge().intersection(other)
if n_sol < 2 and t_sol[n_sol].is_valid() and top_y <= t_sol[n_sol].y() <= bottom_y:
t_sol[n_sol] = self.left_edge().intersection(other) # n_sol 0
if t_sol[n_sol].is_valid() and top_y <= t_sol[n_sol].y() <= bottom_y:
n_sol += 1
t_sol[n_sol] = self.right_edge().intersection(other)
t_sol[n_sol] = self.right_edge().intersection(other) # n_sol 0 1
if n_sol < 2 and t_sol[n_sol].is_valid() and top_y <= t_sol[n_sol].y() <= bottom_y:
n_sol += 1
t_sol[n_sol] = self.top_edge().intersection(other)
if n_sol < 2 and (t_sol[n_sol]).is_valid() and left_x <= t_sol[n_sol].x() <= right_x:
n_sol += 1
t_sol[n_sol] = self.top_edge().intersection(other)
if n_sol < 2 and (t_sol[n_sol]).is_valid() and left_x <= t_sol[n_sol].x() <= right_x:
n_sol += 1
if n_sol < 2:
t_sol[n_sol] = self.top_edge().intersection(other) # n_sol 0-1 2
if (t_sol[n_sol]).is_valid() and left_x <= t_sol[n_sol].x() <= right_x:
n_sol += 1
if n_sol < 2:
t_sol[n_sol] = self.bottom_edge().intersection(other) # n_sol 0-2 3
if (t_sol[n_sol]).is_valid() and left_x <= t_sol[n_sol].x() <= right_x:
n_sol += 1
if n_sol == 2 and math.fabs(t_sol[0].x() - t_sol[1].x()) < EPSILON and math.fabs(
t_sol[0].y() - t_sol[1].y()) < EPSILON:
n_sol = 1
Expand All @@ -746,21 +746,19 @@ def intersection(self, other:Union[Line2D,Ray2D,Segment2D]) -> list[Vector2D]:
if isinstance(other, Ray2D):
n_sol = self.intersection(other.line())

if len(n_sol) > 1 and not other.in_right_dir(n_sol[2], 1.0):
del n_sol[1]

if len(n_sol) > 0 and not other.in_right_dir(n_sol[1], 1.0):
n_sol[0] = n_sol[1]
if len(n_sol) > 1 and not other.in_right_dir(n_sol[1], 1.0):
del n_sol[1]

if len(n_sol) > 0 and not other.in_right_dir(n_sol[0], 1.0):
del n_sol[0]
return n_sol
if isinstance(other, Segment2D):
n_sol = self.intersection(other.line())
if len(n_sol) > 1 and not other.contains(n_sol[2]):
if len(n_sol) > 1 and not other.contains(n_sol[1]):
del n_sol[1]

if len(n_sol) > 0 and not other.contains(n_sol[1]):
n_sol[0] = n_sol[1]
del n_sol[1]
if len(n_sol) > 0 and not other.contains(n_sol[0]):
del n_sol[0]
return n_sol
raise Exception("Input must be Line/Ray/Segment")

Expand Down Expand Up @@ -865,3 +863,4 @@ def to_str(self, ostr) -> str:
"""
ostr += f'(rect {round(self.left(), 3)} {round(self.top(), 3)} \
f{round(self.right(), 3)} {round(self.bottom(), 3)})'
return ostr
2 changes: 0 additions & 2 deletions src/pyrusgeom/segment_2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,6 @@ def dist(self, other: Union[Segment2D, Vector2D]) -> float:
"""

if isinstance(other, Vector2D):
other: Vector2D = other
length = self.length()
if length == 0.0:
return self._origin.dist(other)
Expand All @@ -491,7 +490,6 @@ def dist(self, other: Union[Segment2D, Vector2D]) -> float:
self._terminal.dist2(other)))

if isinstance(other, Segment2D):
other: Segment2D = other
if self.exist_intersection(other):
return 0.0
return min(self.dist(other.origin()),
Expand Down
2 changes: 1 addition & 1 deletion src/pyrusgeom/size_2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def diagonal(self) -> float:
"""
return math.sqrt(self._length * self._length + self._width * self._width)

def is_valid(self) -> float:
def is_valid(self) -> bool:
"""check if size is valid or not.
Returns:
Expand Down
3 changes: 2 additions & 1 deletion src/pyrusgeom/vector_2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,8 @@ def set_dir(self, direction: Union[int, float, AngleDeg]) -> None:

# __ operator section __

__hash__ = None
def __hash__(self):
return hash((self._x, self._y))

def __eq__(self, other: Vector2D) -> bool:
return isinstance(other, Vector2D) and self._x == other.x() and self._y == other.y()
Expand Down

0 comments on commit 594a950

Please sign in to comment.