Advertisement
Guest User

Daily Programmer 174-Hard YuriKahn v2.1

a guest
Aug 8th, 2014
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.71 KB | None | 0 0
  1. package hull;
  2.  
  3. import java.awt.BasicStroke;
  4. import java.awt.Color;
  5. import java.awt.Dimension;
  6. import java.awt.Graphics;
  7. import java.awt.Graphics2D;
  8. import java.awt.RenderingHints;
  9. import java.awt.event.MouseAdapter;
  10. import java.awt.event.MouseEvent;
  11. import java.awt.event.MouseMotionAdapter;
  12. import java.awt.geom.Ellipse2D;
  13. import java.awt.geom.Line2D;
  14. import java.io.BufferedReader;
  15. import java.io.File;
  16. import java.io.FileReader;
  17. import java.io.IOException;
  18. import java.util.Arrays;
  19. import java.util.StringTokenizer;
  20.  
  21. import javax.swing.JFrame;
  22. import javax.swing.JPanel;
  23.  
  24.  
  25. @SuppressWarnings("serial")
  26. public class ConvexHull extends JPanel {
  27.  
  28.     static class Point implements Comparable {
  29.         public double x;
  30.         public double y;
  31.         public char val;
  32.         public boolean selected = false;
  33.        
  34.         public Point(char val, double x, double y) {
  35.             this.x = x;
  36.             this.y = y;
  37.             this.val = val;
  38.         }
  39.  
  40.         @Override
  41.         public int compareTo(Object o) {
  42.             Point p2 = (Point) o;
  43.             if(p2.y>y) {
  44.                 return 1;
  45.             }
  46.             if(p2.y<y) {
  47.                 return -1;
  48.             }
  49.             return 0;
  50.         }
  51.     }
  52.    
  53.     public static ConvexHull instance;
  54.    
  55.     double xScale = 50;
  56.     double yScale = 50;
  57.     public Point[] hull;
  58.     public Point[] all;
  59.     public int maxX;
  60.     public int maxY;
  61.     public boolean somethingSelected = false;
  62.     public boolean hasDragged = false;
  63.    
  64.     public void handleMousePress(int mouseX, int mouseY) {
  65.         System.out.println("press");
  66.         int foundPoint = -1;
  67.         for(int i=0; i<all.length; i++) {
  68.             if(Math.abs(all[i].x*xScale+20-mouseX) < 4 && Math.abs(all[i].y*yScale+20-mouseY) < 4) {
  69.                 foundPoint = i;
  70.                 if(!somethingSelected) {
  71.                     all[i].selected = true;
  72.                     somethingSelected = true;
  73.                     System.out.println("new selection");
  74.                     this.repaint();
  75.                     return;
  76.                 }
  77.                 break;
  78.             }
  79.         }
  80.         if(somethingSelected) {
  81.             for(int i=0; i<all.length; i++) {
  82.                 if(all[i].selected && i != foundPoint) {
  83.                     /* The one to move */
  84.                     all[i].selected = false;
  85.                     if(foundPoint != -1) {
  86.                         all[foundPoint].selected = true;
  87.                         System.out.println("switch selection");
  88.                     }else{
  89.                         somethingSelected = false;
  90.                         System.out.println("cancel selection");
  91.                     }
  92.                     this.repaint();
  93.                     return;
  94.                 }else if(all[i].selected && i == foundPoint){
  95.                     /* The one to move */
  96.                     all[i].selected = false;
  97.                     somethingSelected = false;
  98.                     System.out.println("cancel selection");
  99.                     this.repaint();
  100.                     return;
  101.                 }
  102.             }
  103.         }
  104.     }
  105.    
  106.     public void handleMouseRelease(int mouseX, int mouseY) {
  107.         System.out.println("release");
  108.         if(somethingSelected && hasDragged) {
  109.             somethingSelected = false;
  110.             hasDragged = false;
  111.             for(int i=0; i<all.length; i++) {
  112.                 if(all[i].selected) {
  113.                     /* The one to move */
  114.                     all[i].selected = false;
  115.                     this.repaint();
  116.                     return;
  117.                 }
  118.             }
  119.         }
  120.     }
  121.    
  122.     public void handleMouseDrag(int mouseX, int mouseY) {
  123.         System.out.println("drag");
  124.         if(somethingSelected) {
  125.             hasDragged=true;
  126.             for(int i=0; i<all.length; i++) {
  127.                 if(all[i].selected) {
  128.                     /* The one to move */
  129.                     all[i].x = (mouseX-20)/xScale;
  130.                     all[i].y = (mouseY-20)/yScale;
  131.                     hull = calculateConvexHull(all);
  132.                     this.repaint();
  133.                     return;
  134.                 }
  135.             }
  136.         }
  137.     }
  138.    
  139.     public void paint(Graphics page) {
  140.        
  141.         Graphics2D g2d = (Graphics2D) page;
  142.         g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
  143.                 RenderingHints.VALUE_ANTIALIAS_ON);
  144.         /* draw forest */
  145.         g2d.setColor(new Color(254,254,254));
  146.         g2d.fillRect(0, 0, maxX+40, maxY+40);
  147.        
  148.         float dash1[] = {5.0f};
  149.         BasicStroke dashed =  new BasicStroke(2.0f,
  150.                             BasicStroke.CAP_BUTT,
  151.                             BasicStroke.JOIN_MITER,
  152.                             10.0f, dash1, 0.0f);
  153.         g2d.setStroke(dashed);
  154.    
  155.         /* draw lines */
  156.         g2d.setColor(new Color(200,30,30));
  157.         for(int i=0; i<hull.length;i++) {
  158.             double x1 = hull[i].x*xScale+20;
  159.             double y1 = hull[i].y*yScale+20;
  160.             double x2 = hull[(i+1)%hull.length].x*xScale+20;
  161.             double y2 = hull[(i+1)%hull.length].y*yScale+20;
  162.             Line2D.Double thisLine = new Line2D.Double(x1, y1, x2, y2);
  163.             g2d.draw(thisLine);
  164.         }
  165.         BasicStroke normal =  new BasicStroke();
  166.         g2d.setStroke(normal);
  167.        
  168.         /* draw points */
  169.         g2d.setColor(new Color(50,50,50));
  170.         g2d.setPaint(new Color(50,50,50));
  171.         for(int i=0; i<all.length; i++) {
  172.             double x = (all[i].x*xScale)-4+20;
  173.             double y = (all[i].y*yScale)-4+20;
  174.             Ellipse2D.Double thisOval = new Ellipse2D.Double(x, y, 8, 8);
  175.            
  176.             if(all[i].selected) {
  177.                 g2d.setColor(new Color(30,230,30));
  178.                 g2d.setPaint(new Color(30,230,30));
  179.                 g2d.fill(thisOval);
  180.                 g2d.setColor(new Color(50,50,50));
  181.                 g2d.setPaint(new Color(50,50,50));
  182.             }else{
  183.                 g2d.fill(thisOval);
  184.             }
  185.         }
  186.     }
  187.    
  188.     public ConvexHull(Point[] hull, Point[] all, double maxX, double maxY) {
  189.         this.hull = hull;
  190.         this.all = all;
  191.         this.setPreferredSize(new Dimension((int)(maxX*xScale)+40,(int)(maxY*yScale)+40));
  192.        
  193.         this.maxX = (int) (maxX*xScale);
  194.         this.maxY = (int) (maxY*yScale);
  195.        
  196.     }
  197.    
  198.     private static double cross(Point O, Point A, Point B) {
  199.         return (A.x - O.x) * (B.y - O.y) - (A.y - O.y) * (B.x - O.x);
  200.     }
  201.  
  202.     private static Point[] calculateConvexHull(Point[] grid) {
  203.         int n = grid.length;
  204.         int k = 0;
  205.        
  206.         Arrays.sort(grid);
  207.         Point[] hull = new Point[n*2];
  208.        
  209.         /* Lower hull */
  210.         for(int i=0; i<n; ++i) {
  211.             while (k >= 2 && cross(hull[k-2], hull[k-1], grid[i]) <= 0) {
  212.                 k--;
  213.             }
  214.             hull[k++] = grid[i];
  215.         }
  216.        
  217.         /* Upper hull */
  218.         int t = k+1;
  219.         for(int i=n-2; i>=0; i--) {
  220.             while(k >= t && cross(hull[k-2], hull[k-1], grid[i]) <= 0) {
  221.                 k--;
  222.             }
  223.             hull[k++] = grid[i];
  224.         }
  225.        
  226.         Point[] hull2 = new Point[k];
  227.         for(int i=0; i<k; i++) {
  228.             hull2[i] = hull[i];
  229.         }
  230.         return hull2;
  231.     }
  232.    
  233.     public static void main(String[] args) {
  234.         File inputFile = new File(args[0]);
  235.        
  236.         String names = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  237.         names += names.toLowerCase();
  238.        
  239.         Point[] hull = null, arrayOfPoints = null;
  240.         double maxX = 0;
  241.         double maxY = 0;
  242.         try{
  243.             BufferedReader br = new BufferedReader(new FileReader(inputFile));
  244.             String line = null;
  245.             int numPoints = Integer.parseInt(br.readLine());
  246.             arrayOfPoints = new Point[numPoints];
  247.            
  248.             for(int i=0; i<numPoints; i++) {
  249.                 line = br.readLine();
  250.                 StringTokenizer st = new StringTokenizer(line, ",");
  251.                 arrayOfPoints[i] = new Point(names.charAt(i), Double.parseDouble(st.nextToken()), Double.parseDouble(st.nextToken()));
  252.                 if(arrayOfPoints[i].x > maxX) {
  253.                     maxX = arrayOfPoints[i].x;
  254.                 }
  255.                 if(arrayOfPoints[i].y > maxY) {
  256.                     maxY = arrayOfPoints[i].y;
  257.                 }
  258.             }
  259.             hull = calculateConvexHull(arrayOfPoints);
  260.             br.close();
  261.         }catch(IOException e) {
  262.             System.out.println("Error: Could not read input.");
  263.             return;
  264.         }
  265.        
  266.         JFrame window = new JFrame("Daily Programmer 174 Hard");
  267.         window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  268.  
  269.         instance = new ConvexHull(hull, arrayOfPoints, maxX, maxY);
  270.         instance.addMouseListener(new MouseAdapter() {
  271.             @Override
  272.             public void mousePressed(MouseEvent e) {
  273.                 int mouseX = e.getX();
  274.                 int mouseY = e.getY();
  275.                
  276.                 instance.handleMousePress(mouseX, mouseY);
  277.             }
  278.            
  279.             @Override
  280.             public void mouseReleased(MouseEvent e) {
  281.                 int mouseX = e.getX();
  282.                 int mouseY = e.getY();
  283.                
  284.                 instance.handleMouseRelease(mouseX, mouseY);
  285.             }
  286.         });
  287.        
  288.         instance.addMouseMotionListener(new MouseMotionAdapter() {
  289.             @Override
  290.             public void mouseDragged(MouseEvent e) {
  291.                 int mouseX = e.getX();
  292.                 int mouseY = e.getY();
  293.                 instance.handleMouseDrag(mouseX, mouseY);
  294.             }
  295.         });
  296.         window.add(instance);
  297.         window.pack();
  298.         window.setVisible(true);
  299.     }
  300.  
  301. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement