/tags/ttn-post-libtool-1-4-3-upgrade/SWIG/Examples/GIFPlot/Java/full/main.java
Java | 75 lines | 61 code | 10 blank | 4 comment | 4 complexity | 00e6821ed9571ffd45da2cbffbdd1582 MD5 | raw file
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0
1// Plot a 3D function
2import java.lang.Math;
3
4public class main {
5
6 static {
7 try {
8 System.loadLibrary("gifplot");
9 } catch (UnsatisfiedLinkError e) {
10 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);
11 System.exit(1);
12 }
13 }
14
15 public static void main(String argv[]) {
16
17 // Here are some plotting parameters
18 double xmin = -5.0;
19 double xmax = 5.0;
20 double ymin = -5.0;
21 double ymax = 5.0;
22 double zmin = -5.0;
23 double zmax = 5.0;
24
25 // Grid resolution
26 int nxpoints = 60;
27 int nypoints = 60;
28
29 SWIGTYPE_p_ColorMap cmap = gifplot.new_ColorMap("cmap");
30 SWIGTYPE_p_FrameBuffer frame = gifplot.new_FrameBuffer(500,500);
31 gifplot.FrameBuffer_clear(frame,(short)gifplot.BLACK);
32
33 SWIGTYPE_p_Plot3D p3 = gifplot.new_Plot3D(frame,xmin,ymin,zmin,xmax,ymax,zmax);
34 gifplot.Plot3D_lookat(p3,2*(zmax-zmin));
35 gifplot.Plot3D_autoperspective(p3,40);
36 gifplot.Plot3D_rotu(p3,60);
37 gifplot.Plot3D_rotr(p3,30);
38 gifplot.Plot3D_rotd(p3,10);
39
40 System.out.println( "Making a nice 3D plot..." );
41 gifplot.Plot3D_clear(p3,(short)gifplot.BLACK);
42 gifplot.Plot3D_start(p3);
43 double dx = 1.0*(xmax-xmin)/nxpoints;
44 double dy = 1.0*(ymax-ymin)/nypoints;
45 double cscale = 240.0/(zmax-zmin);
46 double x = xmin;
47 for (int i = 0; i < nxpoints; i++) {
48 double y = ymin;
49 for (int j = 0; j < nypoints; j++) {
50 double z1 = func(x,y);
51 double z2 = func(x+dx,y);
52 double z3 = func(x+dx,y+dy);
53 double z4 = func(x,y+dy);
54 double c1 = cscale*(z1-zmin);
55 double c2 = cscale*(z2-zmin);
56 double c3 = cscale*(z3-zmin);
57 double c4 = cscale*(z4-zmin);
58 double c = (c1+c2+c3+c4)/4;
59 if (c < 0) c = 0;
60 if (c > 239) c = 239;
61 gifplot.Plot3D_solidquad(p3,x,y,z1,x+dx,y,z2,x+dx,y+dy,z3,x,y+dy,z4,(short)(c+16));
62 y = y + dy;
63 }
64 x = x + dx;
65 }
66
67 gifplot.FrameBuffer_writeGIF(frame,cmap,"image.gif");
68 System.out.println( "Wrote image.gif" );
69 }
70
71 // Here is the function to plot
72 public static double func(double x, double y) {
73 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));
74 }
75}