Computes the polygon's normal. - Java 2D Graphics

Java examples for 2D Graphics:Polygon

Description

Computes the polygon's normal.

Demo Code

/*/*from   ww w. java 2  s .  c  o m*/
 *  Flingbox - An OpenSource physics sandbox for Google's Android
 *  Copyright (C) 2009  Jon Ander Pe?alba & Endika Guti?rrez
 *
 *  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 3 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/>.
 */
import java.util.ArrayList;

public class Main{
    /**
     * Computes the polygon's normal.  
     * 
     * @param contour Counterclockwise polygon points
     * @return Polygon's normal
     */
    public static Vector2D[] computePolygonNormals(final Vector2D[] contour) {
        final int pointsCount = contour.length;
        Vector2D[] normals = new Vector2D[pointsCount];

        for (int i = 0; i < pointsCount; i++) {
            final Vector2D p0 = contour[i], p1 = contour[i == pointsCount - 1 ? 0
                    : i];
            normals[i] = new Vector2D((p1.j - p0.j), (p0.i - p1.i));//.normalize();
        }

        return normals;
    }
}

Related Tutorials