2007-03-18 Jeffrey Stedfast <fejj@gnome.org>

* DemoLib/Vector.cs: Added more special vectors
	(x2y2z2): Renamed to LengthSquared
	(Cross): Fixed a 3am typo that caused the cross-product methods to
	be broken
	(Angle): Added a static version of this method
	(Normal): Return this if the length is 1
	(Normalize): No-op if the length is 1



svn path=/trunk/DemoLib/; revision=74585
This commit is contained in:
Jeffrey Stedfast 2007-03-19 03:12:38 +00:00
Родитель 863de2ffa5
Коммит cfed690ef2
3 изменённых файлов: 42 добавлений и 16 удалений

Просмотреть файл

@ -0,0 +1,10 @@
2007-03-18 Jeffrey Stedfast <fejj@gnome.org>
* DemoLib/Vector.cs: Added more special vectors
(x2y2z2): Renamed to LengthSquared
(Cross): Fixed a 3am typo that caused the cross-product methods to
be broken
(Angle): Added a static version of this method
(Normal): Return this if the length is 1
(Normalize): No-op if the length is 1

Просмотреть файл

@ -21,8 +21,15 @@ using System.Text;
namespace DemoLib { namespace DemoLib {
public class Vector : ICloneable { public class Vector : ICloneable {
#region Special Vectors
public static readonly Vector Backward = new Vector (0.0f, 0.0f, -1.0f);
public static readonly Vector Forward = new Vector (0.0f, 0.0f, 1.0f);
public static readonly Vector Right = new Vector (1.0f, 0.0f, 0.0f);
public static readonly Vector Left = new Vector (-1.0f, 0.0f, 0.0f);
public static readonly Vector Down = new Vector (0.0f, -1.0f, 0.0f);
public static readonly Vector Zero = new Vector (0.0f, 0.0f, 0.0f); public static readonly Vector Zero = new Vector (0.0f, 0.0f, 0.0f);
public static readonly Vector Up = new Vector (0.0f, 1.0f, 0.0f); public static readonly Vector Up = new Vector (0.0f, 1.0f, 0.0f);
#endregion
public float[] v; public float[] v;
@ -72,23 +79,21 @@ namespace DemoLib {
set { v[3] = value; } set { v[3] = value; }
} }
float x2y2z2 { float LengthSquared {
get { return (x * x) + (y * y) + (z * z); } get { return (x * x) + (y * y) + (z * z); }
} }
// Length of this Vector // Length of this Vector
public float Length { public float Length {
get { get { return (float) Math.Sqrt (LengthSquared); }
return (float) Math.Sqrt ((double) (x2y2z2));
}
} }
// A new Normal Vector representative of this Vector // A Normal Vector representative of this Vector
public Vector Normal { public Vector Normal {
get { get {
float len = this.Length; float len = Length;
if (len == 0.0f) if (len == 0.0f || len == 1.0f)
return this; return this;
return new Vector (x / len, y / len, z / len, w); return new Vector (x / len, y / len, z / len, w);
@ -98,7 +103,7 @@ namespace DemoLib {
// Angle between this Vector and the X-Axis // Angle between this Vector and the X-Axis
public float AngleX { public float AngleX {
get { get {
float n = x2y2z2; float n = LengthSquared;
return (float) Math.Acos (x / Math.Sqrt ((double) (n * n))); return (float) Math.Acos (x / Math.Sqrt ((double) (n * n)));
} }
@ -107,7 +112,7 @@ namespace DemoLib {
// Angle between this Vector and the Y-Axis // Angle between this Vector and the Y-Axis
public float AngleY { public float AngleY {
get { get {
float n = x2y2z2; float n = LengthSquared;
return (float) Math.Acos (y / Math.Sqrt ((double) (n * n))); return (float) Math.Acos (y / Math.Sqrt ((double) (n * n)));
} }
@ -116,7 +121,7 @@ namespace DemoLib {
// Angle between this Vector and the Z-Axis // Angle between this Vector and the Z-Axis
public float AngleZ { public float AngleZ {
get { get {
float n = x2y2z2; float n = LengthSquared;
return (float) Math.Acos (z / Math.Sqrt ((double) (n * n))); return (float) Math.Acos (z / Math.Sqrt ((double) (n * n)));
} }
@ -130,9 +135,9 @@ namespace DemoLib {
// Normalize this Vector // Normalize this Vector
public void Normalize () { public void Normalize () {
float len = this.Length; float len = Length;
if (len == 0.0f) if (len == 0.0f || len == 1.0f)
return; return;
x /= len; x /= len;
@ -143,8 +148,17 @@ namespace DemoLib {
// Angle between this Vector and another Vector // Angle between this Vector and another Vector
public float Angle (Vector vector) { public float Angle (Vector vector) {
float dot = this.Dot (vector); float dot = this.Dot (vector);
float n1 = vector.x2y2z2; float n1 = vector.LengthSquared;
float n0 = x2y2z2; float n0 = LengthSquared;
return (float) Math.Acos (dot / Math.Sqrt ((double) (n0 * n1)));
}
// Angle between 2 vectors
public static float Angle (Vector v0, Vector v1) {
float n0 = v0.LengthSquared;
float n1 = v1.LengthSquared;
float dot = v0.Dot (v1);
return (float) Math.Acos (dot / Math.Sqrt ((double) (n0 * n1))); return (float) Math.Acos (dot / Math.Sqrt ((double) (n0 * n1)));
} }
@ -190,7 +204,7 @@ namespace DemoLib {
// Cross product of 2 Vectors // Cross product of 2 Vectors
public Vector Cross (Vector vector) { public Vector Cross (Vector vector) {
return new Vector ((y * vector.z) - (z * vector.y), return new Vector ((y * vector.z) - (z * vector.y),
(z * vector.z) - (z * vector.x), (z * vector.x) - (x * vector.z),
(x * vector.y) - (y * vector.x)); (x * vector.y) - (y * vector.x));
} }
@ -202,7 +216,7 @@ namespace DemoLib {
// Cross product of 2 Vectors // Cross product of 2 Vectors
public static Vector Cross (Vector v0, Vector v1) { public static Vector Cross (Vector v0, Vector v1) {
return new Vector ((v0.y * v1.z) - (v0.z * v1.y), return new Vector ((v0.y * v1.z) - (v0.z * v1.y),
(v0.z * v1.z) - (v0.z * v1.x), (v0.z * v1.x) - (v0.x * v1.z),
(v0.x * v1.y) - (v0.y * v1.x)); (v0.x * v1.y) - (v0.y * v1.x));
} }

Просмотреть файл

@ -64,6 +64,8 @@ namespace DemoLib {
this.Name = "DemoLib.ScreenResolution"; this.Name = "DemoLib.ScreenResolution";
this.Title = Mono.Unix.Catalog.GetString("Settings"); this.Title = Mono.Unix.Catalog.GetString("Settings");
this.Icon = Gtk.IconTheme.Default.LoadIcon("stock_3d-colors", 16, 0); this.Icon = Gtk.IconTheme.Default.LoadIcon("stock_3d-colors", 16, 0);
this.WindowPosition = ((Gtk.WindowPosition)(4));
this.HasSeparator = false;
// Internal child DemoLib.ScreenResolution.VBox // Internal child DemoLib.ScreenResolution.VBox
Gtk.VBox w1 = this.VBox; Gtk.VBox w1 = this.VBox;
w1.Events = ((Gdk.EventMask)(256)); w1.Events = ((Gdk.EventMask)(256));