fork download
  1. // TPoint.java
  2.  
  3. package convexHull;
  4.  
  5. public class TPoint {
  6. private int x;
  7. private int y;
  8. public TPoint()
  9. {
  10. x = 0;
  11. y = 0;
  12. }
  13. public TPoint(int x, int y) {
  14. super();
  15. this.x = x;
  16. this.y = y;
  17. }
  18.  
  19. public int getX() {
  20. return x;
  21. }
  22. public void setX(int x) {
  23. this.x = x;
  24. }
  25. public int getY() {
  26. return y;
  27. }
  28. public void setY(int y) {
  29. this.y = y;
  30. }
  31.  
  32. }
  33.  
  34. // Jarvis.java
  35.  
  36. package convexHull;
  37.  
  38. import java.util.ArrayList;
  39.  
  40. public class Jarvis {
  41. public static int vect(TPoint a1,TPoint a2,TPoint b1,TPoint b2)
  42. {
  43. return (a2.getX()-a1.getX())*(b2.getY()-b1.getY())-(b2.getX()-b1.getX())*(a2.getY()-a1.getY());
  44. }
  45. public static int dist2(TPoint a1,TPoint a2)
  46. {
  47. return (a2.getX()-a1.getX())*(a2.getX()-a1.getX())+(a2.getY()-a1.getY())*(a2.getY()-a1.getY());
  48. }
  49. public static void Solve(ArrayList<TPoint> a,ArrayList<TPoint> b)
  50. {
  51. int i,j,k,m,n,min;
  52. n = a.size();
  53. m = 0;
  54. try
  55. {
  56. for(i = 1;i < n;i++)
  57. if(a.get(i).getY() < a.get(m).getY())
  58. m = i;
  59. else if(a.get(i).getY() == a.get(m).getY() && a.get(i).getX() > a.get(m).getX())
  60. m = i;
  61. b.add(a.get(m));
  62. a.set(m, a.get(0));
  63. a.set(0, b.get(0));
  64. k = 0;
  65. min = 1;
  66. do
  67. {
  68. for(j = 1;j < n;j++)
  69. if(vect(b.get(k),a.get(min),b.get(k),a.get(j)) < 0 ||
  70. vect(b.get(k),a.get(min),b.get(k),a.get(j)) == 0 &&
  71. dist2(b.get(k),a.get(min)) < dist2(b.get(k),a.get(j)))
  72. min = j;
  73. k++;
  74. b.add(a.get(min));
  75. min = 0;
  76. }
  77. while(b.get(k).getX() != b.get(0).getX() || b.get(k).getY() != b.get(0).getY());
  78. }
  79. {
  80.  
  81. }
  82. }
  83. }
  84.  
  85. // Graham.java
  86.  
  87. package convexHull;
  88.  
  89. import java.util.ArrayList;
  90.  
  91. public class Graham {
  92. public static int direction(TPoint p0,TPoint p1,TPoint p2)
  93. {
  94. return (p1.getX()-p0.getX())*(p2.getY()-p0.getY())-(p2.getX()-p0.getX())*(p1.getY()-p0.getY());
  95. }
  96. public static int distSquared(TPoint a,TPoint b)
  97. {
  98. return (a.getX() - b.getX())*(a.getX() - b.getX())+(a.getY()-b.getY())*(a.getY()-b.getY());
  99. }
  100. public static boolean comp(TPoint p0,TPoint a,TPoint b)
  101. {
  102. int d = direction(p0,a,b);
  103. if(d != 0)
  104. return d>0;
  105. else
  106. return distSquared(p0,a) > distSquared(p0,b);
  107. }
  108. public static void heapify(int l,int r,ArrayList<TPoint> A)
  109. {
  110. int i,j;
  111. TPoint x;
  112. boolean isCorrect;
  113. x = A.get(l);
  114. i = l;
  115. j = 2 * i;
  116. isCorrect = false;
  117. while(j <= r && !isCorrect)
  118. {
  119. if(j < r && comp(A.get(0),A.get(j),A.get(j+1)))
  120. j++;
  121. if(comp(A.get(0),x,A.get(j)))
  122. {
  123. A.set(i, A.get(j));
  124. i = j;
  125. j = 2 * i;
  126. }
  127. else
  128. isCorrect = true;
  129. }
  130. A.set(i, x);
  131. }
  132. public static void buildHeap(ArrayList<TPoint> A)
  133. {
  134. int n;
  135. n = A.size()-1;
  136. for(int i = n/2;i >= 1;i--)
  137. heapify(i,n,A);
  138. }
  139. public static void heapSort(ArrayList<TPoint> A)
  140. {
  141. TPoint x;
  142. int n = A.size()-1;
  143. buildHeap(A);
  144. for(int i = n;i >= 2;i--)
  145. {
  146. x = A.get(1);
  147. A.set(1, A.get(i));
  148. A.set(i, x);
  149. heapify(1,i - 1,A);
  150. }
  151. }
  152. public static int removeDup(ArrayList<TPoint> A,ArrayList<TPoint> B)
  153. {
  154. int i,prev,count,size;
  155. size = A.size();
  156. prev = 1;
  157. for(i = 1; i < size;i++)
  158. {
  159. if(direction(A.get(0),A.get(i),A.get(prev))!=0)
  160. {
  161. prev++;
  162. B.add(A.get(prev));
  163. A.set(prev, A.get(i));
  164. }
  165. }
  166. i = prev + 1;
  167. try
  168. {
  169. while(!B.isEmpty())
  170. {
  171. System.out.println(i+" "+size);
  172. A.set(i, B.get(0));
  173. B.remove(0);
  174. i++;
  175. }
  176. System.out.println();
  177. }
  178. {
  179.  
  180. }
  181. count = prev + 1;
  182. return count;
  183. }
  184. public static void Solve(ArrayList<TPoint> P,ArrayList<TPoint> S)
  185. {
  186. int j,m,n,min,top;
  187. TPoint temp;
  188. n = P.size();
  189. min = 0;
  190. for(int i = 1;i < n;i++)
  191. if(P.get(i).getY() < P.get(min).getY() || (P.get(i).getY() == P.get(min).getY() && P.get(i).getX() < P.get(min).getX()))
  192. min = i;
  193. temp = P.get(0);
  194. P.set(0, P.get(min));
  195. P.set(min, temp);
  196. heapSort(P);
  197. while(!S.isEmpty())
  198. S.remove(0);
  199. m = removeDup(P,S);
  200. while(!S.isEmpty())
  201. S.remove(0);
  202. top = -1;
  203. try
  204. {
  205. top++;
  206. S.add(top,P.get(0));
  207. top++;
  208. S.add(top,P.get(1));
  209. top++;
  210. S.add(top,P.get(2));
  211. for(int i = 3;i < m;i++)
  212. {
  213. while(direction(S.get(top-1),S.get(top),P.get(i))<=0)
  214. {
  215. S.remove(top);
  216. top--;
  217. }
  218. top++;
  219. S.add(P.get(i));
  220. }
  221. S.add(P.get(0));
  222. }
  223. {
  224.  
  225. }
  226. }
  227. }
  228.  
  229. // ConvexHull.java
  230.  
  231. package convexHull;
  232.  
  233. import java.awt.Canvas;
  234. import java.awt.Graphics;
  235. import java.awt.event.MouseEvent;
  236. import java.awt.event.MouseListener;
  237. import java.util.ArrayList;
  238.  
  239. import javax.swing.JFrame;
  240.  
  241. public class ConvexHull extends Canvas implements MouseListener{
  242. private static int countA;
  243. private static int countB;
  244. private static ArrayList<TPoint> A = new ArrayList<TPoint>();
  245. private static ArrayList<TPoint> B = new ArrayList<TPoint>();
  246. private static Graphics gDC;
  247. public ConvexHull() {
  248. // TODO Auto-generated constructor stub
  249. countA = 0;
  250. countB = 0;
  251. addMouseListener((MouseListener) this);
  252. }
  253.  
  254. public static void main(String[] args) {
  255. // TODO Auto-generated method stub
  256. JFrame jf = new JFrame("Convex Hull");
  257. ConvexHull convexHull = new ConvexHull();
  258. jf.setSize(800, 600);
  259. jf.setVisible(true);
  260. jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  261. jf.add(convexHull);
  262. }
  263.  
  264. public void paint(Graphics gDC)
  265. {
  266. for(int i = 0;i < countA;i++)
  267. gDC.fillOval(A.get(i).getX()-5, A.get(i).getY()-5, 10, 10);
  268. for(int i = 0;i < countB;i++)
  269. {
  270. gDC.drawLine(B.get(i).getX(),B.get(i).getY(), B.get((i+1)%countB).getX(),B.get((i+1)%countB).getY());
  271. System.out.println(B.get(i).getX() + " "+ B.get(i).getY());
  272. }
  273. System.out.println();
  274. }
  275.  
  276. @Override
  277. public void mouseClicked(MouseEvent e) {
  278. // TODO Auto-generated method stub
  279.  
  280. }
  281.  
  282. @Override
  283. public void mouseEntered(MouseEvent e) {
  284. // TODO Auto-generated method stub
  285.  
  286. }
  287.  
  288. @Override
  289. public void mouseExited(MouseEvent e) {
  290. // TODO Auto-generated method stub
  291.  
  292. }
  293.  
  294. @Override
  295. public void mousePressed(MouseEvent e) {
  296. // TODO Auto-generated method stub
  297. int x = e.getX();
  298. int y = e.getY();
  299. TPoint point = new TPoint(x,y);
  300. A.add(point);
  301. countA++;
  302. while(!B.isEmpty())
  303. B.remove(B.get(0));
  304. //Jarvis.Solve(A, B);
  305. Graham.Solve(A, B);
  306. countB = B.size();
  307. repaint();
  308. }
  309.  
  310. @Override
  311. public void mouseReleased(MouseEvent e) {
  312. // TODO Auto-generated method stub
  313.  
  314. }
  315.  
  316. }
  317.  
  318.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:36: error: class, interface, or enum expected
package convexHull;
^
Main.java:38: error: class, interface, or enum expected
import java.util.ArrayList;
^
Main.java:88: error: class, interface, or enum expected
package convexHull;
^
Main.java:90: error: class, interface, or enum expected
import java.util.ArrayList;
^
Main.java:234: error: class, interface, or enum expected
package convexHull;
^
Main.java:236: error: class, interface, or enum expected
import java.awt.Canvas;
^
Main.java:237: error: class, interface, or enum expected
import java.awt.Graphics;
^
Main.java:238: error: class, interface, or enum expected
import java.awt.event.MouseEvent;
^
Main.java:239: error: class, interface, or enum expected
import java.awt.event.MouseListener;
^
Main.java:240: error: class, interface, or enum expected
import java.util.ArrayList;
^
Main.java:242: error: class, interface, or enum expected
import javax.swing.JFrame;
^
11 errors
stdout
Standard output is empty