ObjectARX

Reply
Active Contributor
cadproman
Posts: 43
Registered: ‎03-20-2002
Message 1 of 4 (217 Views)

AcGePoint3d sorting

217 Views, 3 Replies
08-27-2012 01:12 PM

Probably more of a C++ question than ARX, and I'm sorry ahead of time, but can anyone tell me what is going wrong here? The code below will compile and run when I pass in an AcGePoint3dArray of several points, but the array is unchanged after it runs...

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);

 TIA,

Ralph Gaston

Employee
artc2
Posts: 229
Registered: ‎06-08-2010
Message 2 of 4 (211 Views)

Re: AcGePoint3d sorting

08-27-2012 03:44 PM in reply to: cadproman

You need to pass the array by reference so that you are operating on the original array not a copy.

Distinguished Mentor
owenwengerd
Posts: 686
Registered: ‎08-06-2002
Message 3 of 4 (209 Views)

Re: AcGePoint3d sorting

08-27-2012 03:45 PM in reply to: cadproman

It looks like you intended to pass  the argument to sortPoints() by reference instead of by value.

--
Owen Wengerd
ManuSoft
Active Contributor
cadproman
Posts: 43
Registered: ‎03-20-2002
Message 4 of 4 (192 Views)

Re: AcGePoint3d sorting

08-28-2012 07:04 AM in reply to: cadproman

That appears to have fixed it. Thanks to both of you. I thought I was passing in the address just because it was an array. Pretty embarrassing, but no telling how much time it saved me. Thanks again!

Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Do you have 60 seconds to spare? The Autodesk Community Team is revamping our site ranking system and we want your feedback! Please click here to launch the 5 question survey. As always your input is greatly appreciated.