KTH-DD2480-Group-2 / Algorithms

A collection of algorithms and data structures
MIT License
0 stars 0 forks source link

Improve lineSegmentLineSegmentIntersection() in `LineSegmentLineSegment.java` #8

Closed LaraRos closed 3 years ago

LaraRos commented 3 years ago

Description

The function finds the point/points at which two line segments intersect each other. It has a high complexity, probably because there are many different geometry cases to take into account.

Refactoring

The are some simple refactoring that could be done with code repitition that don't reduce the complexity, which can be found here: commit: refactor plan. I then did a larger refactor, where I separated two larger cases from the main function: the case where the line sigments are colinear (on top of each other) and when there is a single unique intersection point. The complexity is reduced to 3 by doing this. It can be seen as being "too much", but personally I feel like the logic becomes more clear in this way. The final refactoring result can be found in commit: refactoring.

Tests

For the four tests, I decided to test some of the complex cases that were previously not dealt with. I tested the cases were

See commit: tests for the tests.

Data from Lizard tool:

LaraRos commented 3 years ago

Cyclomatic complexity calculation:

Cyclometric complexity = 19 - 11 + 2 = 10