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

📄 vector3.java

📁 使用stl技术,(还没看,是听说的)
💻 JAVA
字号:
/*  Ogre4J
    Copyright (C) 2002 macross

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or any later version.

    This library 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
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/
package org.ogre4j.math;

import java.io.Serializable;

/**
 * Vector3
 * 
 * @author Ivica Aracic <ivica.aracic@bytelords.de>
 */
public class Vector3 implements Serializable {
    
    public static final Vector3
        ZERO   = new Vector3(),
        UNIT_X = new Vector3(1,0,0),
        UNIT_Y = new Vector3(0,1,0),
        UNIT_Z = new Vector3(0,0,1);
    
    public float x, y, z;
    
    
    
    public Vector3() {
        this(0, 0, 0);
    }

    public Vector3(Vector3 other) {
        this(other.x, other.y, other.z);
    }
    
    public Vector3(float x, float y, float z) {
        this.x = x;
        this.y = y;
        this.z = z;
    }
    
    /**
     * Set values
     */
    public void set(float x, float y, float z) {
        this.x = x;
        this.y = y;
        this.z = z;
    }

    public void set(Vector3 src) {
        x = src.x;
        y = src.y;
        z = src.z;
    }

    public float lengthSquared() {
        return x * x + y * y + z * z;
    }

    public void translate(float x, float y, float z) {
        this.x += x;
        this.y += y;
        this.z += z;        
    }

    public Vector3 cross(Vector3 other) {
    
        return
            new Vector3(
                this.y * other.z - this.z * other.y,
                other.x * this.z - other.z * this.x,
                this.x * other.y - this.y * other.x    
            );
    }    
  
    public Vector3 negate() {
        return new Vector3(-x, -y, -z);
    }

    public Vector3 mult(float s) {
        return new Vector3(x*s, y*s, z*s);
    }

    public void multThis(float s) {
        x *= s;
        y *= s;
        z *= s;
    }
    
    public Vector3 add(Vector3 other) {        
        return new Vector3(x+other.x, y+other.y, z+other.z);
    }

    public void addThis(Vector3 other) {        
        x += other.x;
        y += other.y;
        z += other.z;
    }

    public Vector3 sub(Vector3 other) {        
        return new Vector3(x-other.x, y-other.y, z-other.z);
    }
    
    public float length() {
        return (float)Math.sqrt(lengthSquared());
    }

    public float fastLength() {
        return 
            (float)(Math.abs(x)+Math.abs(y)+Math.abs(z));
    }
    
    public Vector3 normalize() {
        float len = length();
        if (len != 0.0f) {
            float l = 1.0f / len;
            return mult(l);
        } else {
            return new Vector3(this);
        }
    }

    public float dotProduct(Vector3 other) {
        return x * other.x + y * other.y + z * other.z;
    }

    public float angle(Vector3 b) {
        float dls = dotProduct(b) / (length() * b.length());
        if (dls < -1f)
            dls = -1f;
        else if (dls > 1.0f)
            dls = 1.0f;
        return (float)Math.acos(dls);
    }

    public boolean isEqual(Vector3 other) {
        return x==other.x && y==other.y && z==other.z;
    }

    public String toString() {
        StringBuffer sb = new StringBuffer(64);
    
        sb.append("(");
        sb.append(x);
        sb.append(", ");
        sb.append(y);
        sb.append(", ");
        sb.append(z);
        sb.append(')');
        return sb.toString();
    }
    public float getX() {
        return x;
    }

    public float getY() {
        return y;
    }

    public float getZ() {
        return z;
    }

    public void setX(float f) {
        x = f;
    }

    public void setY(float f) {
        y = f;
    }

    public void setZ(float f) {
        z = f;
    }

}

⌨️ 快捷键说明

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