日期:2014-05-20  浏览次数:20781 次

java 数学问题 求解
已知2个点。(2点坐标已知 x1,y1 x2,y2)
  求一条路线,点1围绕点2旋转靠近,最终重合。
  这样的效果如何实现?
   


  在线等,求大神帮忙。小弟先谢了!!!

------解决方案--------------------
Java code

public class ScrewPanel extends JPanel{
    
    private Point p1;
    private Point p2;
    private double distance;
    private static final int OVAL_SIZE = 20;
    private static final Double ANGLE = Math.PI/60;
    private double rate;
    private double th = 0;
    private Timer timer;
    
    public ScrewPanel(Point p1, final Point p2, int times) {
        
        super();
        this.p1 = p1;
        this.p2 = p2;
        this.distance = Math.sqrt(Math.pow(p1.x-p2.x, 2)+Math.pow(p1.y-p2.y, 2));
        this.rate = distance/times;
        this.setPreferredSize(new Dimension(500, 600));
        timer = new Timer((int) rate, new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e) {
                
                EventQueue.invokeLater(new Runnable(){

                    @Override
                    public void run() {    
                        distance-=rate;
                        if(distance<=0)return;
                        th = th+ANGLE;
                        p2.x = (int) (distance*Math.cos(th));
                        p2.y = (int) (distance*Math.sin(th));
                        repaint();
                        try {
                            Thread.sleep(30);
                        } catch (InterruptedException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                        
                    }
                    
                });                
            }        
        });    
        timer.start();
    }
    
    @Override
    public void paint(Graphics g)
    {
        super.paint(g);
        g.fillOval(p1.x, p1.y, OVAL_SIZE, OVAL_SIZE);
        g.translate(p1.x, p1.y);    
        g.fillOval(p2.x, p2.y, OVAL_SIZE, OVAL_SIZE);
    
    
    }
    
    @Override
    public void finalize()
    {
        timer.stop();
        timer = null;
    }
    /**
     * @param args
     */
    public static void main(String[] args) {
        
        JFrame f = new JFrame();
        f.setVisible(true);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(new ScrewPanel(new Point(200, 200), new Point(50, 50), 1000));
        f.pack();
    }

}

------解决方案--------------------
Java code

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;


public class ScrewPanel extends JPanel{
    
    private Point p1;
    private Point p2;
    private double distance;
    private static final int OVAL_SIZE = 20;
    private static final Double ANGLE = Math.PI/60;
    private double rate;
    private double th = 0;
    private Timer timer;
    
    public ScrewPanel(Point p1, final Point p2, int times) {
        
        super();
        this.p1 = p1;
        this.p2 = p2;
        this.distance = Math.sqrt(Math.pow(p1.x-p2.x, 2)+Math.pow(p1.y-p2.y, 2));
        this.rate = distance/times;
        this.setPreferredSize(new Dimension(500, 600));
        timer = new Timer((int) rate, new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e) {
                
                EventQueue.invokeLater(new Runnable(){

                    @Override
                    public void run() {    
                        distance-=rate;
                        if(distance<=0)
                        {
                            timer.stop();
                            timer = null;
                            return;
                        }
                        th = th+ANGLE;
                        p2.x = (int) (distance*Math.cos(th));
                        p2.y = (int) (distance*Math.sin(th));
                        repaint();
                        try {
                            Thread.sleep(30);
                        } catch (InterruptedException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                        
                    }
                    
                });                
            }        
        });    
        timer.start();
    }
    
    @Override
    public void paint(Graphics g)
    {
        super.paint(g);
        g.fillOval(p1.x, p1.y, OVAL_SIZE, OVAL_SIZE);
        g.translate(p1.x, p1.y);    
        g.fillOval(p2.x, p2.y, OVAL_SIZE, OVAL_SIZE);
    
    
    }
    
    public static void main(String[] args) {
        
        JFrame f = new JFrame();
        f.setVisible(true);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(new ScrewPanel(new Point(200, 200), new Point(50, 50), 1000));
        f.pack();
    }

}