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

Method find_intersection

pygorithm/geometry/line2.py:401–599  ·  view source on GitHub ↗

Find the intersection between the two lines. The lines may optionally be offset by a fixed amount. This will incur a minor performance penalty which is less than that of recreating new lines. Two lines are considered touching if they only

(line1, line2, offset1 = None, offset2 = None)

Source from the content-addressed store, hash-verified

399
400 @staticmethod
401 def find_intersection(line1, line2, offset1 = None, offset2 = None):
402 """
403 Find the intersection between the two lines.
404
405 The lines may optionally be offset by a fixed amount. This
406 will incur a minor performance penalty which is less than
407 that of recreating new lines.
408
409 Two lines are considered touching if they only share exactly
410 one point and that point is an edge of one of the lines.
411
412 If two lines are parallel, their intersection could be a line.
413
414 .. tip::
415
416 This will never return True, True
417
418 :param line1: the first line
419 :type line1: :class:`pygorithm.geometry.line2.Line2`
420 :param line2: the second line
421 :type line2: :class:`pygorithm.geometry.line2.Line2`
422 :param offset1: the offset of line 1
423 :type offset1: :class:`pygorithm.geometry.vector2.Vector2` or None
424 :param offset2: the offset of line 2
425 :type offset2: :class:`pygorithm.geometry.vector2.Vector2` or None
426 :returns: (touching, overlapping, intersection_location)
427 :rtype: (bool, bool, :class:`pygorithm.geometry.line2.Line2` or :class:`pygorithm.geometry.vector2.Vector2` or None)
428 """
429
430
431 # We will ensure that:
432 # - If one line is vertical and one horizontal, line1 is the vertical line
433 # - If only one line is vertical, line1 is the vertical line
434 # - If only one line is horizontal, line1 is the horizontal line
435
436 if line2.vertical and not line1.vertical:
437 return Line2.find_intersection(line2, line1, offset2, offset1)
438 if line2.horizontal and not line1.horizontal and not line1.vertical:
439 return Line2.find_intersection(line2, line1, offset2, offset1)
440
441 l1_st_x = line1.start.x + (offset1.x if offset1 is not None else 0)
442 l1_st_y = line1.start.y + (offset1.y if offset1 is not None else 0)
443 l1_en_x = line1.end.x + (offset1.x if offset1 is not None else 0)
444 l1_en_y = line1.end.y + (offset1.y if offset1 is not None else 0)
445
446 l2_st_x = line2.start.x + (offset2.x if offset2 is not None else 0)
447 l2_st_y = line2.start.y + (offset2.y if offset2 is not None else 0)
448 l2_en_x = line2.end.x + (offset2.x if offset2 is not None else 0)
449 l2_en_y = line2.end.y + (offset2.y if offset2 is not None else 0)
450
451 if line1.vertical and line2.vertical:
452 # Two vertical lines
453 if not math.isclose(l1_st_x, l2_st_x):
454 return False, False, None
455
456 aal1 = axisall.AxisAlignedLine(None, l1_st_y, l1_en_y)
457 aal2 = axisall.AxisAlignedLine(None, l2_st_y, l2_en_y)
458

Calls 3

Line2Class · 0.85
are_parallelMethod · 0.80
calculate_y_interceptMethod · 0.80