32 using System.Globalization;
33 using System.Collections.Generic;
99 private static Vector FromLengthAndAngle(
double length,
double angle )
102 result.
X = length * Math.Cos( angle );
103 result.
Y = length * Math.Sin( angle );
112 return FromLengthAndAngle( length, angle.
Radians );
120 return FromLengthAndAngle( 1, angle.
Radians );
131 return MathHelper.Sqrt( (
float)(x * x + y * y) );
142 return left.
Y * right.
Y + left.
X * right.
X;
196 double minMagnitude = vectors[0].
Magnitude;
198 for (
int i = 1; i < vectors.Length; i++ )
200 double m = vectors[i].Magnitude;
201 if ( m < minMagnitude )
208 return vectors[minIndex];
219 double maxMagnitude = vectors[0].
Magnitude;
221 for (
int i = 1; i < vectors.Length; i++ )
223 double m = vectors[i].Magnitude;
224 if ( m > maxMagnitude )
231 return vectors[maxIndex];
245 foreach ( var v
in vectors )
265 foreach ( var v
in vectors )
271 return vectors.Length > 0 ?
new Vector( sumX, sumY ) / vectors.Length :
Vector.
Zero;
274 [Save]
public double X;
275 [Save]
public double Y;
290 get {
return X * X + Y *
Y; }
311 double a = Math.Atan2( Y, X );
324 return ToString( NumberFormatInfo.InvariantInfo );
327 public string ToString( IFormatProvider formatProvider )
329 string x = X.ToString( formatProvider );
330 string y = Y.ToString( formatProvider );
331 return string.Format(
"({0},{1})", x, y );
336 return Parse( vectorStr, NumberFormatInfo.InvariantInfo );
339 public static Vector Parse(
string vectorStr, IFormatProvider formatProvider )
341 string[] splitStr = vectorStr.Split(
'(',
',',
')' );
343 if ( splitStr.Length != 4 )
344 throw new FormatException(
"Not a vector string: " + vectorStr );
346 double x =
double.Parse( splitStr[1], formatProvider );
347 double y =
double.Parse( splitStr[2], formatProvider );
349 return new Vector( x, y );
355 result.
X = left.
X + right.
X;
356 result.
Y = left.
Y + right.
Y;
363 result.
X = left.
X - right.
X;
364 result.
Y = left.
Y - right.
Y;
371 result.
X = source.
X * scalar;
372 result.
Y = source.
Y * scalar;
378 return source * scalar;
386 result.
X = source.
X / scalar;
387 result.
Y = source.
Y / scalar;
394 result.
X = -source.
X;
395 result.
Y = -source.
Y;
404 public override bool Equals(
object obj )
413 else if ( obj is Vector2D )
415 x = ( (Vector2D)obj ).X;
416 y = ( (Vector2D)obj ).Y;
418 else if ( obj is Vector3D )
420 x = ( (Vector3D)obj ).X;
421 y = ( (Vector3D)obj ).Y;
423 else if ( obj is Vector4D )
425 x = ( (Vector4D)obj ).X;
426 y = ( (Vector4D)obj ).Y;
433 return ( Math.Abs(
this.X - x ) <
float.Epsilon ) && ( Math.Abs(
this.Y - y ) <
float.Epsilon );
435 return ( Math.Abs(
this.X - x ) <
double.Epsilon ) && ( Math.Abs(
this.Y - y ) <
double.Epsilon );
443 return ( Math.Abs( left.
X - right.
X ) <
float.Epsilon ) && ( Math.Abs( left.
Y - right.
Y ) <
float.Epsilon );
445 return ( Math.Abs( left.
X - right.
X ) <
double.Epsilon ) && ( Math.Abs( left.
Y - right.
Y ) <
double.Epsilon );
451 return !( left == right );
454 public static explicit operator Vector( Vector2D v )
456 return new Vector( v.X, v.Y );
459 public static explicit operator Vector2D(
Vector v )
461 return new Vector2D( v.
X, v.
Y );
464 public static explicit operator Vector( Microsoft.Xna.Framework.Vector2 v )
466 return new Vector( v.X, v.Y );
469 public static explicit operator Microsoft.Xna.Framework.Vector2(
Vector v )
471 return new Microsoft.Xna.Framework.Vector2( (
float)v.
X, (
float)v.
Y );
474 public static implicit
operator Vector( Microsoft.Xna.Framework.Vector3 v )
479 public static implicit
operator Microsoft.Xna.Framework.Vector3(
Vector v )
481 return new Microsoft.Xna.Framework.Vector3( (
float)v.
X, (
float)v.
Y, 0 );
static Vector operator/(Vector source, double scalar)
static readonly Vector UnitX
Vaakasuuntainen yksikkövektori (pituus 1, suunta oikealle).
double Magnitude
Vektorin pituus.
static Vector operator+(Vector left, Vector right)
static Vector FromAngle(Angle angle)
Luo vektorin kulman perusteella yksikköpituudella.
Angle Angle
Kulma radiaaneina.
Suuntakulma (rajoitettu -180 ja 180 asteen välille) asteina ja radiaaneina. Tietoja kulmasta: http://...
static bool operator!=(Vector left, Vector right)
Apuluokka kaikille olioille
static readonly Vector UnitY
Pystysuuntainen yksikkövektori (pituus 1, suunta ylös).
static bool operator==(Vector left, Vector right)
override string ToString()
Vektori merkkijonona muodossa (x,y)
static readonly Vector Zero
Nollavektori.
static int GetHashCode(params object[] objects)
Palauttaa hajautuskoodin usean olion kokoelmalle.
override bool Equals(object obj)
Vector Project(Vector to)
static Vector Average(params Vector[] vectors)
Palauttaa kahden tai useamman vektorin keskiarvon.
static Vector Average(IEnumerable< Vector > vectors)
Palauttaa kahden tai useamman vektorin keskiarvon.
static Vector Parse(string vectorStr)
double ScalarProjection(Vector unitVector)
static Vector Min(params Vector[] vectors)
Palauttaa lyhimmän vektorin.
static Vector FromLengthAndAngle(double length, Angle angle)
Luo vektorin pituuden ja kulman perusteella.
static double CrossProduct(Vector left, Vector right)
Ristitulo. Palauttaa kohtisuoraan vektoreita vastaan olevan uuden vektorin pituuden. Tuloksen merkki kertoo kumpaan suuntaan vektori osoittaa.
Vector LeftNormal
Vasen normaali.
static Angle FromRadians(double radian)
Luo kulman annettujen radiaanien mukaan.
static double DotProduct(Vector left, Vector right)
Pistetulo.
static readonly Vector Diagonal
Diagonaalivektori (1,1)
double Radians
Palauttaa tai asettaa kulman radiaaneina.
static Vector Max(params Vector[] vectors)
Palauttaa pisimmän vektorin.
static double Distance(Vector p1, Vector p2)
Etäisyys kahden pisteen välillä.
static Vector Parse(string vectorStr, IFormatProvider formatProvider)
double MagnitudeSquared
Vektorin pituuden neliö.
string ToString(IFormatProvider formatProvider)
static Vector operator*(Vector source, double scalar)
Vector(double X, double Y)
Luo uuden vektorin komponenteista.
static Vector operator-(Vector left, Vector right)
Vector Normalize()
Palauttaa uuden vektorin, jonka suunta pysyy samana, mutta pituudeksi tulee 1.0.
override int GetHashCode()
Vector RightNormal
Oikea normaali.
static Vector ComponentProduct(Vector a, Vector b)
Kertoo kaksi vektoria komponenteittain.