Here you can find the source of edge(Collection
Parameter | Description |
---|---|
points | the point set of the specified linked area |
public static HashSet<Point> edge(Collection<Point> points)
//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; } }