If you think the Android project min3d listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
Java Source Code
package com.min3d.lib.vos;
/*www.java2s.com*//**
* Simple VO holding x,y, and z values. Plus helper math functions.
* Care should be taken to avoid creating Number3d instances unnecessarily.
* Its use is not required for the construction of vertices.
*/publicclass Number3d
{
publicfloat x;
publicfloat y;
publicfloat z;
privatestatic Number3d _temp = new Number3d();
public Number3d()
{
x = 0;
y = 0;
z = 0;
}
public Number3d(float $x, float $y, float $z)
{
x = $x;
y = $y;
z = $z;
}
//
publicvoid setAll(float $x, float $y, float $z)
{
x = $x;
y = $y;
z = $z;
}
publicvoid setAllFrom(Number3d $n)
{
x = $n.x;
y = $n.y;
z = $n.z;
}
publicvoid 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;
}
}
publicvoid add(Number3d n)
{
this.x += n.x;
this.y += n.y;
this.z += n.z;
}
publicvoid subtract(Number3d n)
{
this.x -= n.x;
this.y -= n.y;
this.z -= n.z;
}
publicvoid multiply(Float f)
{
this.x *= f;
this.y *= f;
this.z *= f;
}
publicfloat length()
{
return (float) Math.sqrt( this.x*this.x + this.y*this.y + this.z*this.z );
}
public Number3d clone()
{
returnnew Number3d(x,y,z);
}
publicvoid rotateX(float angle)
{
float cosRY = (float) Math.cos(angle);
float sinRY = (float) Math.sin(angle);
_temp.setAll(this.x, this.y, this.z);
this.y = (_temp.y*cosRY)-(_temp.z*sinRY);
this.z = (_temp.y*sinRY)+(_temp.z*cosRY);
}
publicvoid rotateY(float angle)
{
float cosRY = (float) Math.cos(angle);
float sinRY = (float) Math.sin(angle);
_temp.setAll(this.x, this.y, this.z);
this.x = (_temp.x*cosRY)+(_temp.z*sinRY);
this.z = (_temp.x*-sinRY)+(_temp.z*cosRY);
}
publicvoid rotateZ(float angle)
{
float cosRY = (float) Math.cos(angle);
float sinRY = (float) Math.sin(angle);
_temp.setAll(this.x, this.y, this.z);
this.x = (_temp.x*cosRY)-(_temp.y*sinRY);
this.y = (_temp.x*sinRY)+(_temp.y*cosRY);
}
@Override
public String toString()
{
return x + "," + y + "," + z;
}
//
publicstatic Number3d add(Number3d a, Number3d b)
{
returnnew Number3d(a.x + b.x, a.y + b.y, a.z + b.z);
}
publicstatic Number3d subtract(Number3d a, Number3d b)
{
returnnew Number3d(a.x - b.x, a.y - b.y, a.z - b.z);
}
publicstatic Number3d multiply(Number3d a, Number3d b)
{
returnnew Number3d(a.x * b.x, a.y * b.y, a.z * b.z);
}
publicstatic Number3d cross(Number3d v, Number3d w)
{
returnnew Number3d((w.y * v.z) - (w.z * v.y), (w.z * v.x) - (w.x * v.z), (w.x * v.y) - (w.y * v.x));
}
publicstaticfloat dot(Number3d v, Number3d w)
{
return ( v.x * w.x + v.y * w.y + w.z * v.z );
}
// * Math functions thanks to Papervision3D AS3 library
// http://code.google.com/p/papervision3d/
}