Java Geometry Algorithm edge(Collection points)

Here you can find the source of edge(Collection points)

Description

Calculate the edge of the specified linked area.

License

Open Source License

Parameter

Parameter Description
points the point set of the specified linked area

Return

the point set of the edge

Declaration

public static HashSet<Point> edge(Collection<Point> points) 

Method Source Code


//package com.java2s;
import java.awt.Point;

import java.util.Collection;

import java.util.HashSet;

public class Main {
    /**/* w  w  w.  j  a v a  2  s .  co  m*/
     * Calculate the edge of the specified linked area. This algorithm is
     * implemented according to Robert algorithm.
     * 
     * @param points
     *            the point set of the specified linked area
     * @return the point set of the edge
     */
    public static HashSet<Point> edge(Collection<Point> points) {
        // Clear the edge set first.
        HashSet<Point> edge = new HashSet<Point>();
        int x = 0;
        int y = 0;
        for (Point p : points) {
            if (p.x > x)
                x = p.x;
            if (p.y > y)
                y = p.y;
        }
        // x,y are used as the size of the array, therefore it should be 1
        // larger than the maximum coordinate. In the meanwhile, Robert
        // algorithm needs one more size to deal with the algorithm model,
        // therefore x,y should plus 2.
        x += 2;
        y += 2;
        boolean[][] image = new boolean[x][y];
        boolean[][] copy = image.clone();
        for (Point p : points)
            image[p.x][p.y] = true;
        for (int i = 0; i < image.length - 1; i++)
            for (int j = 0; j < image[i].length - 1; j++) {
                // Robert algorithm.
                int p5 = image[i][j] ? 1 : 0;
                int p6 = image[i][j + 1] ? 1 : 0;
                int p8 = image[i + 1][j] ? 1 : 0;
                int p9 = image[i + 1][j + 1] ? 1 : 0;
                copy[i][j] = Math.max(Math.abs(p5 - p9), Math.abs(p8 - p6)) == 1;
            }
        for (int i = 0; i < copy.length - 1; i++)
            for (int j = 0; j < copy[i].length - 1; j++)
                if (copy[i][j])
                    edge.add(new Point(i, j));
        return edge;
    }
}

Related

  1. dot(final Point2D a, final Point2D b)
  2. dot(Point a, Point b, Point c)
  3. dotNorm(final Point2D a, final Point2D b)
  4. drag(Robot robot, Point startPoint, Point endPoint, int button)
  5. dx(Point a, Point b)
  6. extendLine(Point2D p0, Point2D p1, double toLength)
  7. findArea(Point2D p1, Point2D p2, Point2D p3)
  8. findDimension(Point2D[] pts)
  9. findDistancedPoint(double t, Point2D sp, Point2D c1, Point2D c2, Point2D ep)