// July 2002, java applet by Oliver Knill for the CCP project // Vector Fields and Line Integrals by Oliver Knill and Dale Winter // Harvard University, Nov 15, 2002. See // http://www.math.duke.edu/education/ccp/materials/mvcalc/vfield/index.html // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License of the Free Software Foundation for // details. Compile with // javac pencil.java vectorfield.java lineintegral.java import java.lang.Math; import java.awt.*; class vectorfield { private int cx1, cy1; // previous plot point private int cx2, cy2; // next plot point private int cx3, cy3; // midpoint of last two points private int cx1_old, cy1_old; // previous plot point private int cx2_old, cy2_old; // previous plot point private int grid1=10; // number of grid points for vector field private int grid=1000; // number of grid points *100 for vector field private int re,gr,bl; // red, green, blue private int re0,gr0,bl0; // red, green, blue private int i,j,k; // indices private double sum=0; // line integral sum private int x,y,x_old,y_old; // pointer location private int left_border=80; // left border for menu private int xx[]; // x-coordinates of traced curve private int yy[]; // y-coordinates of traced curve private int length=1000; // maximal number of points in curve private int t=-1; // time, parameter to trace curve private int dx=0,dy=0; // distance of beginning and end point private int tx=14, ty=70; // place to put comments private int n0,n0_old; // sums to be written private boolean finished=false; // curve closed private boolean smoothed=false; // curve smoothed public void draw_text(Graphics g, int red, int green, int blue, int center_x,int center_y, int size,String text) { g.setColor(new Color(red,green,blue)); Font f = new Font("Helvetica",Font.BOLD,size); g.setFont(f); g.drawString(text,center_x+10,center_y); } public vectorfield(int width,int height) { grid=100*grid1; xx=new int[length]; for (i=0; ileft_border) { draw_vector(g,cx1_old,cy1_old,cx2_old,cy2_old); } g.setColor(new Color(red,green,blue)); if (center_x>left_border) { draw_vector(g,cx1,cy1,cx2,cy2); } cx1_old=cx1; cy1_old=cy1; cx2_old=cx2; cy2_old=cy2; } public void put_single_vector(Graphics g, int red, int green, int blue, int center_x,int center_y, int vector_length,int vectorfield) { cx1=center_x; cy1=center_y; cx2=(int) (cx1+2*ff(vector_length,(int) ((cx1-300)*grid/250),(int) ((cy1-250)*grid/250),vectorfield)); cy2=(int) (cy1+2*gg(vector_length,(int) ((cx1-300)*grid/250),(int) ((cy1-250)*grid/250),vectorfield)); g.setColor(new Color(red,green,blue)); draw_vector(g,cx1,cy1,cx2,cy2); cx1_old=cx1; cy1_old=cy1; cx2_old=cx2; cy2_old=cy2; } public void erase(Graphics g) { int l; for (l=1; l<8; l++) { draw_field(g,0,0,0,300,250,250,250,10,15,l); } } public void menu(Graphics g,boolean over,int vectorfield) { re0=255; gr0=127; bl0=0; if (over) { sum=0; re=255; gr=255; bl=0; } else { re=255; gr=200; bl=0; } write_sum(g,sum); g.setColor(new Color(125,0,125)); // menu field around result g.drawRect(0,0,50,75); g.setColor(new Color(125,50,0)); // menu field around vectorfields g.drawRect(0,left_border,50,400); g.setColor(new Color(255,200,0)); draw_text(g,re0,gr0,bl0,0,100,12,"New"); for (k=1; k<8; k++) { if (k !=vectorfield) { draw_text(g,re0,gr0,bl0,8,100+k*50,30,""+k); } } draw_text(g,re,gr,bl,8,100+vectorfield*50,30,""+vectorfield); } public void read_pointer(int x, int y) { this.x = x; this.y = y; } public void erase_old_curve(Graphics g) { if (t>1) { g.setColor(new Color(0,0,0)); g.fillOval(xx[2]-3,yy[2]-3,6,6); for (i=2; i1) { g.fillOval(xx[2]-3,yy[2]-3,6,6); for (i=2; i0) { write_sum(g,sum); } } public void trace_curve(Graphics g, int c) { for (i=2; i<=t; i=i+5) { put_single_vector(g,255,255,0,xx[i],yy[i],20,c); } } public void erase_trace_curve(Graphics g) { for (i=2; i<=t; i=i+5) { for (j=1; j<8; j++) { put_single_vector(g,0,0,0,xx[i],yy[i],20,j); } } } public void update(Graphics g,int c) { draw_field(g,255,127,0,300,250,250,250,10,15,c); if (!finished) { draw_single_vector(g,255,255,0,x,y,20,c); } else { draw_single_vector(g,0,0,0,x,y,20,c); } if (x>left_border) { draw_new_curve(g); } if (finished && (!smoothed)) { erase_old_curve(g); draw_single_vector(g,0,0,0,x,y,20,c); smooth_curve(); smooth_curve(); draw_new_curve(g); smoothed=true; } if (finished) { trace_curve(g,c); line_integral(g,c); } if (x<=left_border) { erase_old_curve(g); erase_trace_curve(g); t=-1; finished=false; smoothed=false; } x_old=x; y_old=y; if (!finished) { t++; while (t>length) {t=t-length; } xx[t]=x_old; yy[t]=y_old; if (t<=2) { i=2; j=2; sum=0; } else {i=t; j=t-1; } cx1=(xx[i]-300)*grid/250; cy1=(yy[i]-250)*grid/250; cx2=(xx[j]-300)*grid/250; cy2=(yy[j]-250)*grid/250; cx3=(int) (cx1+cx2)/2; cy3=(int) (cy1+cy2)/2; sum+=((cx1-cx2)*ff(10,cx3,cy3,c)+(cy1-cy2)*gg(10,cx3,cy3,c)); write_sum(g,sum); dx=xx[t]-xx[2]; dy=yy[t]-yy[2]; } if (((dx*dx+dy*dy)<20) && (t>20)) { finished=true; } } }