JavaScript Coding Test Course, Checking the Intersection of Line Segments

Hello everyone! Today we will discuss one of the frequently encountered problems in JavaScript coding tests: ‘Determining if line segments intersect’. This problem is often asked in interviews and is very helpful in developing the ability to understand geometric concepts and implement them in code.

Problem Description

This problem is about determining whether two given line segments intersect. Each segment is defined by two points: segment A is represented by points A1(x1, y1) and A2(x2, y2), while segment B is represented by points B1(x3, y3) and B2(x4, y4). We need to determine whether these two segments intersect.

Input

  • A1: (x1, y1)
  • A2: (x2, y2)
  • B1: (x3, y3)
  • B2: (x4, y4)

Output

If they intersect, the output should be true; otherwise, it should return false.

Examples

Input Example

    A1: (1, 1), A2: (4, 4)
    B1: (1, 4), B2: (4, 1)
  

Output Example

    true
  

Approach to the Problem

To solve this problem, we need to utilize some geometric concepts and algorithms. We will introduce two methods to determine whether the line segments intersect. The first method uses the area of a polygon, while the second utilizes the concept of the cross product of vectors.

1. Determining intersection using the cross product

To determine if the segments intersect, we use the cross product of vectors. If the cross product of two direction vectors A and B is greater than 0, they are on one side; if less than 0, they are on the opposite side.

Equation of a line

    Let's define line segments A and B:
    A: [(x1, y1), (x2, y2)]
    B: [(x3, y3), (x4, y4)]
  

Cross product formula

The following cross product formula can be used for segments AB and AC:

    cross(A, B) = (A.x * B.y - A.y * B.x)
  

2. Determining intersection between a segment and a line

When given a segment, we can use another technique to determine if a specific line intersects the segment. This fundamentally involves reading the line defined by the endpoints of the segments and calculating the intersection.

Implementation Code

Now, let’s implement the actual code using the methods described above.


    function isIntersect(A1, A2, B1, B2) {
      const orientation = (p, q, r) => {
        const val = (q[1] - p[1]) * (r[0] - q[0]) - (q[0] - p[0]) * (r[1] - q[1]);
        if (val === 0) return 0; // collinear
        return (val > 0) ? 1 : 2; // clock or counterclockwise
      };
      
      const onSegment = (p, q, r) => {
        return (
          q[0] <= Math.max(p[0], r[0]) &&
          q[0] >= Math.min(p[0], r[0]) &&
          q[1] <= Math.max(p[1], r[1]) &&
          q[1] >= Math.min(p[1], r[1])
        );
      };
      
      const o1 = orientation(A1, A2, B1);
      const o2 = orientation(A1, A2, B2);
      const o3 = orientation(B1, B2, A1);
      const o4 = orientation(B1, B2, A2);
      
      if (o1 !== o2 && o3 !== o4) return true;
      if (o1 === 0 && onSegment(A1, B1, A2)) return true;
      if (o2 === 0 && onSegment(A1, B2, A2)) return true;
      if (o3 === 0 && onSegment(B1, A1, B2)) return true;
      if (o4 === 0 && onSegment(B1, A2, B2)) return true;

      return false;
    }

    // Example usage
    const A1 = [1, 1],
          A2 = [4, 4],
          B1 = [1, 4],
          B2 = [4, 1];

    console.log(isIntersect(A1, A2, B1, B2)); // true
  

Code Explanation

First, the orientation function determines the directionality of the three given points (p, q, r). This helps determine the intersection of segments A and B.

Next, the onSegment function checks if the point q lies on the segment pr. After confirming the intersection, further checks are performed for specific cases (when all three points coincide).

Time Complexity

The time complexity of this algorithm is O(1) since the intersection can be determined with just one comparison.

Conclusion

In this tutorial, we explored an algorithm to determine the intersection of line segments using JavaScript. I hope this helps enhance your understanding of geometric problems and coding skills. I wish you the best in your interview preparation, and I look forward to seeing you in the next lesson!