maths

  • Thread starter Thread starter David Sobey
  • Start date Start date
D

David Sobey

hi everyone

i'm looking to make a program that not only implements some fancy control
stuff with GDI+, but does some tricky maths using polymorphic binary data
trees, matrices, etc. Now I've gotten a really bad impression of C#
regarding its suitability for this kinda thing, ie no typedef,making matrix
data type definitions and binary trees a pain. The only custom "data types"
i can define is structs, which are really watered down classes, which isn't
highly suitable. Should i stick to C++ and import it into c#?

cheers
dave
--
"Aristotle said that some people were
only fit to be slaves. I do not
contradict him. But I reject slavery
becase I see no people fit to be masters"
C.S Lewis
 
You could, if you're more comfortable with that...
However C#'s main advantage over C++ for this kind of thing is not having to
worry about cleaning up things that you "new".
Best bet is think of something specific, how you'd code it in C++, and post
that - and I bet someone can come up with a just as clean method of coding it
in C#.
 
well for example how would i define a matrix data type, and so it is
available to other source files. In C or C++ it's something like

typedef int[][] matrix;

easy as pie. But in C#?

cheers
dave
 
This do not have complete functionality, Rotate method is not implemented,
but should give you a quick start:

<code>
/// <summary>
/// Stores a 3 row by 3 column matrix representing a geometric transform.
/// Not inheritable.
/// </summary>
public sealed
class AffineMatrix
{

/// <summary>
/// Creates an <code>AffineMatrix</code> with the elements of the identity
matrix.
/// </summary>
public AffineMatrix ( )
: this ( new float [0x6] { 1F, 0F, 0F, 1F, 0F, 0F } )
{
}

/// <summary>
/// Creates an <code>AffineMatrix</code> with the specified elements.
/// </summary>
/// <param name="m11">The value in the first row and first column.</param>
/// <param name="m12">The value in the first row and second
column.</param>
/// <param name="m21">The value in the second row and first
column.</param>
/// <param name="m22">The value in the second row and second
column.</param>
/// <param name="m31">The value in the third row and first column.</param>
/// <param name="m32">The value in the third row and second
column.</param>
public AffineMatrix
( float m11, float m12, float m21, float m22, float m31, float m32 )
: this ( new float [0x6] { m11, m12, m21, m22, m31, m32 } )
{
}

private AffineMatrix ( float [] matrix )
{
this.matrix = matrix;
}

~AffineMatrix ( )
{
matrix = null;
}


#region Internal Members

/// <summary>
/// The six-element array that represent this <code>AffineMatrix</code>.
/// </summary>
internal float [] matrix = null;

#endregion


/// <summary>
/// Gets a six-element array that represent this
<code>AffineMatrix</code>.
/// </summary>
public float [] Elements
{
get
{
return matrix;
}
}

/// <summary>
/// Gets the current translation on the horizontal axis.
/// </summary>
public float OffsetX
{
get
{
return matrix [0x4];
}
}

/// <summary>
/// Gets the current translation on the vertical axis.
/// </summary>
public float OffsetY
{
get
{
return matrix [0x5];
}
}

/// <summary>
/// Gets a value indicating whether this <code>AffineMatrix</code>
/// equals the identity matrix.
/// </summary>
public bool IsIdentity
{
get
{
return 1F == matrix [0x0] && 0F == matrix [0x1]
&& 0F == matrix [0x2] && 1F == matrix [0x3]
&& 0F == matrix [0x4] && 0F == matrix [0x5];
}
}


/// <summary>
/// Resets this <code>AffineMatrix</code> to have the elements of the
identity matrix.
/// </summary>
public void Reset ( )
{
matrix [0x5]
= matrix [0x4]
= matrix [0x2]
= matrix [0x1]
= 0F;
matrix [0x3]
= matrix [0x0]
= 1F;
}

/// <summary>
/// Offsets the coordinates of this <code>AffineMatrix</code>.
/// </summary>
/// <param name="dx">Specifies the horizontal translation.</param>
/// <param name="dy">Specifies the vertical translation.</param>
public void Translate ( float dx, float dy )
{
matrix [0x4] = dx;
matrix [0x5] = dy;
}

/// <summary>
/// Applies a scale vector to this <code>AffineMatrix</code>.
/// </summary>
/// <param name="sx">Specifies the horizontal scale factor.</param>
/// <param name="sy">Specifies the vertical scale factor.</param>
public void Scale ( float sx, float sy )
{
matrix [0x0] = sx;
matrix [0x3] = sy;
}

/// <summary>
/// Applies a rotation to this <code>AffineMatrix</code>.
/// </summary>
/// <param name="sx">Specifies the horizontal scale factor.</param>
/// <param name="sy">Specifies the vertical scale factor.</param>
public void Rotate ( float sx, float sy )
{
/*
Rotations are produced by [cos 0 sin 0 -sin 0 cos 0 0 0], which has the
effect
of rotating the coordinate system axes by an angle 0 counterclockwise.
*/
// matrix [0x0] = sx;
// matrix [0x3] = sy;
throw new System.NotImplementedException();
}

/// <summary>
/// Creates an exact copy of this <code>AffineMatrix</code>.
/// </summary>
/// <returns>An instance copy.</returns>
public AffineMatrix Clone ( )
{
return new AffineMatrix( new float [0x6]
{ matrix [0x0], matrix [0x1]
, matrix [0x2], matrix [0x3]
, matrix [0x4], matrix [0x5] } );
}


}

</code>


--
Regards,
Dennis JD Myrén
Oslo Kodebureau
David Sobey said:
well for example how would i define a matrix data type, and so it is
available to other source files. In C or C++ it's something like

typedef int[][] matrix;

easy as pie. But in C#?

cheers
dave
 
You just don't use a typedef.
If you wanted to *use* such a matrix in C++, you could do
matrix thematrix;

while in C#, you'd just do
int[][] thematrix;

it's only one character more to type!

All a typedef does in C++ is an instruction to the preprocessor. It's not
actually adding another datatype to the compiler, you're just saying
"wherever I use the type 'matrix', just make it an int[][] ."

I don't really see why you're stuck on the idea that you *need* to use a
typedef...
 
typedefs ensure type uniformity and can become almost critical in complex
mathematical programming. Can you imagine a program like matlab made without
custom data type definitions?
 
Back
Top