|
Post by davy on Aug 28, 2006 17:28:28 GMT -5
Ok, so far I've been picking up C++ pretty well, but I recently ran into a snag... How would I go about returning an array as the result of a function?... I know how to say...
float Add(float a, float b) { return (a+b); }
Now... How could I make it return an array? Something along the lines of...
float Add(float a[2], float b[2]) { float c[2]; c[0]=a[0]+b[0]; c[1]=a[1]+b[1]; c[2]=a[2]+b[2]; return (c); }
|
|
|
Post by xenovacivus on Aug 28, 2006 18:42:24 GMT -5
Yeah, I had the same problem. I don't do it myself, but you could return the address of the array instead of the array itself. float c[3]; int Array (float x, float y, float z) { c[0] = x; c[1] = y; c[2] = z; return &c; }
I think thats the right way to do it... use "int" for the return type since an address is a 4 byte value (actually, you may have to use something different, just start throwing & and * signs in front of the "int" until it works ;D. That's how I usually do it...). Also, you need to declare the float[3] globally so the address always points to the same thing... then have a pointer variable and set it to the function. int &pointer; &pointer = Array (3, 4, 9);
Then the only problem is determining how big the array is. I'm pretty sure you could find out how many elements are in there using int elements = sizeof (pointer)/sizeof (float) // (would be 3 if the above is used).
Again, I'm not positive if the above would work accurately as is, but the concept should be sound. Since all that seemed like it'd make a giant mess before long, I made a vec3 class that works similar to vec3 in basic4gl. (actually, I made a whole matrix/vector class that has all sorts of operations like inverting matrices, dot products, cross products, etc.). Here's a stripped down version of the vec3 class-
class vec3 { public: float array [3];
// CONSTRUCTORS- // Default Constructor Sets Vec3 to (0, 0, 0), // Custom Constructor Sets Vec3 to (x, y, z). vec3 (){array[0]=0; array[1]=0; array[2]=0;} vec3 (float xIn, float yIn, float zIn) { array[0] = xIn; array[1] = yIn; array[2] = zIn; }
// Add/Subtract One Vector To/From Another // Vector. The Function Returns The // Sum/Difference Of The Vectors. vec3 operator+ (vec3 a) { return vec3 (array[0] + a.array[0], array[1] + a.array[1], array[2] + a.array[2]); } vec3 operator- (vec3 a) { return vec3 (array[0] - a.array[0], array[1] - a.array[1], array[2] - a.array[2]); }
// Sets A Vector To Any Other Array // Of 3 floating point values. vec3 operator= (float Array [3]) { array [0] = Array [0]; array [1] = Array [1]; array [2] = Array [2]; }
// Returns The Vector Negated. vec3 operator- () { return vec3 (-array[0], -array[1], -array[2]); }
// Multiplies The Vector By Another Vector. // Returns The Dot Product Of The Two Vectors. float operator* (vec3 a) { return (float)(array[0]*a.array[0] + array[1]*a.array[1] + array[2]*a.array[2]); } };
That allows the following to be done:
vec3 CrossProduct (vec3 a, vec3 b) { return vec3 (a.array[1]*b.array[2] - a.array[2]*b.array[1], a.array[2]*b.array[0] - a.array[0]*b.array[2], a.array[0]*b.array[1] - a.array[1]*b.array[0]); }
// Returns The Length Of Any 2 or 3 Unit Vector. float Length (vec3 a) { return sqrt (a.array[0]*a.array[0] + a.array[1]*a.array[1] + a.array[2]*a.array[2]); }
vec3 Normalize (vec3 a) { float length; length = Length (a); if (!(length==0)) { return vec3 (a.array[0]/length, a.array[1]/length, a.array[2]/length); } else { return vec3 (0, 0, 0); } }
vec3 Point1; vec3 Point2; int thingy;
Point1 = vec3 (1, 2, 3);
thingy = Length (Point1); thingy = Length (vec3 (0, 1, 0));
Point2 = -Normalize (Point1); thingy = Point1 * Point2; Point2 = CrossProduct (Point1, Point2); Point1 = Point1 +Point2 + Point1 - Point2;
All that should work, not that there's any reason for it... The entire text document with all the class and functions in it's floating around the web somewhere (I think I have it on my site, www.xenovacivus.50megs.com or something like that - I was surprised that it still existed, actually...). It's outdated, but everything there should work... You can still load the arrays into a glcommand also- glvertex3fv (point.array); where point is a vec3. The .array is needed because that's where all the stuff is stored. To manually assign a value to any single element in the array, use point.array [element] = value.
|
|
|
Post by James :) (aka Madcow) on Sept 5, 2006 17:27:50 GMT -5
u'm you call a function inside a varible and return the result using return inside the fuction
then the varible will equal what you returned
|
|
|
Post by Centanoul on Nov 10, 2006 23:01:32 GMT -5
The sloppy way, which I still prefer because pointers just smack me in the face sometimes is thus:
int main () { for (int n=0; n<3; n++) { nx [n]=Add(a[2], b[2], n); } return 0; }
float Add(float a[2], float b[2], float p) { float c[2]; c[0]=a[0]+b[0]; c[1]=a[1]+b[1]; c[2]=a[2]+b[2]; return (c[p]); }
But mostly I use
float Add(float a, float b) { return (a+b); }
with a for/next for each element. As its the least code, and just my preference
|
|