Question

Suppose you have some points in 3D. You would like to sort it either increasing or decreasing order. You don't consider CW/CCW on sorting. How do you sort?

Was it helpful?

Solution

One method of sorting 3D points would be to compare their magnitudes - their distance from the origin point (0, 0, 0) - which may be computed using a 3D analogue of Pythagora's Theorem:

M = sqrt(x^2 + y^2 + z^2) http://www.sciweavers.org/upload/Tex2Img_1392933041/eqn.png

You'll then have a list of floats/doubles that may be sorted using any conventional sorting algorithm.

That's just the most common method, though. There exist infinitely many ways of comparing 3D points, some of which are more sensible than others. For example, which is the "bigger" point, (1, 0, 0) or (-10, -50, 5)? Comparing the X or Y coordinate would suggest the former being larger, while comparing the Z coordinate or magnitude suggests the latter being larger. None of these answers are completely right or wrong; it really depends on what you need your application to do.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top