Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package hull;
- import java.awt.BasicStroke;
- import java.awt.Color;
- import java.awt.Dimension;
- import java.awt.Graphics;
- import java.awt.Graphics2D;
- import java.awt.RenderingHints;
- import java.awt.event.MouseAdapter;
- import java.awt.event.MouseEvent;
- import java.awt.event.MouseMotionAdapter;
- import java.awt.geom.Ellipse2D;
- import java.awt.geom.Line2D;
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileReader;
- import java.io.IOException;
- import java.util.Arrays;
- import java.util.StringTokenizer;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- @SuppressWarnings("serial")
- public class ConvexHull extends JPanel {
- static class Point implements Comparable {
- public double x;
- public double y;
- public char val;
- public boolean selected = false;
- public Point(char val, double x, double y) {
- this.x = x;
- this.y = y;
- this.val = val;
- }
- @Override
- public int compareTo(Object o) {
- Point p2 = (Point) o;
- if(p2.y>y) {
- return 1;
- }
- if(p2.y<y) {
- return -1;
- }
- return 0;
- }
- }
- public static ConvexHull instance;
- double xScale = 50;
- double yScale = 50;
- public Point[] hull;
- public Point[] all;
- public int maxX;
- public int maxY;
- public boolean somethingSelected = false;
- public boolean hasDragged = false;
- public void handleMousePress(int mouseX, int mouseY) {
- System.out.println("press");
- int foundPoint = -1;
- for(int i=0; i<all.length; i++) {
- if(Math.abs(all[i].x*xScale+20-mouseX) < 4 && Math.abs(all[i].y*yScale+20-mouseY) < 4) {
- foundPoint = i;
- if(!somethingSelected) {
- all[i].selected = true;
- somethingSelected = true;
- System.out.println("new selection");
- this.repaint();
- return;
- }
- break;
- }
- }
- if(somethingSelected) {
- for(int i=0; i<all.length; i++) {
- if(all[i].selected && i != foundPoint) {
- /* The one to move */
- all[i].selected = false;
- if(foundPoint != -1) {
- all[foundPoint].selected = true;
- System.out.println("switch selection");
- }else{
- somethingSelected = false;
- System.out.println("cancel selection");
- }
- this.repaint();
- return;
- }else if(all[i].selected && i == foundPoint){
- /* The one to move */
- all[i].selected = false;
- somethingSelected = false;
- System.out.println("cancel selection");
- this.repaint();
- return;
- }
- }
- }
- }
- public void handleMouseRelease(int mouseX, int mouseY) {
- System.out.println("release");
- if(somethingSelected && hasDragged) {
- somethingSelected = false;
- hasDragged = false;
- for(int i=0; i<all.length; i++) {
- if(all[i].selected) {
- /* The one to move */
- all[i].selected = false;
- this.repaint();
- return;
- }
- }
- }
- }
- public void handleMouseDrag(int mouseX, int mouseY) {
- System.out.println("drag");
- if(somethingSelected) {
- hasDragged=true;
- for(int i=0; i<all.length; i++) {
- if(all[i].selected) {
- /* The one to move */
- all[i].x = (mouseX-20)/xScale;
- all[i].y = (mouseY-20)/yScale;
- hull = calculateConvexHull(all);
- this.repaint();
- return;
- }
- }
- }
- }
- public void paint(Graphics page) {
- Graphics2D g2d = (Graphics2D) page;
- g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
- RenderingHints.VALUE_ANTIALIAS_ON);
- /* draw forest */
- g2d.setColor(new Color(254,254,254));
- g2d.fillRect(0, 0, maxX+40, maxY+40);
- float dash1[] = {5.0f};
- BasicStroke dashed = new BasicStroke(2.0f,
- BasicStroke.CAP_BUTT,
- BasicStroke.JOIN_MITER,
- 10.0f, dash1, 0.0f);
- g2d.setStroke(dashed);
- /* draw lines */
- g2d.setColor(new Color(200,30,30));
- for(int i=0; i<hull.length;i++) {
- double x1 = hull[i].x*xScale+20;
- double y1 = hull[i].y*yScale+20;
- double x2 = hull[(i+1)%hull.length].x*xScale+20;
- double y2 = hull[(i+1)%hull.length].y*yScale+20;
- Line2D.Double thisLine = new Line2D.Double(x1, y1, x2, y2);
- g2d.draw(thisLine);
- }
- BasicStroke normal = new BasicStroke();
- g2d.setStroke(normal);
- /* draw points */
- g2d.setColor(new Color(50,50,50));
- g2d.setPaint(new Color(50,50,50));
- for(int i=0; i<all.length; i++) {
- double x = (all[i].x*xScale)-4+20;
- double y = (all[i].y*yScale)-4+20;
- Ellipse2D.Double thisOval = new Ellipse2D.Double(x, y, 8, 8);
- if(all[i].selected) {
- g2d.setColor(new Color(30,230,30));
- g2d.setPaint(new Color(30,230,30));
- g2d.fill(thisOval);
- g2d.setColor(new Color(50,50,50));
- g2d.setPaint(new Color(50,50,50));
- }else{
- g2d.fill(thisOval);
- }
- }
- }
- public ConvexHull(Point[] hull, Point[] all, double maxX, double maxY) {
- this.hull = hull;
- this.all = all;
- this.setPreferredSize(new Dimension((int)(maxX*xScale)+40,(int)(maxY*yScale)+40));
- this.maxX = (int) (maxX*xScale);
- this.maxY = (int) (maxY*yScale);
- }
- private static double cross(Point O, Point A, Point B) {
- return (A.x - O.x) * (B.y - O.y) - (A.y - O.y) * (B.x - O.x);
- }
- private static Point[] calculateConvexHull(Point[] grid) {
- int n = grid.length;
- int k = 0;
- Arrays.sort(grid);
- Point[] hull = new Point[n*2];
- /* Lower hull */
- for(int i=0; i<n; ++i) {
- while (k >= 2 && cross(hull[k-2], hull[k-1], grid[i]) <= 0) {
- k--;
- }
- hull[k++] = grid[i];
- }
- /* Upper hull */
- int t = k+1;
- for(int i=n-2; i>=0; i--) {
- while(k >= t && cross(hull[k-2], hull[k-1], grid[i]) <= 0) {
- k--;
- }
- hull[k++] = grid[i];
- }
- Point[] hull2 = new Point[k];
- for(int i=0; i<k; i++) {
- hull2[i] = hull[i];
- }
- return hull2;
- }
- public static void main(String[] args) {
- File inputFile = new File(args[0]);
- String names = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
- names += names.toLowerCase();
- Point[] hull = null, arrayOfPoints = null;
- double maxX = 0;
- double maxY = 0;
- try{
- BufferedReader br = new BufferedReader(new FileReader(inputFile));
- String line = null;
- int numPoints = Integer.parseInt(br.readLine());
- arrayOfPoints = new Point[numPoints];
- for(int i=0; i<numPoints; i++) {
- line = br.readLine();
- StringTokenizer st = new StringTokenizer(line, ",");
- arrayOfPoints[i] = new Point(names.charAt(i), Double.parseDouble(st.nextToken()), Double.parseDouble(st.nextToken()));
- if(arrayOfPoints[i].x > maxX) {
- maxX = arrayOfPoints[i].x;
- }
- if(arrayOfPoints[i].y > maxY) {
- maxY = arrayOfPoints[i].y;
- }
- }
- hull = calculateConvexHull(arrayOfPoints);
- br.close();
- }catch(IOException e) {
- System.out.println("Error: Could not read input.");
- return;
- }
- JFrame window = new JFrame("Daily Programmer 174 Hard");
- window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- instance = new ConvexHull(hull, arrayOfPoints, maxX, maxY);
- instance.addMouseListener(new MouseAdapter() {
- @Override
- public void mousePressed(MouseEvent e) {
- int mouseX = e.getX();
- int mouseY = e.getY();
- instance.handleMousePress(mouseX, mouseY);
- }
- @Override
- public void mouseReleased(MouseEvent e) {
- int mouseX = e.getX();
- int mouseY = e.getY();
- instance.handleMouseRelease(mouseX, mouseY);
- }
- });
- instance.addMouseMotionListener(new MouseMotionAdapter() {
- @Override
- public void mouseDragged(MouseEvent e) {
- int mouseX = e.getX();
- int mouseY = e.getY();
- instance.handleMouseDrag(mouseX, mouseY);
- }
- });
- window.add(instance);
- window.pack();
- window.setVisible(true);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement