Java Applet Source Code
import java.awt.*;
import java.applet.*;
import java.lang.*;
public class Cross extends Applet
{
Graphics sG;
Graphics bG;
Image bI;
double[] xaxis = {0.75, -0.5, -0.4330127};
double[] yaxis = {0.4330127, 0.8660254, -0.25};
double[] zaxis = {0.5, 0.0, 0.8660254};
double[] origin = {0.0, 0.0, 0.0};
double[] rod = {0.711603, -0.0767949, 0.166506};
double[] force = {0.928109, 0.456218, 0.387917};
double[] oldvector = {0.0, 0.0, 0.0};
double[] newvector = {0.0, 0.0, 0.0};
double[] perpvector = {0.0, 0.0, 0.0};
int[] xaxiscolor = {255, 0, 0};
int[] yaxiscolor = {0, 0, 255};
int[] zaxiscolor = {255, 0, 255};
int[] rodcolor = {0, 196, 0};
int[] forcecolor = {0, 0, 0};
int[] origincolor = {0, 0, 0};
int ballradius = 7;
int endradius = 5;
int cylinderradius = 2;
Color backcolor = new Color(247, 243, 234);
Color black = new Color(0, 0, 0);
Color warning = new Color(255, 192, 192);
public void init()
{
bI = createImage(this.size().width, this.size().height);
bG = bI.getGraphics();
bG.setFont(new Font("Courier", Font.BOLD, 14));
drawscreen();
}
public void paint(Graphics g)
{
g.drawImage(bI, 0, 0, this);
sG = getGraphics();
}
public void update(Graphics g)
{
g.drawImage(bI, 0, 0, this);
}
public Color makecolor(int[] clr)
{
Color out;
out = new Color(clr[0], clr[1], clr[2]);
return out;
}
public void drawscreen()
{
bG.setColor(backcolor);
bG.fillRect(0, 0, 421, 480);
cylinder(origin, xaxis, xaxiscolor, cylinderradius);
cylinder(origin, yaxis, yaxiscolor, cylinderradius);
cylinder(origin, zaxis, zaxiscolor, cylinderradius);
cylinder(origin, rod, rodcolor, cylinderradius);
cylinder(rod, force, forcecolor, cylinderradius);
ball(origin, origincolor, ballradius + 2);
ball(xaxis, xaxiscolor, ballradius);
ball(yaxis, yaxiscolor, ballradius);
ball(zaxis, zaxiscolor, ballradius);
ball(rod, rodcolor, endradius);
ball(force, forcecolor, endradius);
bG.setColor(makecolor(xaxiscolor));
bG.drawString("x-axis", 180, 440);
bG.setColor(makecolor(yaxiscolor));
bG.drawString("y-axis", 180, 455);
bG.setColor(makecolor(zaxiscolor));
bG.drawString("z-axis", 180, 470);
if(xaxis[0] < 0.0)
{
bG.setColor(black);
bG.fillRect(250, 430, 131, 10);
}
if(yaxis[0] < 0.0)
{
bG.setColor(black);
bG.fillRect(250, 445, 131, 10);
}
if(zaxis[0] < 0.0)
{
bG.setColor(black);
bG.fillRect(250, 460, 131, 10);
}
if(xaxis[0] > 0.999999)
{
bG.setColor(makecolor(xaxiscolor));
bG.fillRect(20, 430, 131, 10);
}
if(yaxis[0] > 0.999999)
{
bG.setColor(makecolor(yaxiscolor));
bG.fillRect(20, 445, 131, 10);
}
if(zaxis[0] > 0.999999)
{
bG.setColor(makecolor(zaxiscolor));
bG.fillRect(20, 460, 131, 10);
}
}
public void ball(double[] pt, int[] color, int radius)
{
double t;
int red, green, blue;
int x = xscale(pt[1]);
int y = yscale(pt[2]);
for (int i = radius; i >= 0; i = i - 1)
{
t = java.lang.Math.pow(
java.lang.Math.cos(1.57 * i/radius), 6.0);
red = (int) java.lang.Math.round(
t * 255 + (1 - t) * color[0]);
green = (int) java.lang.Math.round(
t * 255 + (1 - t) * color[1]);
blue = (int) java.lang.Math.round(
t * 255 + (1 - t) * color[2]);
bG.setColor(new Color(red, green, blue));
bG.fillOval(x - i, y - i, 2 * i + 1, 2 * i + 1);
}
}
public void cylinder(double[] from, double[] to, int[] color, int radius)
{
double t;
int red, green ,blue;
int a = xscale(from[1]);
int b = yscale(from[2]);
int c = xscale(to[1]);
int d = yscale(to[2]);
for(int i = radius; i >= 0; i = i - 1)
{
t = java.lang.Math.cos(1.57 * i/radius);
red = (int) java.lang.Math.round(
t * 255 + (1 - t) * color[0]);
green = (int) java.lang.Math.round(
t * 255 + (1 - t) * color[1]);
blue = (int) java.lang.Math.round(
t * 255 + (1 - t) * color[2]);
bG.setColor(new Color(red, green, blue));
for(int j = -i; j <= i; j = j + 1)
{
for(int k = -i; k <= i; k = k + 1)
{
bG.drawLine(a + j, b + k, c + j, d + k);
}
}
}
}
public int xscale(double x)
{
int z;
z = (int) java.lang.Math.round(210 + 200 * x);
return z;
}
public int yscale(double y)
{
int z;
z = (int) java.lang.Math.round(210 - 200 * y);
return z;
}
public void drawaxis(double[] v, Color hue)
{
bG.setColor(hue);
bG.drawLine(xscale(0), yscale(0), xscale(v[1]), yscale(v[2]));
bG.fillOval(xscale(v[1]) - 2, yscale(v[2]) - 2, 5, 5);
}
public double[] crossproduct(double[] x, double[] y)
{
double[] z = {1.0, 2.0, 3.0};
z[0] = x[1] * y[2] - x[2] * y[1];
z[1] = x[2] * y[0] - x[0] * y[2];
z[2] = x[0] * y[1] - x[1] * y[0];
return z;
}
public double dotproduct(double[] x, double[] y)
{
double z;
z = x[0] * y[0] + x[1] * y[1] + x[2] * y[2];
return z;
}
public double[] transform(double[] arg, double[] from, double[] to)
{
double[] perp = {1.0, 2.0, 3.0};;
double[] cto = {1.0, 2.0, 3.0};
double[] step1 = {1.0, 2.0, 3.0};
double[] step2 = {1.0, 2.0, 3.0};
double[] step3 = {1.0, 2.0, 3.0};
double mag, sine, cosine;
cosine = dotproduct(from, to);
sine = java.lang.Math.sqrt(1 - cosine * cosine);
cto[0] = to[0] - cosine * from[0];
cto[1] = to[1] - cosine * from[1];
cto[2] = to[2] - cosine * from[2];
mag = java.lang.Math.sqrt(dotproduct(cto, cto));
cto[0] = cto[0]/mag;
cto[1] = cto[1]/mag;
cto[2] = cto[2]/mag;
perp = crossproduct(from, cto);
step1[0] = dotproduct(arg, from);
step1[1] = dotproduct(arg, cto);
step1[2] = dotproduct(arg, perp);
step2[0] = step1[0] * cosine - step1[1] * sine;
step2[1] = step1[0] * sine + step1[1] * cosine;
step2[2] = step1[2];
step3[0] = step2[0] * from[0] +
step2[1] * cto[0] +
step2[2] * perp[0];
step3[1] = step2[0] * from[1] +
step2[1] * cto[1] +
step2[2] * perp[1];
step3[2] = step2[0] * from[2] +
step2[1] * cto[2] +
step2[2] * perp[2];
return step3;
}
public boolean mouseDrag(Event evt, int mx, int my)
{
mouseDown(evt, mx, my);
return true;
}
public boolean mouseDown(Event evt, int mx, int my)
{
double x, y, z, cut;
double mag, c1, c2;
int sw = 0;
double[] newxaxis = {1.0, 2.0, 3.0};
double[] newyaxis = {1.0, 2.0, 3.0};
double[] newzaxis = {1.0, 2.0, 3.0};
if((mx - 210) * (mx - 210) + (my - 210) * (my - 210) < 10)
{
mx = 210;
my = 210;
}
y = (mx - 210)/200.0;
z = (210 - my)/200.0;
if(1 - y * y - z * z < 0.00000001)
{
cut = java.lang.Math.sqrt(y * y + z * z);
y = y/cut;
z = z/cut;
}
x = 1.0 - y * y - z * z;
x = java.lang.Math.max(0.0, x);
x = java.lang.Math.sqrt(x);
if (((xaxis[1] - y) * (xaxis[1] - y) +
(xaxis[2] - z) * (xaxis[2] - z)) < 0.01)
{
sw = 1;
oldvector = xaxis;
}
if (((yaxis[1] - y) * (yaxis[1] - y) +
(yaxis[2] - z) * (yaxis[2] - z)) < 0.01)
{
sw = 1;
oldvector = yaxis;
}
if (((zaxis[1] - y) * (zaxis[1] - y) +
(zaxis[2] - z) * (zaxis[2] - z)) < 0.01)
{
sw = 1;
oldvector = zaxis;
}
if (sw == 1)
{
newvector[0] = x;
newvector[1] = y;
newvector[2] = z;
if(((newvector[0] - oldvector[0]) *
(newvector[0] - oldvector[0]) +
(newvector[1] - oldvector[1]) *
(newvector[1] - oldvector[1]) +
(newvector[2] - oldvector[2]) *
(newvector[2] - oldvector[2])) > 0.000001)
{
newxaxis = transform(xaxis, oldvector, newvector);
newyaxis = transform(yaxis, oldvector, newvector);
newzaxis = transform(zaxis, oldvector, newvector);
xaxis = newxaxis;
yaxis = newyaxis;
zaxis = newzaxis;
mag = java.lang.Math.sqrt(dotproduct(xaxis, xaxis));
xaxis[0] = xaxis[0]/mag;
xaxis[1] = xaxis[1]/mag;
xaxis[2] = xaxis[2]/mag;
c1 = dotproduct(yaxis, xaxis);
yaxis[0] = yaxis[0] - c1 * xaxis[0];
yaxis[1] = yaxis[1] - c1 * xaxis[1];
yaxis[2] = yaxis[2] - c1 * xaxis[2];
mag = java.lang.Math.sqrt(dotproduct(yaxis, yaxis));
yaxis[0] = yaxis[0]/mag;
yaxis[1] = yaxis[1]/mag;
yaxis[2] = yaxis[2]/mag;
c1 = dotproduct(zaxis, xaxis);
c2 = dotproduct(zaxis, yaxis);
zaxis[0] = zaxis[0] - c1 * xaxis[0] - c2 * yaxis[0];
zaxis[1] = zaxis[1] - c1 * xaxis[1] - c2 * yaxis[1];
zaxis[2] = zaxis[2] - c1 * xaxis[2] - c2 * yaxis[2];
mag = java.lang.Math.sqrt(dotproduct(zaxis, zaxis));
zaxis[0] = zaxis[0]/mag;
zaxis[1] = zaxis[1]/mag;
zaxis[2] = zaxis[2]/mag;
rod = transform(rod, oldvector, newvector);
force = transform(force, oldvector, newvector);
drawscreen();
sG.drawImage(bI, 0, 0, this);
}
}
return true;
}
}
Copyright c 1997 by
Frank Wattenberg, Department of Mathematics, Montana State University,
Bozeman, MT 59717