Jump to content

Vector to string


AggrorJorn
 Share

Recommended Posts

The current API has these nice String converters.

 

//Leadwerks.h

std::string IntToString( int i );
std::string String( double f );
std::string String( int i );
std::string String( unsigned int i );
std::string String( long l );
std::string String( const unsigned long l );

 

 

I use these Vector2, Vector3 and Vector4 converters myself. They might be useful for the API to be there:

 

//Header
std::string String(Leadwerks::Vec2 vector);
std::string String(Leadwerks::Vec3 vector);
std::string String(Leadwerks::Vec4 vector);

 

//CPP
std::string String(Vec2 v)
{
return "X: " + Leadwerks::String(v.x) + " Y: " + Leadwerks::String(v.y);
}

std::string String(Vec3 v)
{
return "X: " + Leadwerks::String(v.x) + " Y: " + Leadwerks::String(v.y) + " Z: " + Leadwerks::String(v.z);
}

std::string String(Vec4 v)
{
return "X: " + Leadwerks::String(v.x) + " Y: " + Leadwerks::String(v.y) + " Z: " + Leadwerks::String(v.z) + " W: " + Leadwerks::String(v.w);
}

Link to comment
Share on other sites

Copying and pasting similar functions like this can lead to problems, particularly later when they are more complex. You'd do better to roll them all into a single function, particularly if you want to do more complicated things with your vectors later. This is a general programming thing

 

 

//Header (Kept the same)
std::string String(Leadwerks::Vec2 vector);
std::string String(Leadwerks::Vec3 vector);
std::string String(Leadwerks::Vec4 vector);

 

 

 

//CPP (New function added at top. It's not declared in the header, so it's invisible outside this source file)
std::string VectorToString(vec4 v, int elementCount)
{
   std::string outputString = "";
   char letters[] = {'X','Y','Z','W'};
   for(unsigned int element = 0; element < elementCount; element += 1)
   {
       outputString += letters[element];
       outputString += ": ";
       outputString += Leadwerks::String(v.a[element]); //I'm hoping the member 'a' was carried over from LE2's vectors, because in for-loops (like this) is where it really shines
       if(element != (elementCount - 1)) outputString += ", "; //If it's not the last element, put a comma after it.
       //Single line if statement, code below (if any) executes as part of the for-loop as normal
   }
   return outputString;
}
//Now, this is the only function that manipulates vectors into strings. If you notice a mistake, or want to change the formatting for example, you only rewrite this function, rather than 3 separate functions, Where you might forget one. Or you might typo in one, but not the other two...

std::string String(Vec2 v)
{
   vec4 tempVec;
   tempVec.x = v.x; tempVec.y = v.y; //We don't care about z or w... The VectorToString function will ignore them because we have passed the elementCount parameter as 2.
   return VectorToString(tempVec,2);
}

std::string String(Vec3 v)
{
   vec4 tempVec;
   tempVec.x = v.x; tempVec.y = v.y; tempVec.z = v.z;
   return VectorToString(tempVec,3);
}

std::string String(Vec4 v)
{
   return VectorToString(v,4); //We already have a Vec4, no need to create a new one just for purpose
}

 

As you see:

Don't like the comma between the vector components? Take it out.

Want to put brackets around the whole vector? Do that on either side of the for loop.

 

Whatever change you want to make, you just make the change to that one function. You don't have to copy the change to the others as well. Copy and paste errors are one of the most frequent you'll come across. Try not to get into the habit of copy and pasting for small (but slightly different) functions, as projects get bigger, they begin to cause you no end of hell...

  • Upvote 1

LE Version: 2.50 (Eventually)

Link to comment
Share on other sites

Thinking about it, it's probably a better idea to have the "big" function using a Vec16 instead. I assume Vec9 and Vec16 still exist? The only difference would be that if there are more than 4 elements you may wish to print an index number rather than a letter. Also, in case of a Vec9, you may want a new line every 3 elements, and for a Vec16, every 4 elements, just to make it more readable.

LE Version: 2.50 (Eventually)

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...