Good day. I have an array with the coordinates of the contour of the object, the coordinates in the array in a random order. It is necessary to arrange the elements in such a order that when connecting the points, a closed contour would turn out (a continuous line). Tell me how this can be implemented, or in which direction to move? I find the contour of the object as follows
//FIND LEFT EDGE OF COLOR AREA int LFillLoc = x; //the location to check/fill on the left while (true) { PixelsChecked[LFillLoc, y] = true; LFillLoc--; //de-increment counter bool ret = CheckPixel(LFillLoc, y, imgBrush, startcolor); if (LFillLoc <= 0 || !ret || (PixelsChecked[LFillLoc, y])) break; //exit loop if we're at edge of bitmap or color area } LFillLoc ++; //FIND RIGHT EDGE OF COLOR AREA int RFillLoc = x; //the location to check/fill on the left while (true) { PixelsChecked[RFillLoc, y] = true; RFillLoc++; //increment counter bool ret = CheckPixel(RFillLoc, y, imgBrush, startcolor); if (RFillLoc >= bmpsize.Width || !ret || (PixelsChecked[RFillLoc, y])) break; //exit loop if we're at edge of bitmap or color area } RFillLoc --; //START THE LOOP UPWARDS AND DOWNWARDS //ptr = (int)bmpsize.Width * (y1) + x; for (int i = LFillLoc; i <= RFillLoc; ++i) { //START LOOP UPWARDS //if we're not above the top of the bitmap and the pixel above this one is within the color tolerance if (y > 0 && CheckPixel(i, y - 1, imgBrush, startcolor) && !PixelsChecked[i, y - 1]) LinearFloodFill4(i, y - 1, bmpsize, startcolor, imgBrush); //START LOOP DOWNWARDS if (y < (bmpsize.Height - 1) && CheckPixel(i, y + 1, imgBrush, startcolor) && !PixelsChecked[i, y + 1]) LinearFloodFill4(i, y + 1, bmpsize, startcolor, imgBrush); } bool CheckPixel(int x, int y, Color[] imgBrush, Color original) { int index = (int)imagerender.Size.Width * (y) + x; bool ret = true; if (30 * Math.Pow((imgBrush[index].R - original.R), 2) + 59 * Math.Pow((imgBrush[index].G - original.G), 2) + 11 * Math.Pow((imgBrush[index].B - original.B), 2) > sl) { position.Add(index); // координата контура ret = false; } return ret; } The Magic Wand tool in Photoshop.
