java 数学问题 求解 已知2个点。(2点坐标已知 x1,y1 x2,y2) 求一条路线,点1围绕点2旋转靠近,最终重合。 这样的效果如何实现? 在线等,求大神帮忙。小弟先谢了!!! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 http://js.fgm.cc/learn/lesson7/03.html好像和你要的差不多, 1、根据AB两点计算出以B为圆心,A为边的圆的方程式2、根据你每秒需要转动的角度,每秒去计算一次当前AB点半径的直线公式3、根据你每秒需要缩短的长度,根据B点和 2的直线公式计算A点当前坐标。然后就是每秒绘制一次A点坐标了这样似乎可以?数学公式神马的算起来比较烦LZ加油吧 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(); }} 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(); }} 关于局部变量的问题 关于泛型 dispose()的问题 请问一个页面正则表达式 文件的创建、修改、删除哪种操作会消耗较多的资源 关于NoClassDefFoundError的奇怪郁闷头疼问题 一个程序的问题 接上次提问的字符串 接受的问题(未解决) tcp 仿真 最大的网上电脑书籍专卖店开张了 spring 注解事务的问题 求高手帮忙写个算法
2、根据你每秒需要转动的角度,每秒去计算一次当前AB点半径的直线公式
3、根据你每秒需要缩短的长度,根据B点和 2的直线公式计算A点当前坐标。然后就是每秒绘制一次A点坐标了这样似乎可以?数学公式神马的算起来比较烦LZ加油吧
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();
}}
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();
}}