JWorld@TW the best professional Java site in Taiwan
      註冊 | 登入 | 全文檢索 | 排行榜  

» JWorld@TW » Java 程式分享區 » Image  

按列印兼容模式列印這個話題 列印話題    把這個話題寄給朋友 寄給朋友    訂閱主題
reply to postflat modego to previous topicgo to next topic
本主題所含的標籤
無標籤
作者 [圖像]3D函數畫圖
T55555

Java, Ruby, Haskell

版主

發文: 1026
積分: 24
於 2004-05-06 04:04 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
看到 Yoshi 的函數畫圖 ( Mathenobita - http://www.javaworld.com.tw/jute/post/view?bid=35&id=47178&sty=1&tpg=1&age=0 )
也想來寫一個簡單的數學函數畫圖.

To Run this program: ( 去掉 comment, 連 90 行都不到 )
(1) You have to install the Java 3D package. ( + OpenGL or DirectX )
(2) Use mouse to rotate. (Hold left mouse button and move to rotate)
(3) 見笑了, This is a quick-and-dirty program, it is just for fun, it is not perfect and maybe having bug.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import com.sun.j3d.utils.behaviors.mouse.MouseRotate;
import com.sun.j3d.utils.universe.SimpleUniverse;
import javax.media.j3d.*;
import javax.swing.JFrame;
import javax.vecmath.Point3f;
 
/**
 * Quick draw function:
 * f(x, y) = sin(r) / r  where r = sqrt(x ^ 2 + y ^ 2)
 *
 * @author T55555
 * @version 2004-05-05
 */
public class MathGeom extends JFrame {
    public static void main(String[] args) {
        new MathGeom().show();
    }
 
    public MathGeom() {
        super("z = Sin(r)/r | r = sqrt(x^2+y^2) ----- By T55555");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(400, 400);
        setLocationRelativeTo(null);
        Canvas3D canvas3D = new Canvas3D(SimpleUniverse.getPreferredConfiguration());
        getContentPane().add(canvas3D);
 
        SimpleUniverse simpleU = new SimpleUniverse(canvas3D);
        simpleU.getViewingPlatform().setNominalViewingTransform();
 
        BranchGroup objRoot = new BranchGroup();
        Appearance appearance = new Appearance();
        PolygonAttributes attributes = new PolygonAttributes();
        attributes.setPolygonMode(PolygonAttributes.POLYGON_LINE);
        attributes.setCullFace(PolygonAttributes.CULL_BACK);
        appearance.setPolygonAttributes(attributes);
        objRoot.addChild(new Shape3D(createGeometry(), appearance));
 
        objRoot = addMouseRotateBehavior(objRoot);
        objRoot.compile();
        simpleU.addBranchGraph(objRoot);
    }
 
    private Geometry createGeometry() {
        int nx = 21 * 2 - 1;
        int ny = 21 * 2 - 1;
        Point3f coords[] = new Point3f[nx * ny];
        float z;
        int count = 0;
        for (float y = -10f; y <= 10f; y += 0.5f) {
            for (float x = -10f; x <= 10f; x += 0.5f) {
                if (x == 0f && y == 0f) {  // 這點, 與眾不同, 要特別處理.
                    z = 1f; // (for n --> 0), lim (sin(n) / n) --> 1
                } else {
                    double r = Math.sqrt(x * x + y * y);
                    z = (float) (Math.sin(r) / r);
                }
                coords[count++] = new Point3f(x/10f, y/10f, z);
            }
        }
        int[] indices = new int[(nx-1) * (ny-1) * 4];
        count = 0;
        for (int y = 0, endY = ny - 1; y < endY; y++) {
            for (int x = 0, endX = nx - 1; x < endX; x++) {
                int v = nx * y + x;
                indices[count++] = v;
                indices[count++] = v + 1;
                v += nx;
                indices[count++] = v + 1;
                indices[count++] = v;
            }
        }
 
        IndexedQuadArray iqa = new IndexedQuadArray(coords.length, IndexedQuadArray.COORDINATES, indices.length);
        iqa.setCoordinates(0, coords);
        iqa.setCoordinateIndices(0, indices);
        return iqa;
    }
 
    private BranchGroup addMouseRotateBehavior(BranchGroup child) {
        BranchGroup objRoot = new BranchGroup();
        TransformGroup objRotate = new TransformGroup();
        objRotate.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
        objRotate.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
        objRoot.addChild(objRotate);
        objRotate.addChild(child);
 
        MouseRotate myMouseRotate = new MouseRotate();
        myMouseRotate.setTransformGroup(objRotate);
        myMouseRotate.setSchedulingBounds(new BoundingSphere());
        objRoot.addChild(myMouseRotate);
        return objRoot;
    }
}


(縮略圖,點擊圖片鏈接看原圖)


caterpillar edited on 2004-07-16 13:15
reply to postreply to post
話題樹型展開
人氣 標題 作者 字數 發文時間
25435 [圖像]3D函數畫圖 T55555 3949 2004-05-06 04:04
21237 Re:函數畫圖 T55555 28 2004-05-06 04:05
20326 Re:[圖像]3D函數畫圖 aixy 1351 2004-08-19 17:01
19812 Re:[圖像]3D函數畫圖 T55555 207 2004-08-19 21:02
19223 Re:[圖像]3D函數畫圖 supergaosong 226 2004-10-20 16:16
17126 Re:[圖像]3D函數畫圖 jc 155 2005-12-27 19:41
16892 Re:[圖像]3D函數畫圖 T55555 410 2005-12-27 22:15
16345 Re:[圖像]3D函數畫圖 一肩风雪 239 2006-03-07 11:47
18055 Re:[圖像]3D函數畫圖 T55555 425 2006-03-07 22:40
12739 Re:[圖像]3D函數畫圖 CasperLiu 1319 2008-01-07 17:14
6133 j3d如何學習 JOJO10098 39 2011-03-28 23:28
6107 Re:j3d如何學習 T55555 394 2011-03-31 00:02
6011 Re:[圖像]3D函數畫圖 JOJO10098 1431 2011-03-30 18:49
20940 Re:[工具]函數畫圖 T55555 783 2004-05-06 05:22
11001 Re:[工具]函數畫圖 paul760509 55 2008-07-05 15:46
11717 Re:[工具]函數畫圖 T55555 4085 2008-07-08 04:23
8171 Re:[工具]函數畫圖 madmadl 56 2009-06-01 11:56
21040 Re:[工具]函數畫圖 T55555 65 2004-05-06 05:07
20702 Re:[工具]函數畫圖 T55555 32 2004-05-06 05:08
22054 Re:[工具] 3D函數畫圖 T55555 4829 2004-05-29 03:30
20192 Re:[工具] 3D函數畫圖 T55555 15 2004-05-29 03:33
20195 Re:[工具] 3D函數畫圖 T55555 20 2004-05-29 03:35
20202 Re:[工具] 3D函數畫圖 T55555 7 2004-05-29 03:41
20615 Re:[工具] 3D函數畫圖 T55555 247 2004-05-29 03:50
20098 Re:[工具] 3D函數畫圖 worookie 0 2004-05-29 03:55
20209 Re:[工具] 3D函數畫圖 T55555 473 2004-05-29 04:16
20713 Re:[工具] 3D函數畫圖 T55555 5308 2004-08-17 02:06
12654 Re:[工具] 3D函數畫圖 CasperLiu 36 2007-12-26 17:31
» JWorld@TW »  Java 程式分享區 » Image

reply to postflat modego to previous topicgo to next topic
  已讀文章
  新的文章
  被刪除的文章
Jump to the top of page

JWorld@TW 本站商標資訊

Powered by Powerful JuteForum® Version Jute 1.5.8