Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Line::intersectsLine() is buggy #96

Open
vlad-vinogradov opened this issue Sep 10, 2022 · 3 comments
Open

Line::intersectsLine() is buggy #96

vlad-vinogradov opened this issue Sep 10, 2022 · 3 comments

Comments

@vlad-vinogradov
Copy link

Hello,
Unfortunately, the Line::intersectsLine() method doesn't always work correctly. Try this test:

<?php
declare(strict_types=1);
require 'vendor/autoload.php';

use Location\Coordinate;
use Location\Line;

$line1 = new Line(
    new Coordinate(0.0, 0.0),
    new Coordinate(0.0, 2.0)
);
$line2 = new Line(
    new Coordinate(2.0, 2.0),
    new Coordinate(0.0, 10.0)
);
if ($line1->intersectsLine($line2)) {
    echo 'Line::intersectsLine() is buggy!'.PHP_EOL;
}
@mjaschen
Copy link
Owner

Added a test for this issue: master...fix/96

@nilshoerrmann It seems that there's a bug when checking for line intersection for some edge cases – do you have an idea for a fix/workaround?

@nilshoerrmann
Copy link
Contributor

I'll have a look at this over the next days.

@nilshoerrmann
Copy link
Contributor

From a quick look at the source, in intersectsLine() we have these conditions:

// the lines are collinear or touch
if (
    in_array(self::ORIENTATION_COLLINEAR, $orientation, true)
    && (new Intersection())->intersects($this, $line, false)
) {
    return true;
}

This only works if all points are collinear but fails if this is only true for three points (as in the example). So we would either have to switch to high precision for all calculations – (new Intersection())->intersects($this, $line, true) – or we would have to add a more precise condition that distinguises between three collinear points (setting precision to true) and four collinear points (settings precision to false).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants