Skip to main content
gamedev.net
🔒 Locked

Sorting Vector Points

Started by Red_Dash Apr 6, 2005 at 4:09 AM 4 replies 650+ views
Original Post
Red_Dash
Red_Dash
Hi Is there a simple way through DirectX to sort an array of D3DXVECTOR3 points based on their z value into ascending order or will it be a trip into the STL? Thanks Dave
Illco
Illco
Not necessarily. Standard C has quicksort also implemented, see qsort(...) in stdlib.h. You need to provide a comparison function which compares individual elements. Then qsort will sort the array accordingly.

Greetz,

Illco
Muhammad Haggag
Muhammad Haggag
Quote:
Original post by Illco
Not necessarily. Standard C has quicksort also implemented, see qsort(...) in stdlib.h. You need to provide a comparison function which compares individual elements. Then qsort will sort the array accordingly.

Generally, don't use weakly-typed C standard library routines if you can. The STL is better to be used here, both because of type-safety and performance.

bool ascending_vector3_sort_predicate(const D3DXVECTOR3& lhs, const D3DXVECTOR3& rhs){	return lhs.z < rhs.z;}int main(){	D3DXVECTOR3 vecs[100];	std::sort(vecs, vecs + 101, ascending_vector3_sort_predicate);	return 0;}

Red_Dash
Red_Dash
Thanks for your help guys.

'Coder' - that was just what I needed... thank you.
Illco
Illco
Good point. I thought I remembered that stl's sort worked only on stl structures (vector, array) and that red_dash was trying to avoid stl alltogether. Good that its cleared up now.

Illco
Muhammad Haggag
Muhammad Haggag
Quote:
Original post by Illco
Good point. I thought I remembered that stl's sort worked only on stl structures (vector, array) and that red_dash was trying to avoid stl alltogether. Good that its cleared up now.

One of STL's key strengths is the concept of iterators - 'generalized' pointers of sorts. Check the great info in SGI STL documentation - read it all if you have the time (and brains! This stuff gets thick sometimes [smile]).

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.