已知2个点。(2点坐标已知 x1,y1  x2,y2)
   求一条路线,点1围绕点2旋转靠近,最终重合。
   这样的效果如何实现?
   
   在线等,求大神帮忙。小弟先谢了!!!

解决方案 »

  1.   

    http://js.fgm.cc/learn/lesson7/03.html好像和你要的差不多,
      

  2.   

    1、根据AB两点计算出以B为圆心,A为边的圆的方程式
    2、根据你每秒需要转动的角度,每秒去计算一次当前AB点半径的直线公式
    3、根据你每秒需要缩短的长度,根据B点和 2的直线公式计算A点当前坐标。然后就是每秒绘制一次A点坐标了这样似乎可以?数学公式神马的算起来比较烦LZ加油吧
      

  3.   


    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();
    }}
      

  4.   


    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();
    }}