Java Line Intersect intersects(int b1, int e1, int b2, int e2)

Here you can find the source of intersects(int b1, int e1, int b2, int e2)

Description

Returns true if one span intersects with the other

License

Apache License

Declaration

public static boolean intersects(int b1, int e1, int b2, int e2) 

Method Source Code

//package com.java2s;
/**/*www  .  jav a2 s. c o m*/
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

public class Main {
    /**
     * Returns true if one span intersects with the other
     */
    public static boolean intersects(int b1, int e1, int b2, int e2) {
        if (contains(b1, e1, b2, e2) != 0)
            return true;
        //either 1's begin is within 2 or 2's begin is within 1
        return (b1 <= b2 && b2 < e1 || b2 <= b1 && b1 < e2);
    }

    /**
     * Returns 1 if 1 contains 2
     * Returns 2 if 2 contains 1
     * Returns 0 otherwise
     */
    public static int contains(int b1, int e1, int b2, int e2) {
        if (b1 <= b2 && e1 >= e2)
            return 1;
        else if (b2 <= b1 && e2 >= e1)
            return 2;
        else
            return 0;
    }
}

Related

  1. intersectionZJU(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4)
  2. intersectionZJU(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4)
  3. intersectLines(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4, double[] point)
  4. intersectLineSegments(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4)
  5. intersectLinesWithParams(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4, double[] params)
  6. intersects(int start1, int length1, int start2, int length2)
  7. intersects(int x0, int y0, int w0, int h0, int x, int y, int w, int h)