Jump to content
  • Advertisement
Sign in to follow this  
mike74

setTriangle

This topic is 4834 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have a Triangle class in C++: struct Triangle { Vector v1, v2, v3; }; I would like to use the STL set like so: set<Triangle> triangleset; Unfortunately, I need to define the operator< like so: bool operator<(Triangle t1, Triangle t2) { ... } Is there anything simple that I can put in there to make it work? I don't want to do a ton of comparisons of all 9 values in the triangles. Mike http://www.coolgroups.com/

Share this post


Link to post
Share on other sites
Advertisement
First, what do you mean by <??

The position on the screen..
The area..
The perimeter..

Share this post


Link to post
Share on other sites
Don't use std::set. According to this, a set is a sorted container, which means you have to define the '<' operator so that it can sort the triangles. If you don't want to define '<', and thus you don't want a sorted container, you should choose between std::vector, std::list, or std::deque.

Share this post


Link to post
Share on other sites
I need a container that will not keep multiple copies of the triangles if I insert the same triangle twice.

Mike
http://www.coolgroups.com/

Share this post


Link to post
Share on other sites
This is what I have, and I don't like it:

bool operator<(Triangle t1, Triangle t2)
{
if (t1.v0[0] != t2.v0[0]) return (t1.v0[0] < t2.v0[0]);
if (t1.v0[1] != t2.v0[1]) return (t1.v0[1] < t2.v0[1]);
if (t1.v0[2] != t2.v0[2]) return (t1.v0[2] < t2.v0[2]);

if (t1.v1[0] != t2.v1[0]) return (t1.v1[0] < t2.v1[0]);
if (t1.v1[1] != t2.v1[1]) return (t1.v1[1] < t2.v1[1]);
if (t1.v1[2] != t2.v1[2]) return (t1.v1[2] < t2.v1[2]);

if (t1.v2[0] != t2.v2[0]) return (t1.v2[0] < t2.v2[0]);
if (t1.v2[1] != t2.v2[1]) return (t1.v2[1] < t2.v2[1]);
return (t1.v2[2] < t2.v2[2]);
}

Any ideas to improve it?

I'd like it to return false if the triangles are equal, but have the vertices mixed up. For instance, t1 might have points p1, p2, and p3, and t2 might have p3, p2, and p1. I don't really know an elegant way to check this though.

Mike
http://www.coolgroups.com/


Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!