/** * Insert point at the end of the list. * * @param point the point */ public void insertEnd(Point point) { PointElement newElement = new PointElement(point, null, null); if (start == null) { startNotInitialized(newElement); } else { newElement.setPrev(end); end.setNext(newElement); start.setPrev(newElement); newElement.setNext(start); end = newElement; } size++; }
/** * Check if a list contains an element. * * @param point the point * @return true, if successful */ public boolean contains(PointElement point) { if (size == 0) { System.out.println("The list is empty"); return false; } PointElement pointer = start; while (pointer.getNext() != start) { if (pointer.equals(point)) { return true; } pointer = pointer.getNext(); } return false; }
private static boolean boundingBox( PointElement a, PointElement b, PointElement c, PointElement d) { return (a.getX() == c.getX() && a.getY() == c.getY() && b.getX() == d.getX() && b.getY() == d.getY()); }
/** * Gets the element at a specific index. * * @param index the index * @return the element */ public PointElement getElement(int index) { if (index == 0) { return start; } if (index == (size - 1)) { return end; } PointElement pointer = start; for (int i = 1; i < size; i++) { if (i == index) { return pointer.getNext(); } pointer = pointer.getNext(); } return null; }
/** * Insert a point at a given index. * * @param point the point * @param index the index */ public void insertIndex(Point point, int index) { PointElement newElement = new PointElement(point, null, null); if (index == 0) { insertStart(point); return; } PointElement pointer = start; for (int i = 1; i <= size; i++) { if (i == index) { PointElement nextElement = pointer.getNext(); pointer.setNext(newElement); newElement.setPrev(pointer); newElement.setNext(nextElement); nextElement.setPrev(newElement); } pointer = pointer.getNext(); } size++; }
private static int turn(PointElement a, PointElement b, PointElement c) { double res = (b.getX() - a.getX()) * (c.getY() - a.getY()) - (b.getY() - a.getY()) * (c.getX() - a.getX()); double check = 0.1; if (res > check) { return 1; } if (res < -check) { return -1; } return 0; }
/** * This method is a help method and is invoked when the list is still empty. * * @param newElement the new element */ private void startNotInitialized(PointElement newElement) { newElement.setNext(newElement); newElement.setPrev(newElement); start = newElement; end = start; }
/** * Deletes a point from a given index. * * @param index the index * @return true, if successful */ public boolean deleteIndex(int index) { if (index == 0) { if (size == 1) { clearList(); return true; } start = start.getNext(); start.setPrev(end); end.setNext(start); size--; return true; } if (index == (size - 1)) { end = end.getPrev(); end.setNext(start); start.setPrev(end); size--; return true; } PointElement pointer = start.getNext(); for (int i = 1; i < size; i++) { if (i == index) { PointElement previousElement = pointer.getPrev(); PointElement nextElement = pointer.getNext(); previousElement.setNext(nextElement); nextElement.setPrev(previousElement); size--; return true; } pointer = pointer.getNext(); } return false; }
public static float length(PointElement a, PointElement b) { float sqrX = (a.getX() - b.getX()) * (a.getX() - b.getX()); float sqrY = (a.getY() - b.getY()) * (a.getY() - b.getY()); return (float) Math.sqrt(sqrX + sqrY); }