|
[code]int pointSortByX(const void *p1, const void *p2){
return ((AcGePoint3d*)p1)->x - ((AcGePoint3d*)p2)->x;}
int pointSortByY(const void *p1, const void *p2){
return ((AcGePoint3d*)p1)->y - ((AcGePoint3d*)p2)->y;}
void sortPoints(AcGePoint3dArray allIntersPts)
{
/*the first two elements are the start and endpoints. Use them to determine
if the array should be sorted by x or y values. */
double distInX = abs(allIntersPts.at(0).x - allIntersPts.at(1).x);
double distInY = abs(allIntersPts.at(0).y - allIntersPts.at(1).y);
//if the line is more vertical, then sort by the y-coords
if(distInY > distInX)
std::qsort(allIntersPts.asArrayPtr(), allIntersPts.length(), sizeof(AcGePoint3d), pointSortByY);
//else the line is more horizontal, then sort by the x-coords
else
std::qsort(allIntersPts.asArrayPtr(), allIntersPts.length(), sizeof(AcGePoint3d), pointSortByX);
return;
}
AcGePoint3dArray allIntersPts;
/*code to append some points*/
acutPrintf(_T("\nBefore sort:"));
for(int i = 0; i < allIntersPts.logicalLength(); i++)
acutPrintf(_T("\n%d: %.3f, %.3f"), i, allIntersPts.at(i).x, allIntersPts.at(i).y);
sortPoints(allIntersPts);
acutPrintf(_T("\nAfter sort:"));
for(int i = 0; i < allIntersPts.logicalLength(); i++)
acutPrintf(_T("\n%d: %.3f, %.3f"), i, allIntersPts.at(i).x, allIntersPts.at(i).y);[/code] |
|