Advertisement
Guest User

corner detection

a guest
Jul 8th, 2012
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.92 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import java.util.*;
  4. import javax.swing.*;
  5.  
  6.  
  7.  
  8. public class Main{
  9.     public static final double displace=25,tolerance=.7;
  10.    
  11.     public static JJPanel panel;
  12.     public static ArrayList<Point> list;
  13.     public static double px,py,x,y;
  14.    
  15.     public static void main(String[] args) throws Exception{
  16.         list=new ArrayList<Point>();
  17.         JFrame frame=new JFrame("corner detection");
  18.         frame.setContentPane(panel=new JJPanel());
  19.         Mouse m=new Mouse();
  20.         panel.addMouseListener(m);
  21.         panel.addMouseMotionListener(m);
  22.         frame.setVisible(true);
  23.         frame.setSize(800,600);
  24.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  25.     }
  26.    
  27.     public static double d(double x1,double y1,double x2,double y2){
  28.         double dx=x2-x1,dy=y2-y1;
  29.         return Math.sqrt(dx*dx+dy*dy);
  30.     }
  31.    
  32.     public static void click(int xx,int yy){
  33.         list=new ArrayList<Point>();
  34.         list.add(new Point(x=px=xx,y=py=yy));
  35.         panel.repaint();
  36.     }
  37.    
  38.     public static void drag(int xx,int yy){
  39.         double dist;
  40.         while((dist=d(x,y,xx,yy))>=displace){
  41.             double dx=px-xx,dy=py-yy;
  42.             double dd=Math.sqrt(dx*dx+dy*dy);
  43.             if(dd==0) break;
  44.             dx/=dd;
  45.             dy/=dd;
  46.             dd=(x-xx)*dx+(y-yy)*dy;
  47.             double ddx=dx*dd,ddy=dy*dd;
  48.             double backup=Math.sqrt(displace*displace-dist*dist+ddx*ddx+ddy*ddy);
  49.             list.add(new Point(x=xx+ddx-backup*dx,y=yy+ddy-backup*dy));
  50.         }
  51.         px=xx;
  52.         py=yy;
  53.         panel.repaint();
  54.     }
  55.    
  56.     public static void paint(Graphics g){
  57.         g.setColor(Color.black);
  58.         g.fillRect(0,0,800,600);
  59.         g.setColor(Color.gray);
  60.         boolean[] corner=new boolean[list.size()];
  61.         Point prev=null;
  62.         double pdx=0,pdy=0;
  63.         int i=0;
  64.         for(Point p:list){
  65.             if(prev!=null){
  66.                 g.drawLine((int)p.x,(int)p.y,(int)prev.x,(int)prev.y);
  67.                 double dx=p.x-prev.x,dy=p.y-prev.y;
  68.                 corner[i-1]=i>1 && d(dx,dy,pdx,pdy)>displace*tolerance;
  69.                 pdx=dx;
  70.                 pdy=dy;
  71.             }
  72.             i++;
  73.             prev=p;
  74.         }
  75.         i=0;
  76.         for(Point p:list){
  77.             g.setColor(corner[i++]? Color.red:Color.white);
  78.             int x=(int)p.x,y=(int)p.y;
  79.             g.fillRect(x-2,y-2,5,5);
  80.         }
  81.     }
  82. }
  83.  
  84.  
  85.  
  86. class Point{
  87.     public double x,y;
  88.     public Point(double x,double y){this.x=x;this.y=y;}
  89. }
  90.  
  91.  
  92.  
  93. class JJPanel extends JPanel{
  94.     public void paint(Graphics g){
  95.         Main.paint(g);
  96.     }
  97. }
  98.  
  99.  
  100.  
  101. class Mouse implements MouseListener,MouseMotionListener{
  102.     @Override
  103.     public void mousePressed(MouseEvent arg0){Main.click(arg0.getX(),arg0.getY());}
  104.     @Override
  105.     public void mouseDragged(MouseEvent arg0){Main.drag(arg0.getX(),arg0.getY());}
  106.  
  107.     @Override
  108.     public void mouseMoved(MouseEvent arg0){}
  109.     @Override
  110.     public void mouseClicked(MouseEvent arg0){}
  111.     @Override
  112.     public void mouseReleased(MouseEvent arg0){}
  113.     @Override
  114.     public void mouseEntered(MouseEvent arg0){}
  115.     @Override
  116.     public void mouseExited(MouseEvent arg0){}
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement