Remove or insert items in an ‘fixed’ array like int[]

Remove or insert an item in an array.

Use the always useful Array class. It has some static members which are great!

int[] myArray = new int[4];
myArray [0] = 0;
myArray [1] = 1;
myArray [2] = 2;
myArray [3] = 3;

// Remove item 1
Array.Copy(myArray , 2, myArray , 1, 2);
// MyArray contains 0,2,3,3

myArray [0] = 0;
myArray [1] = 1;
myArray [2] = 2;
myArray [3] = 3;

// Insert new item in 1
Array.Copy(myArray , 1, myArray ,2, 2);
// MyArray contains 0,1,1,2

Note: If the bounds of the array are exceeded an exception will be thrown.

The functions below calculate the number of elements to copy.

They use the following arguments:

Array The array to use.
index The offset whereto insert or remove
delta The number of items to add / remove
totCount The number of items currently in the array.

To allow a correct insert the array must be larger than the current numbers (Arrays are not resized). Clearing is optional

void removeFromArray(Array array, int index, int delta, int totCount)
{
   Array.Copy(array, index + delta, array, index, totCount - (index + delta));
   Array.Clear(array, totCount - delta, delta);
}

void insertIntoArray(Array array, int index, int delta, int totCount)
{
   Array.Copy(array, index, array, index + delta, totCount - index);
   Array.Clear(array, index, delta);
}
Lastest update in May 2011, inital post in May 2011

The cross product

Formula:

Nx = ( dz1 * dy2 ) – (dy1 * dz2 );
Ny = ( dx1 * dz2 ) – (dz1 * dx2 );
Nz = ( dy1 * dx2 ) – (dx1 * dy2 );

dx1 is the delta between the two x coordinates (last – first) of vector V1.
dx2 is the delta between the two x coordinates (last – first) of vector V2.
dy1 is the delta between the two y coordinates (last – first) of vector V1.
dy2 is the delta between the two y coordinates (last – first) of vector V2.
dz1 is the delta between the two z coordinates (last – first) of vector V1.
dz2 is the delta between the two z coordinates (last – first) of vector V2.

The cross product calculated between two vectors results in a vector perpendicular to both vectors. The length of this vector is the surface of the parallelogram.

If the angle between the two vectors is 0 or 180 degrees the lengths is zero.  This can be used for vector overlap checks.

In 2D only the Nz value is used, it can be used for overlaps or parallel vectors etc.

Uses:

Are two 2D vectors parallel

Are two 2D vectors  coincident

Get the intersection point of two 2D vectors

Lastest update in May 2011, inital post in May 2011

The dot product

Formula:  dx1 * dx2 + dy1 * dy2 + dz1 * dz1

dx1 is the delta between the two x coordinates (last – first) of vector V1.
dx2 is the delta between the two x coordinates (last – first) of vector V2.
dy1 is the delta between the two y coordinates (last – first) of vector V1.
dy2 is the delta between the two y coordinates (last – first) of vector V2.
dz1 is the delta between the two z coordinates (last – first) of vector V1.
dz2 is the delta between the two z coordinates (last – first) of vector V2.

Check of 90 angles

The dot product is actually a representation of the cosine. When the internal vector of two vectors angle is 90 degrees the dot product is always zero.

Project point on vector

Project P on vector V1

Projection can be done using the dot product.

You can chose to normalize the vector, this is how I learned it.

Normalizing a vector requires you to make its length 1 but keeping its direction.

length = sqrt(  ((vx2 - vx1) * (vx2 - vx1)) + ((vy2 - vy1) * (vy2 - vy1))  +( (vz2 - vz1) * (vz2 - vz1)) );

vx2 = vx1 + (vx2 - vx1) / length;

vy2 = vy1 + (vy2 - vy1) / length;

vz2 = vz1 + (vz2 - vz1) / length;

Note: It is often more efficient to calculate the delta’s up front.

The dot vector can be used to project a point on a vector (line).

The result will be 3 * 1 + 2 * 0  + 0 * 0 = 3

Now vector |V1| multiplied with 3 will point to the projection point.

Alternative

Alternatively do not normalize the vector. In that case the result will be 3 * 4 + 2 * 0  + 0 * 0 = 12.

When you look at the original formula you can divide it by the length of V1 (4) to get the offset relatively to the |V1| vector.

To get the offset to the original vector divide by the length again. Which will give offset 1.0. Useful thing is you will save the square.

For a 2d vector and a point the formula becomes:

(vx2 – vx1) * (px -vx1) + (vy2 – vy1) * (py – vy1)

——————————————————-

((vx2 – vx1) *  (vx2 – vx1)) +  ( (vy2 – vy1) * (vy2 – vy1))

Lastest update in May 2011, inital post in May 2011

OutputDebugString replacement

To replace OutputDebugString API use the System.Diagnostics.Debug class.

For example:

System.Diagnostics.Debug.WriteLine("Debug message");
Lastest update in May 2011, inital post in May 2011

Get the send to folder

For the current user

System.Environment.GetFolderPath(Environment.SpecialFolder.SendTo));

For the all users

System.Environment.GetFolderPath(Environment.SpecialFolder.CommonSendTo));
Lastest update in May 2011, inital post in May 2011

Get the desktop folder

For the current user

System.Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory));

For the all users

System.Environment.GetFolderPath(Environment.SpecialFolder.CommonDesktopDirectory));
Lastest update in May 2011, inital post in May 2011

Get the template folder

For the current user

System.Environment.GetFolderPath(Environment.SpecialFolder.Templates));

For the all users

System.Environment.GetFolderPath(Environment.SpecialFolder.CommonTemplates));
Lastest update in May 2011, inital post in May 2011

Get the favorites folder

For the current user

System.Environment.GetFolderPath(Environment.SpecialFolder.Favorites));

For the all users

System.Environment.GetFolderPath(Environment.SpecialFolder.CommonFavorites));
Lastest update in May 2011, inital post in May 2011