MCPcopy Index your code
hub / github.com/OmkarPathak/pygorithm / find_intersection

Method find_intersection

pygorithm/geometry/polygon2.py:429–503  ·  view source on GitHub ↗

Find if the polygons are intersecting and how to resolve it. Distinguish between polygons that are sharing 1 point or a single line (touching) as opposed to polygons that are sharing a 2-dimensional amount of space. The resulting MTV shoul

(poly1, poly2, offset1, offset2, find_mtv = True)

Source from the content-addressed store, hash-verified

427
428 @staticmethod
429 def find_intersection(poly1, poly2, offset1, offset2, find_mtv = True):
430 """
431 Find if the polygons are intersecting and how to resolve it.
432
433 Distinguish between polygons that are sharing 1 point or a single line
434 (touching) as opposed to polygons that are sharing a 2-dimensional
435 amount of space.
436
437 The resulting MTV should be applied to the first polygon (or its offset),
438 or its negation can be applied to the second polygon (or its offset).
439
440 The MTV will be non-null if overlapping is True and find_mtv is True.
441
442 .. note::
443
444 There is only a minor performance improvement from setting find_mtv to
445 False. It is rarely an improvement to first check without finding
446 mtv and then to find the mtv.
447
448 .. caution::
449
450 The first value in the mtv could be negative (used to inverse the direction
451 of the axis)
452
453 This uses the `Seperating Axis Theorem <http://www.dyn4j.org/2010/01/sat/> to
454 calculate intersection.
455
456 :param poly1: the first polygon
457 :type poly1: :class:`pygorithm.geometry.polygon2.Polygon2`
458 :param poly2: the second polygon
459 :type poly2: :class:`pygorithm.geometry.polygon2.Polygon2`
460 :param offset1: the offset of the first polygon
461 :type offset1: :class:`pygorithm.geometry.vector2.Vector2` or None
462 :param offset2: the offset of the second polygon
463 :type offset2: :class:`pygorithm.geometry.vector2.Vector2` or None
464 :param find_mtv: if False, the mtv is always None and there is a small \
465 performance improvement
466 :type find_mtv: bool
467 :returns: (touching, overlapping, (mtv distance, mtv axis))
468 :rtype: (bool, bool, (:class:`numbers.Number`, :class:`pygorithm.geometry.vector2.Vector2`) or None)
469 """
470
471 unique_normals = list(poly1.normals)
472 for n in poly2.normals:
473 found = False
474 for old_n in poly1.normals:
475 if math.isclose(n.x, old_n.x) and math.isclose(n.y, old_n.y):
476 found = True
477 break
478 if not found:
479 unique_normals.append(n)
480
481
482 not_overlapping = False
483 best_mtv = None
484 for norm in unique_normals:
485 proj1 = Polygon2.project_onto_axis(poly1, offset1, norm)
486 proj2 = Polygon2.project_onto_axis(poly2, offset2, norm)

Callers

nothing calls this directly

Calls 1

project_onto_axisMethod · 0.45

Tested by

no test coverage detected