PageRenderTime 147ms CodeModel.GetById 32ms app.highlight 53ms RepoModel.GetById 1ms app.codeStats 0ms

/tags/Root-branch-php-utl/SWIG/Examples/GIFPlot/Java/shadow/main.java

#
Java | 76 lines | 61 code | 11 blank | 4 comment | 4 complexity | ee3e94b98307a155e96e2e194a758881 MD5 | raw file
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0
 1// Plot a 3D function
 2
 3import java.lang.Math;
 4
 5public class main {
 6
 7  static {
 8    try {
 9        System.loadLibrary("gifplot");
10    } catch (UnsatisfiedLinkError e) {
11      System.err.println("Native code library failed to load. See the chapter on Dynamic Linking Problems in the SWIG Java documentation for help.\n" + e);
12      System.exit(1);
13    }
14  }
15
16  public static void main(String argv[]) {
17    
18    // Here are some plotting parameters
19    double xmin = -5.0;
20    double xmax =  5.0;
21    double ymin = -5.0;
22    double ymax =  5.0;
23    double zmin = -5.0;
24    double zmax =  5.0;
25    
26    // Grid resolution
27    int nxpoints =  60;
28    int nypoints =  60;
29    
30    ColorMap cmap = new ColorMap("cmap");
31    FrameBuffer frame = new FrameBuffer(500,500);
32    frame.clear((short)gifplot.BLACK);
33    
34    Plot3D p3 = new Plot3D(frame,xmin,ymin,zmin,xmax,ymax,zmax);
35    p3.lookat(2*(zmax-zmin));
36    p3.autoperspective(40);
37    p3.rotu(60);
38    p3.rotr(30);
39    p3.rotd(10);
40    
41    System.out.println( "Making a nice 3D plot..." );
42    p3.clear((short)gifplot.BLACK);
43    p3.start();
44    double dx = 1.0*(xmax-xmin)/nxpoints;
45    double dy = 1.0*(ymax-ymin)/nypoints;
46    double cscale = 240.0/(zmax-zmin);
47    double x = xmin;
48    for (int i = 0; i < nxpoints; i++) {
49      double y = ymin;
50      for (int j = 0; j < nypoints; j++) {
51        double z1 = func(x,y);
52        double z2 = func(x+dx,y);
53        double z3 = func(x+dx,y+dy);
54        double z4 = func(x,y+dy);
55        double c1 = cscale*(z1-zmin);
56        double c2 = cscale*(z2-zmin);
57        double c3 = cscale*(z3-zmin);
58        double c4 = cscale*(z4-zmin);
59        double c = (c1+c2+c3+c4)/4;
60        if (c < 0) c = 0;
61        if (c > 239) c = 239;
62        p3.solidquad(x,y,z1,x+dx,y,z2,x+dx,y+dy,z3,x,y+dy,z4,(short)(c+16));
63        y = y + dy;
64      }
65      x = x + dx;
66    }
67
68    frame.writeGIF(cmap,"image.gif");
69    System.out.println( "Wrote image.gif" );
70  } 
71
72  // Here is the function to plot
73  public static double func(double x, double y) {
74    return 5*java.lang.Math.cos(2*java.lang.Math.sqrt(x*x+y*y))*java.lang.Math.exp(-0.3*java.lang.Math.sqrt(x*x+y*y));
75  }
76}