Lagrangesrc.txt

//This applet implements interpolation of a certain number of
//points using Lagrange polynomials
import java.awt.*;
import java.applet.Applet;
import Matrix;
import java.lang.Math;
public class Lagrange extends Applet {
Matrix points; //interpolating points
int numpoints;
Matrix mat; //used to precompute basis functions
Image offscreenImg; //used in double buffering
Graphics offscreenG;
double[] t; //time values
static final int k = 100; //number of partitions
int moveflag; //the point being moved, if any
Button restart;
Button addpoint;
Button subpoint;
boolean go = false; //for restart button
boolean addpt = false; //flag for add point button
boolean subpt = false; //flag for subtracting point button
//used to initialize the applet
public void init() {
//start off with 5 points
numpoints = 5;
startagain();
//create time interval [0,1] with k partitions
t = new double[k+1];
for(int i=0;i<=k;i++)
t[i] = (double)i/(double)k;
//create matrix with current info
create();
//create offscreen buffer
offscreenImg = createImage(size().width,size().height);
offscreenG = offscreenImg.getGraphics();
restart = new Button("Restart");
add(restart);
addpoint = new Button("Add Point");
add(addpoint);
subpoint = new Button("Subtract Point");
add(subpoint);
}
//precompute the basis functions for Lagrange polynomials and evaluate
//over the time interval, store in matrix
public void create() {
mat = new Matrix(numpoints,k+1);
1


Lagrangesrc.txt
double c;
for(int i=0;i<mat.rowsize();i++)
for(int j=0;j<mat.colsize();j++) {
c=1;
for(int h=0;h<numpoints;h++)
if(h!=i)
//calculate i,j position of matrix
c *= (t[j]-(double)h/(double)(numpoints-1))/
((double)i/(double)(numpoints-1)-
(double)h/(double)(numpoints-1));
mat.set(i,j,c);
}
//transpose matrix for later calculations
mat.trans();

}
//called internally by repaint()
public void update(Graphics g) {
paint(g);

}
//check if any of the buttons have been pressed
public boolean action(Event e, Object o) {
if (e.target == restart) {
go = true;
repaint();
return true;
}
else if(e.target == addpoint) {
addpt = true;
repaint();
return true;
}
else if(e.target == subpoint) {
if(numpoints>2) {
subpt = true;
repaint();
}
return true;
}
return false;

}
//Initialize all necessary data: points, moveflag
public void startagain() {
moveflag = numpoints;
points = new Matrix(numpoints,2);
double increment = (double)(size().width-60)/(double)(numpoints-1);
for(int i=0;i<numpoints;i++){
points.set(i,0,(i*increment)+30.0);
points.set(i,1,(double)size().height/2.0);
}

}
public void paint(Graphics g) {
2


Lagrangesrc.txt
//prepare buffered screen for plot
setBackground(Color.white);
offscreenG.setColor(Color.white);
offscreenG.fillRect(0,0,size().width,size().height);
offscreenG.setColor(Color.black);
//if adding a point, create a new matrix and new set of points
if(addpt) {
numpoints++;
startagain();
create();
addpt = false;
}
//if subtracting a point, create new matrix and new set of points
if(subpt) {
numpoints--;
startagain();
create();
subpt = false;
}
//if restart button, start all over
if(go) {
startagain();
go = false;
}
//Display number of points
offscreenG.drawString("Points = "+String.valueOf(numpoints),20,20);
//plot points
for(int i=0;i<numpoints;i++)
offscreenG.fillOval((int)points.get(i,0)-2,
(int)points.get(i,1)-2,4,4);
//draw interpolating polynomial by multiplying precomputed basis
function
//matrix by the points, thus taking a linear combination, and plotting
output
Matrix plot = new Matrix(mat.mult(points));
for(int i=0;i<plot.rowsize()-1;i++)
//must round off the doubles in the matrix
offscreenG.drawLine((int)Math.round(plot.get(i,0)),
(int)Math.round(plot.get(i,1)),
(int)Math.round(plot.get(i+1,0)),
(int)Math.round(plot.get(i+1,1)));
//draw image from buffer to screen
g.drawImage(offscreenImg,0,0,this);

}
//check if user is clicking on any of the points
public boolean mouseDown(Event evt, int x, int y) {
Point p = new Point(x,y);
for(int i=0;i<numpoints;i++)
3


Lagrangesrc.txt
for(int j=-2;j<3;j++)
for(int l=-2;l<3;l++)
if(p.equals(new Point((int)points.get(i,0)+j,
(int)points.get(i,1)+l)))
//set moveflag to the point being moved
moveflag=i;
return true;
}
public boolean mouseDrag(Event evt, int x, int y) {
//check if user is trying to drag an old point
if(moveflag < numpoints) {
//change the point being moved and redraw screen
points.set(moveflag,0,x);
points.set(moveflag,1,y);
repaint();
}
return true;
}
public boolean mouseUp(Event evt, int x, int y) {
moveflag = numpoints;
return true;
}

}
4