Hi Baris,
I think there is a much more efficient way to translate points.
You have a few redundancies with what you are doing:
- For every vector you create a new Transform struct even though you already have a vectors as inputs.
- You have to create another List to store the translated points to be able to output them, even though your input is already a list of points…
Imagine if you are dealing with thousands or millions of points and vectors, this will be a huge amount of unnecessary memory usage
if you just do this, it will work:
for (int i = 0; i < pts.Count; i++)
{
pts[i] += vecs[i];
}
A = pts;
So any class, interface, dynamic, and delegate in C# is a reference type
this means that they contain a “pointer” to a another memory location in your computer. What this basically means is that when you assign a copy of a class to a variable and you modify the newly created variable you will end up modifying the values of both objects.
// Lets say we have a class named Foo
public class Foo
{
public int value;
public Foo(int val)
{
value = val;
}
}
// lets assume this is our main program
Foo a = new Foo(2);
// We create a new Foo object and we assign the previous object to it
// b points to the same memory address of a
// in essence they are the same thing
Foo b = a;
b.value = 5;
// if we would check the results of this
Print(a.value.ToString();
Print(b.value.ToString();
// we would get 5 for both objects. this is because both variables point to the same address in memory
// 5
// 5
Structs
are the opposite, they contain the address of where they are located in memory not a reference to it. So if you try to copy a struct in a new variable what is actually happening is that an independent memory address is created to store that variable This causes that any change that you do on this copied version will not affect your original object because you are not operating on the same data any longer.
// lets take a known struct as an example
Point3d p1 = new Point3d(0,0,0);
// p2 now has an independent memory address
// making it different than p1
Point3d p2 = p1;
p2.X = 10;
// check the results
Print(p1.ToString());
Print(p2.ToStrint());
//ouputs will be
0
10
Structs
also become copied when are passed in to methods
public void Test( int num)
{
num+=1;
}
// Main program
int a = 10;
Test(a);
Print(a.ToString());
// output
10
But if they are passed in a method with a little keyword named ref
they will get passed as a reference type
public void Test( ref int num)
{
num+=1;
}
// Main program
int a = 10;
Test(a);
Print(a.ToString());
// output
11
So this
for (int i = 0; i < pts.Count; i++)
{
pts[i] += vecs[i];
}
A = pts;
works because you are operating on a List
which is defined as a class in C# (meaning a reference type
) So when you are modifying all the elements in the list you are changing the data itself not a copy.
As an exercise, you can create a struct
and define one of its properties to be a reference type. Then create an instance of the struct and assign that instance to another variable. After this, try changing the value of the reference type property, what do you think will happen?