2 mathematical / class object questions

This is a discussion on 2 mathematical / class object questions within the C++ Programming forums, part of the General Programming Boards category; You might want to look at this matrix header written by me. It checks the dimensions (in multiplication, for example) ...

  1. #16
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    You might want to look at this matrix header written by me.
    • It checks the dimensions (in multiplication, for example) at compile-time instead of run-time
    • it doesn't use dynamic memory.
    Attached Files Attached Files
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  2. #17
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    Well since DirectX and about a ton of other graphics APIs including OpenGL use matrices to do 3D graphics, I'm fairly sure that they are not where the slow-down in code is going to be. OpenGL and I think DX create a 1 dimensional 4x4 matrix - like I showed before - and then concatenate them .

    Matrix multiplies are not slow and I can't think of anything that would be faster in pure asm than accessing a linear array. All values could be stored using a stosd esp. if they are floats. Not sure what the big deal here is.

  3. #18
    The Defective GRAPE Lurker's Avatar
    Join Date
    Feb 2003
    Posts
    949
    I have already created a matrix class using my system, but it isn't for any specific project (yet). It is a general class, designed to do whatever it is needed to do. Doesn't mean it's the only matrix class I'll ever create, however .
    Do not make direct eye contact with me.

  4. #19
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Originally posted by Bubba
    Well since DirectX and about a ton of other graphics APIs including OpenGL use matrices to do 3D graphics, I'm fairly sure that they are not where the slow-down in code is going to be. OpenGL and I think DX create a 1 dimensional 4x4 matrix - like I showed before - and then concatenate them .
    Depends on the size of the matrix... Calculating determinants and eigenvectors/eigenvalues as one might do in a scientific app (on large matrices) is umm... generally quite slow.

    Though I agree that a linear array is the way to go. You might want to look at making the size a template parameter as well. Then you'd know size constraints, etc. at compile-time.

    Cheers
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  5. #20
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,669
    Originally posted by Zach L.
    You might want to look at making the size a template parameter as well. Then you'd know size constraints, etc. at compile-time.
    You could do something like that this way:
    Code:
    template<int rows, int cols, typename T=double>
    As for matricies vs. arrays - consider the following:
    Code:
    #include <iostream>
    #include <cstring>
    using namespace std;
    
    int main()
    {
        int matrix[3][3] = {{1,2,3},{4,5,6},{7,8,9}};
        int array[9] = {1,2,3,4,5,6,7,8,9};
    
        if (memcmp(matrix, array, 9*sizeof(int)) == 0)
            cout << "Same" << endl;
        else 
            cout << "Different" << endl;
    
        return 0;
    }//main
    gg

  6. #21
    Registered User grady's Avatar
    Join Date
    Oct 2003
    Posts
    27

    Re: 2 mathematical / class object questions

    #1.
    Code:
    union
    {
            struct
            {
                    float m11,m12,m13,m21,m22,m23,m31,m32,m33;
            };
            float m[3][3];
    };
    #2.
    You can round, but you can't round to the units place, else 0.5 =1. It is simpler to just write
    Code:
    bool eqflt(float a,float b,float tol)
    {
    	if(fabs(tol)>fabs(a-b)) return true;
    	return false;
    }
    
    if(eqflt(mag(vector),1.0f,0.00001f)) printf("normalized");

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Mmk, I give up, lets try your way. (Resource Management)
    By Shamino in forum Game Programming
    Replies: 31
    Last Post: 01-18-2006, 08:54 AM
  2. My Window Class
    By Epo in forum Game Programming
    Replies: 2
    Last Post: 07-10-2005, 02:33 PM
  3. pointer to struct object in class object
    By Stevo in forum C++ Programming
    Replies: 3
    Last Post: 02-25-2004, 06:58 PM
  4. Base class initialization
    By VirtualAce in forum C++ Programming
    Replies: 4
    Last Post: 01-11-2004, 03:52 AM
  5. Replies: 4
    Last Post: 03-20-2002, 01:31 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21