Java Array Duplicate removeDuplicatesInPath(int[][] path)

Here you can find the source of removeDuplicatesInPath(int[][] path)

Description

remove Duplicates In Path

License

Open Source License

Declaration

public static int[][] removeDuplicatesInPath(int[][] path) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.Arrays;

public class Main {
    public static int[][] removeDuplicatesInPath(int[][] path) {
        if (path.length <= 2)
            return path;

        int[][] newPath = new int[path.length][];
        int index = 0;
        newPath[0] = path[0];//from ww w  . j a v  a 2 s.c  o m
        for (int i = 1; i < path.length - 1; ++i) {
            if (isCollinear(path[i][0], path[i][1], path[i + 1][0],
                    path[i + 1][1], newPath[index][0], newPath[index][1])) {
                // skip
            } else {
                index++;
                newPath[index] = path[i];
            }
        }
        index++;
        newPath[index] = path[path.length - 1];
        return Arrays.copyOf(newPath, index + 1);
    }

    private static boolean isCollinear(int x1, int y1, int x2, int y2,
            int x3, int y3) {
        return (y3 - y1) * (x2 - x1) == (x3 - x1) * (y2 - y1);
    }
}

Related

  1. isDuplicated(String[] strArray)
  2. removeDuplicates(double[] array)
  3. removeDuplicates(final Object[] array)
  4. removeDuplicates(int[] input)
  5. removeDuplicates(T[] elements)
  6. removeDuplicateStrings(String[] array)
  7. removeDuplicateValues(double[] values)