package com.lonkal.convexhullapp.main;

import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.LinkedList;

public class ComputeCHButton implements ActionListener {

	private GraphPanel graphPanel;

	public ComputeCHButton(GraphPanel gp) {
		graphPanel = gp;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// default is jarvis
		jarvisMarch();
	}

	public void changeCHMethod() {
		// to change the method
	}

	public void jarvisMarch() {
		LinkedList<Point> convexHullList = new LinkedList<Point>();		
		LinkedList<Point> pointList = graphPanel.getPointList();
		
		graphPanel.addCHList(convexHullList);
		//convexHullList.add(getLeftmostPoint(pointList));
		
		Point hullPoint = getLeftmostPoint(pointList);
		Point endPoint;
		do{
			endPoint = pointList.getFirst();
			convexHullList.add(hullPoint);

			//Walk through the pointList
			for (Point p : pointList) {
				System.out.println(p);
				//graphPanel.updateStep(hullPoint, p);
				//System.out.println(Primitives.orientation(hullPoint, endPoint, p));
				if (endPoint == hullPoint || Primitives.orientation(hullPoint, endPoint, p) == 2) {
					endPoint = p;
				}
			}
			hullPoint = endPoint;

			graphPanel.repaint();
			System.out.println(convexHullList);

		} while (endPoint != convexHullList.getFirst());
		convexHullList.add(convexHullList.getFirst());
		System.out.println(convexHullList);
	}

	public Point getLeftmostPoint(LinkedList<Point> list) {
		Point leftMostPoint = list.getFirst();
		for (Point p : list) {
			if (p.x < leftMostPoint.x) {
				leftMostPoint = p;
			}
		}

		return leftMostPoint;
	}
}

@@ -159,16 +159,43 @@
 dList;%0A%0A
+import javax.swing.Timer;%0A%0A
 public c
@@ -276,17 +276,55 @@
 hPanel;%0A
+%09private ConvexHullAlgo currentAlgo;%0A%09
 %0A
-
 %09public 
@@ -571,16 +571,56 @@
 rch() %7B%0A
+%09%09Timer t = graphPanel.getTaskTimer();%0A%0A
 %09%09Linked
@@ -781,61 +781,8 @@
 t);%0A
-%09%09//convexHullList.add(getLeftmostPoint(pointList));%0A
 %09%09%0A%09
@@ -847,16 +847,32 @@
 dPoint;%0A
+%09%09%0A%09%09t.start();%0A
 %09%09do%7B%0A%09%09
@@ -1036,16 +1036,160 @@
 ;%0A%09%09%09%09//
+Send a draw event%0A%09%09%09%09//Wait for draw event + pause%0A%09%09%09%09//Comeback and keep going%0A%09%09%09%09System.out.println(Thread.currentThread().getName());%0A%09%09%09%09
 graphPan
@@ -1213,24 +1213,29 @@
 lPoint, p);%0A
+%09%09%09%09%0A
 %09%09%09%09//System
@@ -1443,16 +1443,18 @@
 nt;%0A%0A%09%09%09
+//
 graphPan
@@ -1558,16 +1558,16 @@
 rst());%0A
-
 %09%09convex
@@ -1607,16 +1607,28 @@
 rst());%0A
+%09%09t.stop();%0A
 %09%09System

clean up. and adding new design.