⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 face.java

📁 wince 5.0上基于personal java写的的虚拟魔方
💻 JAVA
字号:

/*
 * Generic 3D Model for Java Virtual Cube
 * --------------------------------------
 *
 * Copyright 1996, Song Li  
 * URL: http://www.cs.umbc.edu/~sli2
 *
 * Portion of Program by David W. Liu  
 * URL: http://reality.sgi.com/employees/davidliu_mti
 *
 * 
 * You can use the code for any nonprofittable use. But remember to mention
 * the authors' names in your revised program. You are also encouraged to
 * improve the program or give your comments and bug reports. If there are
 * further questions, please contact me and I'll be glad to help. My E-Mail
 * address is: sli2@gl.umbc.edu.
 *
 */


import java.awt.*;


class Face {
    public int    VerNum;
    public Vertex Vertices  [];
    public Color  FaceColor;
  
    private float  XMin, XMax;
    private float  YMin, YMax;
    private float  ZMin, ZMax;
  
    protected Vertex   ScreenVer [];
    protected int      ScreenX   [];
    protected int      ScreenY   [];
    protected boolean  Visible;
    protected float    Illuminator;

    protected static boolean needlight;
  
  
    public Face (int vernum) {
        VerNum    = vernum;
        Vertices  = new Vertex [vernum];
        ScreenX   = new int [vernum] ;
        ScreenY   = new int [vernum] ;
        ScreenVer = new Vertex [vernum];
        for (int i=0; i<vernum; i++)
            ScreenVer[i] = new Vertex () ;
    }
  

    public static void SetLight (boolean light) {
        needlight = light;
        return;
    }
    
  
    void CalcParam (Matrix3D viewmat) {
        viewmat.Transform (Vertices, ScreenVer, VerNum) ;
        Vector3D v1 = new Vector3D (ScreenVer[1], ScreenVer[2]);
        Vector3D v2 = new Vector3D (ScreenVer[1], ScreenVer[0]);
        Vector3D normal = Vector3D.crosspro (v1, v2);
        normal.Normalize ();
        Visible = normal.z > 0;
        Illuminator = 0.5f + 0.5f*normal.z;

        XMin = ScreenVer[0].x; XMax = XMin;
        YMin = ScreenVer[0].y; YMax = YMin;
        ZMin = ScreenVer[0].z; ZMax = ZMin;
  
        for (int i=1; i<VerNum; i++) {
            XMin = Math.min(XMin, ScreenVer[i].x);
            XMax = Math.max(XMax, ScreenVer[i].x);
            YMin = Math.min(YMin, ScreenVer[i].y);
            YMax = Math.max(YMax, ScreenVer[i].y);
            ZMin = Math.min(ZMin, ScreenVer[i].z);
            ZMax = Math.max(ZMax, ScreenVer[i].z);
        }
    }
  
  
 
    public final boolean CloserThan (Face face) {
        return (ZMax + ZMin < face.ZMax + face.ZMin);
    }


    public void Paint (Graphics graph, int width, int height) {
        if (! Visible)
            return;
            
        for (int i=0; i<VerNum; i++) {
            ScreenX [i] = (int) ScreenVer[i].x + width/2 ;
            ScreenY [i] = height/2 - (int) ScreenVer[i].y ;
            }

        Color color;
        if (needlight)
            color = new Color (
                (int) (Illuminator * FaceColor.getRed ()),
                (int) (Illuminator * FaceColor.getGreen ()),
                (int) (Illuminator * FaceColor.getBlue ()));
        else
            color = FaceColor; 
            
        graph.setColor (color);
        graph.fillPolygon (ScreenX, ScreenY, VerNum);
        graph.setColor (Color.black);
        graph.drawPolygon (ScreenX, ScreenY, VerNum);
    }



    public void Print () {
        System.out.println ("Face -- ") ;
        for (int i=0; i<VerNum; i++) {
            System.out.print ("Origin  ") ;
            Vertices[i].Print () ;
            System.out.print ("Screen  ") ;
            ScreenVer[i].Print () ;
            }
    }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -