• Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

C++ sort square grid


New Coder
Trying to sort 2d coordinates for a square that can be angles relative to x axis. I can get the first corner but I'm having trouble sorting the row.

For example:(This is sorted but I can get these points in any order and need to sort them like this: 1,3 - 2,4 - 3,5 - 2,2 - 3,3 - 4,4 - 3-1 - 4,2 - 5,3

I can find 1,3 as my starting corner using a bubble sort or sort:

typedef struct
float x;
float y;
} points2d;

vector<points2d> pointV;

bool comp(points2d &A, points2d &B)
if(A.x > B.x)
return 0;
else if(A.x == B.x)
if(A.y < B.y)
return 0;

return 1;
sort(mygrid->pointV.begin(), mygrid->pointV.end(), &comp);

I imagine I could find 2,4 - 3,5 by finding the smallest x such that it has a larger value than my starting point of 1,3. Then repeat from the point found. Then I could find row 2 just as I did row 1 by excluding my 3 points and sorting again. Problem is I can't seem to get the points in the row ordered correctly. I keep getting points from another row bubbling up.

1,3 - 2,4 - 2,2 - 3,5
This is where I get stuck 2,2 and 3,5 are out of order in attempts to sort.