Correct me if I am wrong.
I think the implementation is wrong.
There are little gaps in the build in math declarations,
specifically implicit promotions of ints to floats.
If public is dangerous, consider private in a class.
I got close with these. The commented code block is not compiling
but it should.
public static Vertex operator / ( Vertex a , Vertex b )
{
Vertex c = new Vertex ( );
c . x = a . x / b . x;
c . y = a . y / b . y;
c . z = a . z / b . z;
return c;
}
public static Vertex operator / ( Vertex a , float b )
{
Vertex c = new Vertex ( );
c . x = a . x / b;
c . y = a . y / b;
c . z = a . z / b;
return c;
}
public static Vertex operator / ( Vertex a , int b )
{
Vertex c = new Vertex ( );
c . x = a . x / b;
c . y = a . y / b;
c . z = a . z / b;
return c;
}
public static Vertex operator + ( Vertex a , Vertex b )
{
Vertex c = new Vertex ( );
c . x = a . x + b . x;
c . y = a . y + b . y;
c . z = a . z + b . z;
return c;
}
public static implicit operator float[] ( Vertex a )
{
float [ ] f = new float [ 3 ];
f [ 0 ] = a . x;
f [ 1 ] = a . y;
f [ 3 ] = a . z;
return f;
}
//public dynamic float [ ] operator / ( float[] a, int b )
// {
// float [ ] c = new float [ a . Count ( ) ];
// for ( int i = 0 ; i < a . Count ( ) ; i++ )
// {
// c[i] += a [ i ] / b;
// }
// return c;
// }
float [ ] NewCentroidf3 ( Vertex v0 , Vertex v1 )
{
float[] C = new float[3];
C = v0 + v1;
//C /= 2.0f;
//C = C / 2; // would work if above worked.
C [ 0 ] /= 2.0f;
C [ 1 ] /= 2.0f;
C [ 2 ] /= 2.0f;
return C;
}
We are considering adding "extension operators" to a hypothetical future version of C#, which would solve your problem. Straw poll: do any of you have AWESOME scenarios for this feature? The more awesome REALISTIC scenarios we can get, the more likely a feature is to be implemented some day. Send 'em my way; you can use the email link on my blog. – Eric Lippert Nov 6 '09 at 15:15