Back to project page jmini3d.
The source code is released under:
Copyright 2012 Mobialia http://www.mobialia.com/ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to ...
If you think the Android project jmini3d listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package jmini3d; /*from w ww . j a va2s .com*/ public class Vector3 { public float x; public float y; public float z; private static Vector3 tmp = new Vector3(); public Vector3() { x = 0; y = 0; z = 0; } public Vector3(float x, float y, float z) { this.x = x; this.y = y; this.z = z; } public void setAll(float x, float y, float z) { this.x = x; this.y = y; this.z = z; } public void setAllFrom(Vector3 n) { x = n.x; y = n.y; z = n.z; } public void normalize() { float mod = (float) Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z); if (mod != 0 && mod != 1) { mod = 1 / mod; this.x *= mod; this.y *= mod; this.z *= mod; } } public void add(Vector3 n) { this.x += n.x; this.y += n.y; this.z += n.z; } public void subtract(Vector3 n) { this.x -= n.x; this.y -= n.y; this.z -= n.z; } public void multiply(float f) { this.x *= f; this.y *= f; this.z *= f; } public float length() { return (float) Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z); } public Vector3 clone() { return new Vector3(x, y, z); } public void rotateX(float angle) { float cosRY = (float) Math.cos(angle); float sinRY = (float) Math.sin(angle); tmp.setAll(this.x, this.y, this.z); this.y = (tmp.y * cosRY) - (tmp.z * sinRY); this.z = (tmp.y * sinRY) + (tmp.z * cosRY); } public void rotateY(float angle) { float cosRY = (float) Math.cos(angle); float sinRY = (float) Math.sin(angle); tmp.setAll(this.x, this.y, this.z); this.x = (tmp.x * cosRY) + (tmp.z * sinRY); this.z = (tmp.x * -sinRY) + (tmp.z * cosRY); } public void rotateZ(float angle) { float cosRY = (float) Math.cos(angle); float sinRY = (float) Math.sin(angle); tmp.setAll(this.x, this.y, this.z); this.x = (tmp.x * cosRY) - (tmp.y * sinRY); this.y = (tmp.x * sinRY) + (tmp.y * cosRY); } public void rotateAxis(Vector3 a, float angle) { float c = (float) Math.cos(angle); float s = (float) Math.sin(angle); float t = 1 - (float) Math.cos(angle); float nx = x * (t * a.x * a.x + c) + y * (t * a.x * a.y - s * a.z) + z * (t * a.x * a.z + s * a.y); float ny = x * (t * a.x * a.y + s * a.z) + y * (t * a.y * a.y + c) + z * (t * a.y * a.z - s * a.x); float nz = x * (t * a.x * a.z - s * a.y) + y * (t * a.y * a.z + s * a.x) + z * (t * a.z * a.z + c); x = nx; y = ny; z = nz; } public static Vector3 add(Vector3 a, Vector3 b) { return new Vector3(a.x + b.x, a.y + b.y, a.z + b.z); } public static Vector3 subtract(Vector3 a, Vector3 b) { return new Vector3(a.x - b.x, a.y - b.y, a.z - b.z); } public static Vector3 multiply(Vector3 a, Vector3 b) { return new Vector3(a.x * b.x, a.y * b.y, a.z * b.z); } public static Vector3 cross(Vector3 v, Vector3 w) { return new Vector3((w.y * v.z) - (w.z * v.y), (w.z * v.x) - (w.x * v.z), (w.x * v.y) - (w.y * v.x)); } public static void cross(Vector3 result, Vector3 v, Vector3 w) { result.setAll((w.y * v.z) - (w.z * v.y), (w.z * v.x) - (w.x * v.z), (w.x * v.y) - (w.y * v.x)); } public static float dot(Vector3 v, Vector3 w) { return (v.x * w.x + v.y * w.y + w.z * v.z); } @Override public boolean equals(Object o) { if (o == null) { return false; } if (!(o instanceof Vector3)) { return false; } return x == (((Vector3) o).x) && (y == ((Vector3) o).y) && (z == ((Vector3) o).z); } @Override public String toString() { return x + "," + y + "," + z; } }