Reputation: 3
I use Vector3D.Normalize()
in C# to compare different vectors.
In some cases it did not work.
Therefore I made a simple sample just to show (not best coding).
How can I overcome this without using additional vectors like in Test2
?
using System;
using System.Windows.Media.Media3D;
namespace Vecktor3D_Normalize_test
{
class Program
{
static void Main(string[] args)
{
Vektor myVek = new Vektor();
Vector3D myvector0 = new Vector3D(0.7, 0.5, 0.3);
myVek.Test2(myvector0);
Console.WriteLine("Test 2 " + myVek.myvector2);
myVek.Test3(myvector0);
Console.WriteLine("Test 3 " + myVek.myvector3);
Console.WriteLine("Test 0 " + myvector0);
myvector0.Normalize();
Console.WriteLine("Test 1 " + myvector0);
Console.ReadLine();
}
}
class Vektor
{
public Vector3D myvector2 { get; set; }
public Vector3D myvector3 { get; set; }
public void Test2(Vector3D Input)
{
Vector3D myvector4 = Input;
myvector4.Normalize(); // myvector4 is normalized !!!!!!
myvector2 = myvector4;
}
public void Test3(Vector3D Input)
{
myvector3 = Input;
myvector3.Normalize(); // myvector3 is not normalized !!!!!!!
}
}
}
Upvotes: 0
Views: 706
Reputation: 15045
Vector3D
is a struct
, so by default it is passed by value.
This means myvector3.Normalize
only normalizes a copy of myvector3
obtained from its get
function, not myvector3
itself (or to be precise, the underlying hidden member variable which stores the value for the property myvector3
). Hence you must be careful when replacing member variables with properties.
You could instead call Normalize
on Input
, for the same reason:
Input.Normalize();
myvector3 = Input;
Upvotes: 1