Need help reducing an equation, math wiz's welcome. =)

This is a discussion on Need help reducing an equation, math wiz's welcome. =) within the C++ Programming forums, part of the General Programming Boards category; Heya guys, I've been busy! I've been cranking out simple 3D C++ apps that my girlfriend's kids love. I made ...

Need help reducing an equation, math wiz's welcome. =)

Heya guys, I've been busy! I've been cranking out simple 3D C++ apps that my girlfriend's kids love. I made a pong clone and now I'm working on a maze game, etc etc. Lots of fun.
Here's the trouble! 3D graphics = math. I'm a better programmer than arithmetician.

What it does? Takes the x and y coordinates from OpenGL's "glutMotionFunc" - and translates them to object space coordinates some -30Z units in the distance. The x and y are serialized into a linear one dimensional array of 1,400 3-float structures. This array can be saved and loaded from disk like lightning, which is hot... but then getting it back into X and Y (adjusted from two positive coordinates (gotten by a modulus 50, and a subsequent divide 50) into a 0,0 signed coored.... omg... I digress.

I can always post some source. I'm NOT gonna do this way next time, and I learned alot. But man, 3 days to get this solution. The key was finally inverting the Y coord at the end of the process. Anyhow... relevant code for the sick freaks like me:

By the way, I've actually improved in skill, not all my code is wierd and convoluted - here's a templated function that takes a one dimentional array, it's size in elements, and a string filename - and saves it to a file! And the read one too.

Note: I use this in my custom *.hpp toolbox header, so the names are qualified.

Second I would move the calculation from inside the brackets [] to a separate variable, so this variable's value can be tested to insure you do not access the array out of bounds.

Third you can probably remove the C-style float casts from the equation, you already have at least one floating point number in the calculation. I would probably break this calculation up into smaller equations, handling the x and y calculations separately. By breaking the equation up, if you have problems you can display these individual variables to insure that your calculations are correct. It also appears that the -25 and +25 cancel each other out and are not needed.

This is about what I would do for your calculation. I would then test indexValue to insure that it is in bounds for your array. I would also consider changing these "magic" constants to constant variables with meaningful names to help explain what you are trying to accomplish in these calculations.

Right! Sorry Jim, the board is an array of structures. Lemme dig it out:

Code:

struct mrtcolor {
float r;
float g;
float b;
};

And the array:

Code:

mrtcolor board[1400];

But, that aside, your simplification is very nice, and when you advise to get rid of the c-style casts, you mean go with like:
[ reinterpret_cast <new_type> (expression) static_cast <new_type> (expression) const_cast <new_type> (expression) ]
Your advice prompted me to learn about these, thank you!

As for vector, I -had- a vector solution, but ran into trouble getting boost libraries to link to my project. Hence no boost::serialization, hence vectors are not so easy to get into binary files. That board[1400] array is saved to a binary file, and reloaded.

If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.