Whether the point a is inside the N-D box implied by points b2 and b2 (i.e., in 2D, whether a is inside the box with diagonal b1-b2; in 3D, whether a is inside the cube with diagonal b1-b2). - Java java.lang

Java examples for java.lang:Math Geometry Line

Description

Whether the point a is inside the N-D box implied by points b2 and b2 (i.e., in 2D, whether a is inside the box with diagonal b1-b2; in 3D, whether a is inside the cube with diagonal b1-b2).

Demo Code

/*/*from   www .  j a  va 2 s.  c  o m*/
 * #%L
 * VisBio application for visualization of multidimensional biological
 * image data.
 * %%
 * Copyright (C) 2002 - 2014 Board of Regents of the University of
 * Wisconsin-Madison.
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, either version 2 of the
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public
 * License along with this program.  If not, see
 * <http://www.gnu.org/licenses/gpl-2.0.html>.
 * #L%
 */
//package com.java2s;

public class Main {
    /**
     * Whether the point a is inside the N-D box implied by points b2 and b2
     * (i.e., in 2D, whether a is inside the box with diagonal b1-b2; in 3D,
     * whether a is inside the cube with diagonal b1-b2).
     */
    public static boolean inside(final float[] a, final float[] b1,
            final float[] b2) {
        // assumes a, b1, b2 have same lengths
        boolean between = true;
        for (int i = 0; i < a.length; i++) {
            final boolean flip = b1[i] < b2[i] ? false : true;
            final float lo = flip ? b2[i] : b1[i];
            final float hi = flip ? b1[i] : b2[i];
            if (a[i] < lo || a[i] > hi) {
                between = false;
                break;
            }
        }
        return between;
    }
}

Related Tutorials