Листинг 11.izol.java - построение изолиний функции двух переменных
import java.awt.*;
import java.math.*;
import java.applet.*;
import java.awt.event.*;
public class izol extends Applet implements ActionListener{
int imgw,imgh,pw,ph,l,xs,ys;
double mas[]=new double[400];
double xsize,ysize,dxmin,dymin,xos,yos,xmax,ymax,xmin,ymin,
zmax,zmin,dz;
//Массив цветов...
Color colors[]={new Color(125,244,125),new Color(197,203,84),
Color.green, new Color(25,117,146),Color.blue};
int n1=10;
int n2=7;
int nlines=6;
int x0=0;
int x1=1;
int y0=0;
int y1=1;
double eps=0.1;
double pii=3.1415926;
double mark=0.1;
double hx=0.1;
double hy=0.1428;
public void paint(Graphics g) {
imgw=500;
imgh=500;
g.setColor(new Color(171,196,210));
g.fillRect(0,0,imgw,imgh);
g.setColor(Color.black);
g.drawRect(1,1,imgw,imgh);
g.drawRect(1,1,pw-3,ph-3);
}
public void init(){
Dimension d = size();
pw=d.width;
ph=d.height;
setBackground(new Color(125,168,203));
setLayout(null);
Button butt=new Button("Get This!");
butt.setBounds(593,373,53,20);
add(butt);
butt.addActionListener(this);
Label Autor=new Label("(c) 2006. Khramkov Ivan.");
Autor.setBounds(33,ph-30,333,20);
add(Autor);
}
void getfunc(){
int i,j;
double l1,l2,s;
l1=-(1+Math.sqrt(1+4*pii*pii*eps*eps))/(2*eps);
l2=-(1-Math.sqrt(1+4*pii*pii*eps*eps))/(2*eps);
for (i=0;i<=n1;i++)
{
for (j=0;j<=n2-1;j++)
{
s=Math.sin(pii*(y0+j*hy))/(pii*pii*eps)*
(1-((Math.exp(l2)-1)*Math.exp(l1*(x0+i*hx))
+(1-Math.exp(l1))*Math.exp(l2*(x0+i*hx)))/
(Math.exp(l2)-Math.exp(l1)));
if (s<=0)
s=0;
mas[i+j*n1]=s;
}
}
zmin=mas[0];
zmax=mas[0];
for (i=0;i<=n1*n2-1;i++)
if (mas[i]<zmin) zmin=mas[i];
else
if (mas[i]>zmax) zmax=mas[i];
dz=(zmax-zmin)/6;
}
//Функция рисования линии с координатами, полученными
//из функции interpolution с текущим цветом...
void Lines (Graphics g, int x0,int y0,int x1,int y1) {
g.setColor(colors[l]);
g.drawLine(x0,y0,x1,y1);
}
//Функция, вычисляющая коррдинаты точек для рисования
//по ним линий...
void interpolution(int l,int k,int x,int y, double z)
{
double t;
int c,ii;
int px[]=new int[2];
int py[]=new int[2];
double pr[]=new double[2];
Graphics img=getGraphics();
c=0;
if (mas[k+1]!=mas[k])
{
t=(z-mas[k])/(mas[k+1]-mas[k]);
if (t>=0&t<=1)
{
px[c]=(int)(x+t*xs);
py[c]=y;
c=c+1;
}
}
if (mas[k+1]!=mas[k+n1])
{
t=(z-mas[k+n1])/(mas[k+1]-mas[k+n1]);
if (t>=0&t<=1)
{
px[c]=(int)(x+t*xs);
py[c]=(int)(y+(1-t)*ys);
c=c+1;
}
}
if (mas[k]!=mas[k+n1])
{
t=(z-mas[k+n1])/(mas[k]-mas[k+n1]);
if (t>=0&t<=1)
{
px[c]=x;
py[c]=(int)(y+(1-t)*ys);
c=c+1;
}
}
if (c>0)
Lines(img,px[0],py[0],px[1],py[1]);
if (c>2)
Lines(img,px[1],py[1],px[2],py[2]);
c=0;
if (mas[k+1+n1]!=mas[k+1])
{
t=(z-mas[k+1])/(mas[k+1+n1]-mas[k+1]);
if (t>=0&t<=1)
{
px[c]=(int)(x+xs);
py[c]=(int)(y+t*ys);
c=c+1;
}
}
if (mas[k+1+n1]!=mas[k+n1])
{
t=(z-mas[k+n1])/(mas[k+1+n1]-mas[k+n1]);
if (t>=0&t<=1)
{
px[c]=(int)(x+t*xs);
py[c]=y+ys;
c=c+1;
}
}
if (mas[k+1]!=mas[k+n1])
{
t=(z-mas[k+n1])/(mas[k+1]-mas[k+n1]);
if (t>=0&t<=1)
{
px[c]=(int)(x+t*xs);
py[c]=(int)(y+(1-t)*ys);
c=c+1;
}
}
if (c>0)
Lines(img,px[0],py[0],px[1],py[1]);
if (c>2)
Lines(img,px[1],py[1],px[2],py[2]);
c=0;
}
public void actionPerformed(ActionEvent ae){
String s;
double t,z;
int i,j,k,x,y;
String values[]={"Lime","Olive","Green","Sea","Blue"};
String str = ae.getActionCommand();
Graphics img=getGraphics();
if(str.equals("Get This!"))
{
paint(img);
getfunc();
xs=(int)(imgw/n1);
ys=(int)(imgh/n2);
for (i=0;i<=n1-1;i++)
for (j=0;j<=n2-1;j++)
{
k=i+j*n1;
x=i*xs;
y=j*ys;
z=zmin;
l=-1;
while(z<=zmax)
{
z=z+dz;
l=l+1;
interpolution(l,k,x,y,z);
}
}
//Рисование таблицы с данными...
img.drawString("POSITION",549,49);
img.drawString("COLOR",653,49);
int step=30;
for(i=1;i<=5;i++)
{
img.setColor(colors[i-1]);
img.fillRect(650,30+step,195,20);
img.setColor(Color.black);
img.drawString(
""+String.format("%3.2f", (zmin+i*dz))+" "
+values[i-1],515,48+step);
img.drawLine(507,50+step,703,50+step);
img.drawLine(507,30,703,30);
img.drawLine(507,60,703,60);
img.drawLine(507,30,507,165);
img.drawLine(703,30,703,165);
img.drawLine(507,165,703,165);
img.drawLine(650,30,650,165);
step=step+21;
}
}
}
}
Листинг izol.html
---------------------------------------------------------
<html>
<body bgcolor=#FFFFFF>
<center><applet code="izol.class" width=709 height=553></applet></center>
<html>
