📄 threesamples.java
字号:
/*@author 邱宇峰 ,software project031,
*2004,9,30
*/
import java.awt.*;
import java.awt.event.*;
public class threeSamples extends Frame implements MouseListener,ActionListener{
double s1;
static threeSamples frm=new threeSamples();
static Button b1;
static int location[][]=new int[30][2];
static double h[];
static double a[];
static double b[];
static double c[];
static double d[];
//static double s1[];
static double s2[];
static double newY;
static int X=0;
static int count=0;
static double p=0;
public static void main(String[] args){
frm.setTitle("三次自然样条");
b1=new Button("start");
frm.add(b1,"South");
frm.setSize(500,500);
frm.setLocation(200,200);
frm.setVisible(true);
frm.addMouseListener(frm);
b1.addActionListener(frm);
frm.addWindowListener(new WindowAdapter(){public void windowClosing(WindowEvent e){System.exit(0);}
});
}
public void mouseEntered(MouseEvent e){ }
public void mousePressed(MouseEvent e){ }
public void mouseExited(MouseEvent e){ }
public void mouseReleased(MouseEvent e){ }
/*this mouseevent is to get the basic spots
*/
public void mouseClicked(MouseEvent e){
Graphics g=getGraphics();
int x=e.getX();
int y=e.getY();
location[count][0]=x;location[count][1]=y;
g.drawString("。",x,y);
count++;
}
/*this actionevent is to draw the line
*/
public void actionPerformed(ActionEvent e){
Graphics g=getGraphics();
h=new double[count];
a=new double[count];
b=new double[count];
c=new double[count];
d=new double[count];
s2=new double[count];
for(int i=0;i<count-1;i++) h[i]=((double)location[i+1][0]-(double)location[i][0]);
a[1]=2.0*(h[0]+h[1]);
for(int i=2;i<count-1;i++) a[i]=2*(h[i-1]+h[i])-(h[i-1]*h[i-1])/a[i-1];
for(int i=1;i<count;i++) c[i]=((double)location[i][1]-(double)location[i-1][1])/h[i-1];
for(int i=1;i<count-1;i++) d[i]=6*(c[i+1]-c[i]);
b[1]=d[1];
for(int i=2;i<count-1;i++) b[i]=d[i]-(b[i-1]*h[i-1])/a[i-1];
s2[0]=0;s2[count-1]=0;
s2[count-2]=b[count-2]/a[count-2];
for(int i=count-3;i>=1;i--) s2[i]=(b[i]-h[i]*s2[i+1])/a[i];
for(int i=0;i<count-1;i++){
X=location[i][0];
while(X<=location[i+1][0]){
s1=c[i+1]-s2[i+1]*h[i]/6-s2[i]*h[i]/3;
newY=(double)location[i][1]+s1*((double)X-(double)location[i][0])+s2[i]*((double)X-(double)location[i][0])*((double)X-(double)location[i][0])/2+(s2[i+1]-s2[i])*((double)X-(double)location[i][0])*((double)X-(double)location[i][0])*((double)X-(double)location[i][0])/(6*h[i]);
g.drawString(".",X,(int)newY);
X++;
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -