return -1 if point is on left, 1 if on right - Android java.lang

Android examples for java.lang:Math Geometry

Description

return -1 if point is on left, 1 if on right

Demo Code

/*******************************************************************************
 * Copyright (c) 2011 MadRobot./*  w ww. j a va 2  s .  com*/
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License v2.1
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * 
 * Contributors:
 *  Elton Kent - initial API and implementation
 ******************************************************************************/
//package com.java2s;

import android.graphics.PointF;

public class Main {
    /**
     * return -1 if point is on left, 1 if on right
     * 
     * @param ax
     * @param ay
     * @param bx
     * @param by
     * @param px
     * @param py
     * @return -1 if the point is on the left of the line, 1 if the
     *         point is on the right of the line, 0 if the point lies
     *         on the line
     */
    public static int relativeCCW(float ax, float ay, float bx, float by,
            float px, float py) {
        bx -= ax;
        by -= ay;
        px -= ax;
        py -= ay;
        float ccw = px * by - py * bx;

        return ccw < 0.0 ? -1 : ccw > 0.0 ? 1 : 0;
    }

    /**
     * 
     * 
     * @param lineStart
     *            Line start
     * @param lineEnd
     *            Line End
     * @param point
     *            Point
     * @return
     */
    public static int relativeCCW(PointF lineStart, PointF lineEnd,
            PointF point) {
        return relativeCCW(lineStart.x, lineStart.y, lineEnd.x, lineEnd.y,
                point.x, point.y);
    }
}

Related Tutorials