//
import java.awt.*;
import java.applet.*;
public class CondShow extends Applet {
TextField a11,a12,a21,a22,cond;
ConditionOvalPanel co;
Jama.Matrix mat;
public void init(){
setLayout(new BorderLayout());
setBackground(Color.white);
co = new ConditionOvalPanel(10,10);
ConditionOvalPanel.a11 = 1;
ConditionOvalPanel.a12 = 0;
ConditionOvalPanel.a21 = 0;
ConditionOvalPanel.a22 = 1;
co.ax1 = (int)(75*1);
co.ay1 = (int)(75*0);
co.ax2 = (int)(75*0);
co.ay2 = (int)(75*1);
a11 = new TextField(3);
a12 = new TextField(3);
a21 = new TextField(3);
a22 = new TextField(3);
add("Center",co);
Panel pControlMain = new Panel();
pControlMain.setLayout(new BorderLayout());
//----- Control Panel -----//
Panel pControl = new Panel();
Panel A = new Panel();
A.setLayout(new GridLayout(2,2,2,2));
A.add(a11);
A.add(a12);
A.add(a21);
A.add(a22);
pControl.add(new Label("A = "));
pControl.add(A);
pControl.add(new Label(", cond(A) = "));
cond = new TextField(4);
cond.setEditable(false);
pControl.add(cond);
pControlMain.add("Center",pControl);
//----- Control Panel -----//
Panel p123 = new Panel();
p123.add(new Button("Reset"));
p123.add(new Button("Calculate"));
pControlMain.add("South",p123);
add("South",pControlMain);
reset();
} // _init()
public boolean action(Event evt, Object arg){
if (arg.equals("Reset")){
reset();
//repaint();
a11.setText("");
a12.setText("");
a21.setText("");
a22.setText("");
co.ax1 = (int)(0);
co.ay1 = (int)(0);
co.ax2 = (int)(0);
co.ay2 = (int)(0);
ConditionOvalPanel.a11 = 0;
ConditionOvalPanel.a12 = 0;
ConditionOvalPanel.a21 = 0;
ConditionOvalPanel.a22 = 0;
co.repaint();
return true;
}
if (arg.equals("Calculate")){
double a[][] = new double[2][2];
a[0][0]= Double.valueOf(a11.getText()).doubleValue();
a[0][1]=Double.valueOf(a12.getText()).doubleValue();
a[1][0]=Double.valueOf(a21.getText()).doubleValue();
a[1][1]=Double.valueOf(a22.getText()).doubleValue();
Jama.SingularValueDecomposition svd = new Jama.SingularValueDecomposition(new Jama.Matrix(a));
cond.setText(""+svd.cond());
// Site of latest battle
//ConditionOvalPanel.scaling
double an[] = {a[0][0],a[0][1],a[1][0],a[1][1]};
double temp = 1;
ConditionOvalPanel.scaling = 2;
boolean over1_9 = false;
for (int i=0;i<4;i++){
if (Math.abs(an[i]) > 1.9)
over1_9 = true;
}
if (!over1_9) {
for (int i=0;i<4;i++){
if (Math.abs(an[i]) < .5 && Math.abs(an[i]) > 0)
if (Math.abs(an[i])< temp)
temp = an[i];
}
temp = Math.abs(temp);
for (int i=0;i<4;i++){
an[i] = an[i]/temp;
}
ConditionOvalPanel.scaling = ConditionOvalPanel.scaling*temp;
} //else {
temp = 1;
// large
for (int i=0;i<4;i++){
if (Math.abs(an[i]) > 1.9 && Math.abs(an[i]) > temp)
temp = an[i];
}
temp = Math.abs(temp);
for (int i=0;i<4;i++){
an[i] = an[i]/temp;
}
ConditionOvalPanel.scaling = ConditionOvalPanel.scaling*temp;
//}
a[0][0] = an[0];
a[0][1] = an[1];
a[1][0] = an[2];
a[1][1] = an[3];
//
co.ax1 = (int)(75*a[0][0]);
co.ay1 = (int)(75*a[1][0]);
co.ax2 = (int)(75*a[0][1]);
co.ay2 = (int)(75*a[1][1]);
ConditionOvalPanel.a11 = a[0][0];
ConditionOvalPanel.a12 = a[0][1];
ConditionOvalPanel.a21 = a[1][0];
ConditionOvalPanel.a22 = a[1][1];
co.repaint();
}
return true;
}
public void reset(){
a11.setText("1");
a12.setText("0");
a21.setText("0");
a22.setText("1");
cond.setText("1");
}
} // End class
////////////////////////////////////////////////////////////
class ConditionOvalPanel extends Panel {
int w,
h;
int ovalW,
ovalH;
int ax1=0,ay1=0, ax2=0, ay2=0;
static double a11=0,a12=0,a21=0,a22=0;
double rangle = 0;
static double scaling = 2;
static boolean bobish = false;
public ConditionOvalPanel(int w, int h){
this.w = w;
this.h = h;
ovalW = 50;
ovalH = 50;
rangle = 0;
scaling = 2;
}
public void paint(Graphics g){
w = this.size().width;
h = this.size().height;
g.setColor(Color.black);
g.drawLine(0,h/2,w,h/2);
g.drawLine(w/2,0,w/2,h);
// draw scaling
g.drawString("-"+scaling,3,h/2-2);
g.drawString(""+scaling,w/2+2, 12);
drawVector(g);
g.setColor(Color.blue);
CircleEx.generatePoints(a11,a12,a21,a22,75,75,300,300,g);
}
public void drawVector(Graphics g){
g.setColor(Color.red);
// Horizontal Vector e1
drawArrow(w/2,h/2,w/2+ax1,h/2-ay1,g);
// Vertical Vector e2
drawArrow(w/2,h/2,w/2+ax2,h/2-ay2,g);
}
public void drawArrow(int x1, int y1, int x2, int y2, Graphics g) {
double d = Math.atan((float)((double)(y1 - y2) / (double)(x1 - x2)));
if(x1 - x2 < 0)
d += 3.1415926535897931D;
g.drawLine(x1, y1, x2, y2);
g.drawLine(x2, y2, x2 + (int)(Math.cos(d + 0.29999999999999999D) * 10D), y2 + (int)(Math.sin(d + 0.29999999999999999D) * 10D));
g.drawLine(x2, y2, x2 + (int)(Math.cos(d - 0.29999999999999999D) * 10D), y2 + (int)(Math.sin(d - 0.29999999999999999D) * 10D));
}
public boolean mouseDown(Event evt, int x, int y){
repaint();
return true;
}
} // _ConditionOval
//
////////////////////////////////////////////////////////////////////////////////
//
class CircleEx extends Polygon{
int centerX,
centerY,
width,
height;
public CircleEx(int centerX, int centerY, int width, int height){
this.centerX = centerX;
this.centerY = centerY;
}
public static void generatePoints(double a11, double a12, double a21, double a22,int lengthX, int lengthY,int width, int height, Graphics g){
int num_of_points = 360*4;
double x[] = new double [num_of_points],
y[] = new double [num_of_points];
double theta = num_of_points/360;
for (int i=0;i