Here you can find the source of intersects(int b1, int e1, int b2, int e2)
public static boolean intersects(int b1, int e1, int b2, int e2)
//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; } }