Пример #1
0
  /**
   * Return the point where this LineSegment intersects another line segment.
   *
   * @param other the other line segment.
   * @return the point where the two segments intersect, or null if they do not.
   */
  public PointF findIntercept(LineSegment other) {
    // Special case for vertical lines
    if (Float.isInfinite(m) && Float.isInfinite(other.m)) {
      if (start.x == other.start.x)
        return new PointF(Float.POSITIVE_INFINITY, Float.POSITIVE_INFINITY);
      return null;
    }
    // special case for horizontal lines
    if (m == 0 && other.m == 0) {
      if (start.y == other.start.y)
        return new PointF(Float.POSITIVE_INFINITY, Float.POSITIVE_INFINITY);
      return null;
    }

    float x, y;

    if (Float.isInfinite(other.m)) {
      x = other.start.x;
    } else if (Float.isInfinite(m)) {
      return other.findIntercept(this);
    } else {
      // first find intercept.
      // solve for X
      // m1x + b1 = m2x + b2
      // b1 - b2 = (m2 - m1)x

      x = (b - other.b) / (other.m - m);
    }

    // Now solve for y
    y = m * x + b;

    // now see if this point is on both line segments.
    if (isPointInSegment(x, y) && other.isPointInSegment(x, y)) {
      return new PointF(x, y);
    }

    return null;
  }