Ordering 2D Border PointsImplementing a POCO editor split over a number of TabItemsSprite animation handlerBest way of updating a list of unique items“Critter Tracking: When does it cross its own path?”Simple dictionary storing/viewing applicationProject Euler #11 Largest product in a gridRotate an array to the right by a given number of stepsAlgorithm that finds two numbers that sum to a targetFiltering and Validating value in SETTER

What's the difference between a variable and a memory location?

Why do IR remotes influence AM radios?

Did ancient peoples ever hide their treasure behind puzzles?

Is this position a forced win for Black after move 14?

Was it illegal to blaspheme God in Antioch in 360.-410.?

Don't look at what I did there

What is the practical impact of using System.Random which is not cryptographically random?

Journal published a paper, ignoring my objections as a referee

What caused the end of cybernetic implants?

How to handle inventory and story of a player leaving

In Endgame, wouldn't Stark have remembered Hulk busting out of the stairwell?

“I hope he visit us more often” Why is this wrong?

What is the purpose of Strength, Intelligence and Dexterity in Path of Exile?

How can I reply to coworkers who accuse me of automating people out of work?

Which polygons can be turned inside out by a smooth deformation?

is "prohibition against," a double negative?

Do universities maintain secret textbooks?

Isometric Heyacrazy - Now In 3D!

Under GDPR, can I give permission once to allow everyone to store and process my data?

Count the number of triangles

How can I throw a body?

Spicing up a moment of peace

Is Pathfinder 2e compatible with Pathfinder 1e, and D&D 3.5 and 3rd edition?

Why did Starhopper's exhaust plume become brighter just before landing?



Ordering 2D Border Points


Implementing a POCO editor split over a number of TabItemsSprite animation handlerBest way of updating a list of unique items“Critter Tracking: When does it cross its own path?”Simple dictionary storing/viewing applicationProject Euler #11 Largest product in a gridRotate an array to the right by a given number of stepsAlgorithm that finds two numbers that sum to a targetFiltering and Validating value in SETTER






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








3












$begingroup$


What i am trying to do



I have an image of a polygon exemple:
County



what i am tring to do is getting all border points, that do not have at least one neighbour with the same color, and ordering them in a way that later i can draw lines



The Code



private Vector2F[] ReorderBorder(Vector2F[] Data) 

List<Vector2F> BorderPixels = new List<Vector2F>();

BorderPixels.AddRange(Data);

List<int> Indexs = new List<int>();
Indexs.Add(0);

bool Working = true;

while (Working)


int LastIndex = Indexs.Last();

List<int> Possible = new List<int>();

for (int Index = 0; Index < BorderPixels.Count; Index++)


if (!Indexs.Contains(Index))


if (new Vector2F(BorderPixels[LastIndex].X + 1, BorderPixels[LastIndex].Y) == BorderPixels[Index])
Possible.Add(Index);
else if (new Vector2F(BorderPixels[LastIndex].X + 1, BorderPixels[LastIndex].Y + 1) == BorderPixels[Index])
Possible.Add(Index);
else if (new Vector2F(BorderPixels[LastIndex].X + 1, BorderPixels[LastIndex].Y - 1) == BorderPixels[Index])
Possible.Add(Index);
else if (new Vector2F(BorderPixels[LastIndex].X, BorderPixels[LastIndex].Y + 1) == BorderPixels[Index])
Possible.Add(Index);
else if (new Vector2F(BorderPixels[LastIndex].X - 1, BorderPixels[LastIndex].Y + 1) == BorderPixels[Index])
Possible.Add(Index);
else if (new Vector2F(BorderPixels[LastIndex].X + 1, BorderPixels[LastIndex].Y + 1) == BorderPixels[Index])
Possible.Add(Index);
else if (new Vector2F(BorderPixels[LastIndex].X - 1, BorderPixels[LastIndex].Y) == BorderPixels[Index])
Possible.Add(Index);
else if (new Vector2F(BorderPixels[LastIndex].X - 1, BorderPixels[LastIndex].Y + 1) == BorderPixels[Index])
Possible.Add(Index);
else if (new Vector2F(BorderPixels[LastIndex].X - 1, BorderPixels[LastIndex].Y - 1) == BorderPixels[Index])
Possible.Add(Index);
else if (new Vector2F(BorderPixels[LastIndex].X, BorderPixels[LastIndex].Y - 1) == BorderPixels[Index])
Possible.Add(Index);
else if (new Vector2F(BorderPixels[LastIndex].X - 1, BorderPixels[LastIndex].Y - 1) == BorderPixels[Index])
Possible.Add(Index);
else if (new Vector2F(BorderPixels[LastIndex].X + 1, BorderPixels[LastIndex].Y - 1) == BorderPixels[Index])
Possible.Add(Index);



Indexs.Add(GetPreference(BorderPixels[LastIndex], Possible.ToArray(), BorderPixels));

if (BorderPixels.Count == Indexs.Count)
Working = false;


List<Vector2F> Vertices = new List<Vector2F>();

for (int Index = 0; Index < Indexs.Count; Index++)
Vertices.Add(BorderPixels[Indexs[Index]]);

return Vertices.ToArray();


private int GetPreference(Vector2F Origin, int[] Indices, List<Vector2F> BorderPixels)

if (Indices.Length == 1)
return Indices[0];

Vector2F[] IndicesTest = new Vector2F[Indices.Length];

///Relativity
/// 0 - Up
/// 1 - Up Right
/// 2 - Right
/// 3 - Down Right
/// 4 - Down
/// 5 - Down Left
/// 6 - Left
/// 7 - Up Left
for (int Index = 0; Index < Indices.Length; Index++)

if(new Vector2F(Origin.X, Origin.Y - 1) == BorderPixels[Indices[Index]])
IndicesTest[Index] = new Vector2F(Indices[Index], 0);
else if(new Vector2F(Origin.X + 1, Origin.Y - 1) == BorderPixels[Indices[Index]])
IndicesTest[Index] = new Vector2F(Indices[Index], 1);
else if (new Vector2F(Origin.X + 1, Origin.Y) == BorderPixels[Indices[Index]])
IndicesTest[Index] = new Vector2F(Indices[Index], 2);
else if (new Vector2F(Origin.X + 1, Origin.Y + 1) == BorderPixels[Indices[Index]])
IndicesTest[Index] = new Vector2F(Indices[Index], 3);
else if (new Vector2F(Origin.X, Origin.Y + 1) == BorderPixels[Indices[Index]])
IndicesTest[Index] = new Vector2F(Indices[Index], 4);
else if (new Vector2F(Origin.X - 1, Origin.Y + 1) == BorderPixels[Indices[Index]])
IndicesTest[Index] = new Vector2F(Indices[Index], 5);
else if (new Vector2F(Origin.X - 1, Origin.Y) == BorderPixels[Indices[Index]])
IndicesTest[Index] = new Vector2F(Indices[Index], 6);
else if (new Vector2F(Origin.X - 1, Origin.Y - 1) == BorderPixels[Indices[Index]])
IndicesTest[Index] = new Vector2F(Indices[Index], 7);


if (IndicesTest.Length != 0)

Array.Sort(IndicesTest, CompareY);
return (int)IndicesTest[0].X;


return 0;


private int CompareY(Vector2F Left, Vector2F Right)

if (Left.Y < Right.Y)
return -1;
else if (Left.Y == Right.Y)
return 0;
else
return 1;



What it does



private Vector2F[] ReorderBorder(Vector2F[] Data)


This is the main function where i pass the border points, currently orderd by y so 0,0 1,0 2,0 0,1 1,1 etc..



so what it does is starting from the first pixel it get all existing border pixels in 8 directions and add the "preferenced" pixel (see GetPreference function) then after finishing ordering all pixels it sends a orderd array back



private int GetPreference(Vector2F Origin, int[] Indices, List<Vector2F> BorderPixels)


this functions recives a point and a list of possible neighbours then it returns the index of the perfed one (lowest score see code)



private int CompareY(Vector2F Left, Vector2F Right)


this function is simply used to sort by the lowest y, for the score on the GetPreference function



What i want to know



All i want to know is if there is any way to optmized the code, or a better alternative, i know that the code as some bugs but this is the most reliable way i could do, since i was not able to find any good alternative online










share|improve this question











$endgroup$













  • $begingroup$
    All green points seem to have at least one neighbour with the same color and all transparent points seem to have at least one neighbour being transparent. So, according to your definition there are no points in the result set. Your working example draws black lines that are not on a border. I'm a bit confused.
    $endgroup$
    – Olivier Jacot-Descombes
    8 hours ago











  • $begingroup$
    @OlivierJacot-Descombes first i dont know what you mean with green points the exemple is yellow second getting the points from the image is not in question, i have everything working i just want to know if the reorganization of the points can be optimized, and final in the exemple we have yellow so the way i get points is if a points is yellow see if any of the neighbours is transparent if yes then add to the border list if does not then do not add and in the point in question is transperent completely ignore
    $endgroup$
    – Bot Wade
    7 hours ago











  • $begingroup$
    Looking at HTML color codes and names I would say the color is very close to Tea Green.
    $endgroup$
    – Olivier Jacot-Descombes
    7 hours ago











  • $begingroup$
    @OlivierJacot-Descombes look that as nothing to do with the question so unless you have anything constructive to say please stop comenting
    $endgroup$
    – Bot Wade
    7 hours ago






  • 2




    $begingroup$
    @BotWade: don't be rude to people trying to help. The second picture, titled "working exemple", doesn't make much sense. The border lines are all over the place. I suggest you either update or remove the picture. Also, arguing over colors is unnecessary. Ever heard of colorblindness?
    $endgroup$
    – TomG
    7 hours ago

















3












$begingroup$


What i am trying to do



I have an image of a polygon exemple:
County



what i am tring to do is getting all border points, that do not have at least one neighbour with the same color, and ordering them in a way that later i can draw lines



The Code



private Vector2F[] ReorderBorder(Vector2F[] Data) 

List<Vector2F> BorderPixels = new List<Vector2F>();

BorderPixels.AddRange(Data);

List<int> Indexs = new List<int>();
Indexs.Add(0);

bool Working = true;

while (Working)


int LastIndex = Indexs.Last();

List<int> Possible = new List<int>();

for (int Index = 0; Index < BorderPixels.Count; Index++)


if (!Indexs.Contains(Index))


if (new Vector2F(BorderPixels[LastIndex].X + 1, BorderPixels[LastIndex].Y) == BorderPixels[Index])
Possible.Add(Index);
else if (new Vector2F(BorderPixels[LastIndex].X + 1, BorderPixels[LastIndex].Y + 1) == BorderPixels[Index])
Possible.Add(Index);
else if (new Vector2F(BorderPixels[LastIndex].X + 1, BorderPixels[LastIndex].Y - 1) == BorderPixels[Index])
Possible.Add(Index);
else if (new Vector2F(BorderPixels[LastIndex].X, BorderPixels[LastIndex].Y + 1) == BorderPixels[Index])
Possible.Add(Index);
else if (new Vector2F(BorderPixels[LastIndex].X - 1, BorderPixels[LastIndex].Y + 1) == BorderPixels[Index])
Possible.Add(Index);
else if (new Vector2F(BorderPixels[LastIndex].X + 1, BorderPixels[LastIndex].Y + 1) == BorderPixels[Index])
Possible.Add(Index);
else if (new Vector2F(BorderPixels[LastIndex].X - 1, BorderPixels[LastIndex].Y) == BorderPixels[Index])
Possible.Add(Index);
else if (new Vector2F(BorderPixels[LastIndex].X - 1, BorderPixels[LastIndex].Y + 1) == BorderPixels[Index])
Possible.Add(Index);
else if (new Vector2F(BorderPixels[LastIndex].X - 1, BorderPixels[LastIndex].Y - 1) == BorderPixels[Index])
Possible.Add(Index);
else if (new Vector2F(BorderPixels[LastIndex].X, BorderPixels[LastIndex].Y - 1) == BorderPixels[Index])
Possible.Add(Index);
else if (new Vector2F(BorderPixels[LastIndex].X - 1, BorderPixels[LastIndex].Y - 1) == BorderPixels[Index])
Possible.Add(Index);
else if (new Vector2F(BorderPixels[LastIndex].X + 1, BorderPixels[LastIndex].Y - 1) == BorderPixels[Index])
Possible.Add(Index);



Indexs.Add(GetPreference(BorderPixels[LastIndex], Possible.ToArray(), BorderPixels));

if (BorderPixels.Count == Indexs.Count)
Working = false;


List<Vector2F> Vertices = new List<Vector2F>();

for (int Index = 0; Index < Indexs.Count; Index++)
Vertices.Add(BorderPixels[Indexs[Index]]);

return Vertices.ToArray();


private int GetPreference(Vector2F Origin, int[] Indices, List<Vector2F> BorderPixels)

if (Indices.Length == 1)
return Indices[0];

Vector2F[] IndicesTest = new Vector2F[Indices.Length];

///Relativity
/// 0 - Up
/// 1 - Up Right
/// 2 - Right
/// 3 - Down Right
/// 4 - Down
/// 5 - Down Left
/// 6 - Left
/// 7 - Up Left
for (int Index = 0; Index < Indices.Length; Index++)

if(new Vector2F(Origin.X, Origin.Y - 1) == BorderPixels[Indices[Index]])
IndicesTest[Index] = new Vector2F(Indices[Index], 0);
else if(new Vector2F(Origin.X + 1, Origin.Y - 1) == BorderPixels[Indices[Index]])
IndicesTest[Index] = new Vector2F(Indices[Index], 1);
else if (new Vector2F(Origin.X + 1, Origin.Y) == BorderPixels[Indices[Index]])
IndicesTest[Index] = new Vector2F(Indices[Index], 2);
else if (new Vector2F(Origin.X + 1, Origin.Y + 1) == BorderPixels[Indices[Index]])
IndicesTest[Index] = new Vector2F(Indices[Index], 3);
else if (new Vector2F(Origin.X, Origin.Y + 1) == BorderPixels[Indices[Index]])
IndicesTest[Index] = new Vector2F(Indices[Index], 4);
else if (new Vector2F(Origin.X - 1, Origin.Y + 1) == BorderPixels[Indices[Index]])
IndicesTest[Index] = new Vector2F(Indices[Index], 5);
else if (new Vector2F(Origin.X - 1, Origin.Y) == BorderPixels[Indices[Index]])
IndicesTest[Index] = new Vector2F(Indices[Index], 6);
else if (new Vector2F(Origin.X - 1, Origin.Y - 1) == BorderPixels[Indices[Index]])
IndicesTest[Index] = new Vector2F(Indices[Index], 7);


if (IndicesTest.Length != 0)

Array.Sort(IndicesTest, CompareY);
return (int)IndicesTest[0].X;


return 0;


private int CompareY(Vector2F Left, Vector2F Right)

if (Left.Y < Right.Y)
return -1;
else if (Left.Y == Right.Y)
return 0;
else
return 1;



What it does



private Vector2F[] ReorderBorder(Vector2F[] Data)


This is the main function where i pass the border points, currently orderd by y so 0,0 1,0 2,0 0,1 1,1 etc..



so what it does is starting from the first pixel it get all existing border pixels in 8 directions and add the "preferenced" pixel (see GetPreference function) then after finishing ordering all pixels it sends a orderd array back



private int GetPreference(Vector2F Origin, int[] Indices, List<Vector2F> BorderPixels)


this functions recives a point and a list of possible neighbours then it returns the index of the perfed one (lowest score see code)



private int CompareY(Vector2F Left, Vector2F Right)


this function is simply used to sort by the lowest y, for the score on the GetPreference function



What i want to know



All i want to know is if there is any way to optmized the code, or a better alternative, i know that the code as some bugs but this is the most reliable way i could do, since i was not able to find any good alternative online










share|improve this question











$endgroup$













  • $begingroup$
    All green points seem to have at least one neighbour with the same color and all transparent points seem to have at least one neighbour being transparent. So, according to your definition there are no points in the result set. Your working example draws black lines that are not on a border. I'm a bit confused.
    $endgroup$
    – Olivier Jacot-Descombes
    8 hours ago











  • $begingroup$
    @OlivierJacot-Descombes first i dont know what you mean with green points the exemple is yellow second getting the points from the image is not in question, i have everything working i just want to know if the reorganization of the points can be optimized, and final in the exemple we have yellow so the way i get points is if a points is yellow see if any of the neighbours is transparent if yes then add to the border list if does not then do not add and in the point in question is transperent completely ignore
    $endgroup$
    – Bot Wade
    7 hours ago











  • $begingroup$
    Looking at HTML color codes and names I would say the color is very close to Tea Green.
    $endgroup$
    – Olivier Jacot-Descombes
    7 hours ago











  • $begingroup$
    @OlivierJacot-Descombes look that as nothing to do with the question so unless you have anything constructive to say please stop comenting
    $endgroup$
    – Bot Wade
    7 hours ago






  • 2




    $begingroup$
    @BotWade: don't be rude to people trying to help. The second picture, titled "working exemple", doesn't make much sense. The border lines are all over the place. I suggest you either update or remove the picture. Also, arguing over colors is unnecessary. Ever heard of colorblindness?
    $endgroup$
    – TomG
    7 hours ago













3












3








3


1



$begingroup$


What i am trying to do



I have an image of a polygon exemple:
County



what i am tring to do is getting all border points, that do not have at least one neighbour with the same color, and ordering them in a way that later i can draw lines



The Code



private Vector2F[] ReorderBorder(Vector2F[] Data) 

List<Vector2F> BorderPixels = new List<Vector2F>();

BorderPixels.AddRange(Data);

List<int> Indexs = new List<int>();
Indexs.Add(0);

bool Working = true;

while (Working)


int LastIndex = Indexs.Last();

List<int> Possible = new List<int>();

for (int Index = 0; Index < BorderPixels.Count; Index++)


if (!Indexs.Contains(Index))


if (new Vector2F(BorderPixels[LastIndex].X + 1, BorderPixels[LastIndex].Y) == BorderPixels[Index])
Possible.Add(Index);
else if (new Vector2F(BorderPixels[LastIndex].X + 1, BorderPixels[LastIndex].Y + 1) == BorderPixels[Index])
Possible.Add(Index);
else if (new Vector2F(BorderPixels[LastIndex].X + 1, BorderPixels[LastIndex].Y - 1) == BorderPixels[Index])
Possible.Add(Index);
else if (new Vector2F(BorderPixels[LastIndex].X, BorderPixels[LastIndex].Y + 1) == BorderPixels[Index])
Possible.Add(Index);
else if (new Vector2F(BorderPixels[LastIndex].X - 1, BorderPixels[LastIndex].Y + 1) == BorderPixels[Index])
Possible.Add(Index);
else if (new Vector2F(BorderPixels[LastIndex].X + 1, BorderPixels[LastIndex].Y + 1) == BorderPixels[Index])
Possible.Add(Index);
else if (new Vector2F(BorderPixels[LastIndex].X - 1, BorderPixels[LastIndex].Y) == BorderPixels[Index])
Possible.Add(Index);
else if (new Vector2F(BorderPixels[LastIndex].X - 1, BorderPixels[LastIndex].Y + 1) == BorderPixels[Index])
Possible.Add(Index);
else if (new Vector2F(BorderPixels[LastIndex].X - 1, BorderPixels[LastIndex].Y - 1) == BorderPixels[Index])
Possible.Add(Index);
else if (new Vector2F(BorderPixels[LastIndex].X, BorderPixels[LastIndex].Y - 1) == BorderPixels[Index])
Possible.Add(Index);
else if (new Vector2F(BorderPixels[LastIndex].X - 1, BorderPixels[LastIndex].Y - 1) == BorderPixels[Index])
Possible.Add(Index);
else if (new Vector2F(BorderPixels[LastIndex].X + 1, BorderPixels[LastIndex].Y - 1) == BorderPixels[Index])
Possible.Add(Index);



Indexs.Add(GetPreference(BorderPixels[LastIndex], Possible.ToArray(), BorderPixels));

if (BorderPixels.Count == Indexs.Count)
Working = false;


List<Vector2F> Vertices = new List<Vector2F>();

for (int Index = 0; Index < Indexs.Count; Index++)
Vertices.Add(BorderPixels[Indexs[Index]]);

return Vertices.ToArray();


private int GetPreference(Vector2F Origin, int[] Indices, List<Vector2F> BorderPixels)

if (Indices.Length == 1)
return Indices[0];

Vector2F[] IndicesTest = new Vector2F[Indices.Length];

///Relativity
/// 0 - Up
/// 1 - Up Right
/// 2 - Right
/// 3 - Down Right
/// 4 - Down
/// 5 - Down Left
/// 6 - Left
/// 7 - Up Left
for (int Index = 0; Index < Indices.Length; Index++)

if(new Vector2F(Origin.X, Origin.Y - 1) == BorderPixels[Indices[Index]])
IndicesTest[Index] = new Vector2F(Indices[Index], 0);
else if(new Vector2F(Origin.X + 1, Origin.Y - 1) == BorderPixels[Indices[Index]])
IndicesTest[Index] = new Vector2F(Indices[Index], 1);
else if (new Vector2F(Origin.X + 1, Origin.Y) == BorderPixels[Indices[Index]])
IndicesTest[Index] = new Vector2F(Indices[Index], 2);
else if (new Vector2F(Origin.X + 1, Origin.Y + 1) == BorderPixels[Indices[Index]])
IndicesTest[Index] = new Vector2F(Indices[Index], 3);
else if (new Vector2F(Origin.X, Origin.Y + 1) == BorderPixels[Indices[Index]])
IndicesTest[Index] = new Vector2F(Indices[Index], 4);
else if (new Vector2F(Origin.X - 1, Origin.Y + 1) == BorderPixels[Indices[Index]])
IndicesTest[Index] = new Vector2F(Indices[Index], 5);
else if (new Vector2F(Origin.X - 1, Origin.Y) == BorderPixels[Indices[Index]])
IndicesTest[Index] = new Vector2F(Indices[Index], 6);
else if (new Vector2F(Origin.X - 1, Origin.Y - 1) == BorderPixels[Indices[Index]])
IndicesTest[Index] = new Vector2F(Indices[Index], 7);


if (IndicesTest.Length != 0)

Array.Sort(IndicesTest, CompareY);
return (int)IndicesTest[0].X;


return 0;


private int CompareY(Vector2F Left, Vector2F Right)

if (Left.Y < Right.Y)
return -1;
else if (Left.Y == Right.Y)
return 0;
else
return 1;



What it does



private Vector2F[] ReorderBorder(Vector2F[] Data)


This is the main function where i pass the border points, currently orderd by y so 0,0 1,0 2,0 0,1 1,1 etc..



so what it does is starting from the first pixel it get all existing border pixels in 8 directions and add the "preferenced" pixel (see GetPreference function) then after finishing ordering all pixels it sends a orderd array back



private int GetPreference(Vector2F Origin, int[] Indices, List<Vector2F> BorderPixels)


this functions recives a point and a list of possible neighbours then it returns the index of the perfed one (lowest score see code)



private int CompareY(Vector2F Left, Vector2F Right)


this function is simply used to sort by the lowest y, for the score on the GetPreference function



What i want to know



All i want to know is if there is any way to optmized the code, or a better alternative, i know that the code as some bugs but this is the most reliable way i could do, since i was not able to find any good alternative online










share|improve this question











$endgroup$




What i am trying to do



I have an image of a polygon exemple:
County



what i am tring to do is getting all border points, that do not have at least one neighbour with the same color, and ordering them in a way that later i can draw lines



The Code



private Vector2F[] ReorderBorder(Vector2F[] Data) 

List<Vector2F> BorderPixels = new List<Vector2F>();

BorderPixels.AddRange(Data);

List<int> Indexs = new List<int>();
Indexs.Add(0);

bool Working = true;

while (Working)


int LastIndex = Indexs.Last();

List<int> Possible = new List<int>();

for (int Index = 0; Index < BorderPixels.Count; Index++)


if (!Indexs.Contains(Index))


if (new Vector2F(BorderPixels[LastIndex].X + 1, BorderPixels[LastIndex].Y) == BorderPixels[Index])
Possible.Add(Index);
else if (new Vector2F(BorderPixels[LastIndex].X + 1, BorderPixels[LastIndex].Y + 1) == BorderPixels[Index])
Possible.Add(Index);
else if (new Vector2F(BorderPixels[LastIndex].X + 1, BorderPixels[LastIndex].Y - 1) == BorderPixels[Index])
Possible.Add(Index);
else if (new Vector2F(BorderPixels[LastIndex].X, BorderPixels[LastIndex].Y + 1) == BorderPixels[Index])
Possible.Add(Index);
else if (new Vector2F(BorderPixels[LastIndex].X - 1, BorderPixels[LastIndex].Y + 1) == BorderPixels[Index])
Possible.Add(Index);
else if (new Vector2F(BorderPixels[LastIndex].X + 1, BorderPixels[LastIndex].Y + 1) == BorderPixels[Index])
Possible.Add(Index);
else if (new Vector2F(BorderPixels[LastIndex].X - 1, BorderPixels[LastIndex].Y) == BorderPixels[Index])
Possible.Add(Index);
else if (new Vector2F(BorderPixels[LastIndex].X - 1, BorderPixels[LastIndex].Y + 1) == BorderPixels[Index])
Possible.Add(Index);
else if (new Vector2F(BorderPixels[LastIndex].X - 1, BorderPixels[LastIndex].Y - 1) == BorderPixels[Index])
Possible.Add(Index);
else if (new Vector2F(BorderPixels[LastIndex].X, BorderPixels[LastIndex].Y - 1) == BorderPixels[Index])
Possible.Add(Index);
else if (new Vector2F(BorderPixels[LastIndex].X - 1, BorderPixels[LastIndex].Y - 1) == BorderPixels[Index])
Possible.Add(Index);
else if (new Vector2F(BorderPixels[LastIndex].X + 1, BorderPixels[LastIndex].Y - 1) == BorderPixels[Index])
Possible.Add(Index);



Indexs.Add(GetPreference(BorderPixels[LastIndex], Possible.ToArray(), BorderPixels));

if (BorderPixels.Count == Indexs.Count)
Working = false;


List<Vector2F> Vertices = new List<Vector2F>();

for (int Index = 0; Index < Indexs.Count; Index++)
Vertices.Add(BorderPixels[Indexs[Index]]);

return Vertices.ToArray();


private int GetPreference(Vector2F Origin, int[] Indices, List<Vector2F> BorderPixels)

if (Indices.Length == 1)
return Indices[0];

Vector2F[] IndicesTest = new Vector2F[Indices.Length];

///Relativity
/// 0 - Up
/// 1 - Up Right
/// 2 - Right
/// 3 - Down Right
/// 4 - Down
/// 5 - Down Left
/// 6 - Left
/// 7 - Up Left
for (int Index = 0; Index < Indices.Length; Index++)

if(new Vector2F(Origin.X, Origin.Y - 1) == BorderPixels[Indices[Index]])
IndicesTest[Index] = new Vector2F(Indices[Index], 0);
else if(new Vector2F(Origin.X + 1, Origin.Y - 1) == BorderPixels[Indices[Index]])
IndicesTest[Index] = new Vector2F(Indices[Index], 1);
else if (new Vector2F(Origin.X + 1, Origin.Y) == BorderPixels[Indices[Index]])
IndicesTest[Index] = new Vector2F(Indices[Index], 2);
else if (new Vector2F(Origin.X + 1, Origin.Y + 1) == BorderPixels[Indices[Index]])
IndicesTest[Index] = new Vector2F(Indices[Index], 3);
else if (new Vector2F(Origin.X, Origin.Y + 1) == BorderPixels[Indices[Index]])
IndicesTest[Index] = new Vector2F(Indices[Index], 4);
else if (new Vector2F(Origin.X - 1, Origin.Y + 1) == BorderPixels[Indices[Index]])
IndicesTest[Index] = new Vector2F(Indices[Index], 5);
else if (new Vector2F(Origin.X - 1, Origin.Y) == BorderPixels[Indices[Index]])
IndicesTest[Index] = new Vector2F(Indices[Index], 6);
else if (new Vector2F(Origin.X - 1, Origin.Y - 1) == BorderPixels[Indices[Index]])
IndicesTest[Index] = new Vector2F(Indices[Index], 7);


if (IndicesTest.Length != 0)

Array.Sort(IndicesTest, CompareY);
return (int)IndicesTest[0].X;


return 0;


private int CompareY(Vector2F Left, Vector2F Right)

if (Left.Y < Right.Y)
return -1;
else if (Left.Y == Right.Y)
return 0;
else
return 1;



What it does



private Vector2F[] ReorderBorder(Vector2F[] Data)


This is the main function where i pass the border points, currently orderd by y so 0,0 1,0 2,0 0,1 1,1 etc..



so what it does is starting from the first pixel it get all existing border pixels in 8 directions and add the "preferenced" pixel (see GetPreference function) then after finishing ordering all pixels it sends a orderd array back



private int GetPreference(Vector2F Origin, int[] Indices, List<Vector2F> BorderPixels)


this functions recives a point and a list of possible neighbours then it returns the index of the perfed one (lowest score see code)



private int CompareY(Vector2F Left, Vector2F Right)


this function is simply used to sort by the lowest y, for the score on the GetPreference function



What i want to know



All i want to know is if there is any way to optmized the code, or a better alternative, i know that the code as some bugs but this is the most reliable way i could do, since i was not able to find any good alternative online







c# coordinate-system






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 43 mins ago









dfhwze

10.3k2 gold badges19 silver badges67 bronze badges




10.3k2 gold badges19 silver badges67 bronze badges










asked 8 hours ago









Bot WadeBot Wade

601 silver badge10 bronze badges




601 silver badge10 bronze badges














  • $begingroup$
    All green points seem to have at least one neighbour with the same color and all transparent points seem to have at least one neighbour being transparent. So, according to your definition there are no points in the result set. Your working example draws black lines that are not on a border. I'm a bit confused.
    $endgroup$
    – Olivier Jacot-Descombes
    8 hours ago











  • $begingroup$
    @OlivierJacot-Descombes first i dont know what you mean with green points the exemple is yellow second getting the points from the image is not in question, i have everything working i just want to know if the reorganization of the points can be optimized, and final in the exemple we have yellow so the way i get points is if a points is yellow see if any of the neighbours is transparent if yes then add to the border list if does not then do not add and in the point in question is transperent completely ignore
    $endgroup$
    – Bot Wade
    7 hours ago











  • $begingroup$
    Looking at HTML color codes and names I would say the color is very close to Tea Green.
    $endgroup$
    – Olivier Jacot-Descombes
    7 hours ago











  • $begingroup$
    @OlivierJacot-Descombes look that as nothing to do with the question so unless you have anything constructive to say please stop comenting
    $endgroup$
    – Bot Wade
    7 hours ago






  • 2




    $begingroup$
    @BotWade: don't be rude to people trying to help. The second picture, titled "working exemple", doesn't make much sense. The border lines are all over the place. I suggest you either update or remove the picture. Also, arguing over colors is unnecessary. Ever heard of colorblindness?
    $endgroup$
    – TomG
    7 hours ago
















  • $begingroup$
    All green points seem to have at least one neighbour with the same color and all transparent points seem to have at least one neighbour being transparent. So, according to your definition there are no points in the result set. Your working example draws black lines that are not on a border. I'm a bit confused.
    $endgroup$
    – Olivier Jacot-Descombes
    8 hours ago











  • $begingroup$
    @OlivierJacot-Descombes first i dont know what you mean with green points the exemple is yellow second getting the points from the image is not in question, i have everything working i just want to know if the reorganization of the points can be optimized, and final in the exemple we have yellow so the way i get points is if a points is yellow see if any of the neighbours is transparent if yes then add to the border list if does not then do not add and in the point in question is transperent completely ignore
    $endgroup$
    – Bot Wade
    7 hours ago











  • $begingroup$
    Looking at HTML color codes and names I would say the color is very close to Tea Green.
    $endgroup$
    – Olivier Jacot-Descombes
    7 hours ago











  • $begingroup$
    @OlivierJacot-Descombes look that as nothing to do with the question so unless you have anything constructive to say please stop comenting
    $endgroup$
    – Bot Wade
    7 hours ago






  • 2




    $begingroup$
    @BotWade: don't be rude to people trying to help. The second picture, titled "working exemple", doesn't make much sense. The border lines are all over the place. I suggest you either update or remove the picture. Also, arguing over colors is unnecessary. Ever heard of colorblindness?
    $endgroup$
    – TomG
    7 hours ago















$begingroup$
All green points seem to have at least one neighbour with the same color and all transparent points seem to have at least one neighbour being transparent. So, according to your definition there are no points in the result set. Your working example draws black lines that are not on a border. I'm a bit confused.
$endgroup$
– Olivier Jacot-Descombes
8 hours ago





$begingroup$
All green points seem to have at least one neighbour with the same color and all transparent points seem to have at least one neighbour being transparent. So, according to your definition there are no points in the result set. Your working example draws black lines that are not on a border. I'm a bit confused.
$endgroup$
– Olivier Jacot-Descombes
8 hours ago













$begingroup$
@OlivierJacot-Descombes first i dont know what you mean with green points the exemple is yellow second getting the points from the image is not in question, i have everything working i just want to know if the reorganization of the points can be optimized, and final in the exemple we have yellow so the way i get points is if a points is yellow see if any of the neighbours is transparent if yes then add to the border list if does not then do not add and in the point in question is transperent completely ignore
$endgroup$
– Bot Wade
7 hours ago





$begingroup$
@OlivierJacot-Descombes first i dont know what you mean with green points the exemple is yellow second getting the points from the image is not in question, i have everything working i just want to know if the reorganization of the points can be optimized, and final in the exemple we have yellow so the way i get points is if a points is yellow see if any of the neighbours is transparent if yes then add to the border list if does not then do not add and in the point in question is transperent completely ignore
$endgroup$
– Bot Wade
7 hours ago













$begingroup$
Looking at HTML color codes and names I would say the color is very close to Tea Green.
$endgroup$
– Olivier Jacot-Descombes
7 hours ago





$begingroup$
Looking at HTML color codes and names I would say the color is very close to Tea Green.
$endgroup$
– Olivier Jacot-Descombes
7 hours ago













$begingroup$
@OlivierJacot-Descombes look that as nothing to do with the question so unless you have anything constructive to say please stop comenting
$endgroup$
– Bot Wade
7 hours ago




$begingroup$
@OlivierJacot-Descombes look that as nothing to do with the question so unless you have anything constructive to say please stop comenting
$endgroup$
– Bot Wade
7 hours ago




2




2




$begingroup$
@BotWade: don't be rude to people trying to help. The second picture, titled "working exemple", doesn't make much sense. The border lines are all over the place. I suggest you either update or remove the picture. Also, arguing over colors is unnecessary. Ever heard of colorblindness?
$endgroup$
– TomG
7 hours ago




$begingroup$
@BotWade: don't be rude to people trying to help. The second picture, titled "working exemple", doesn't make much sense. The border lines are all over the place. I suggest you either update or remove the picture. Also, arguing over colors is unnecessary. Ever heard of colorblindness?
$endgroup$
– TomG
7 hours ago










3 Answers
3






active

oldest

votes


















2













$begingroup$

You are making the test !Indexs.Contains(Index). Since Indexs is a List<int> you have a look up time of O(n). A HashSet<T> has an approximate look up time of O(1). Create a hash set for this test. Since a set is not ordered, you still need the list.



var Indexs = new List<int>();
var indexTest = new HashSet<int>();

Indexs.Add(0);
indexTest.Add(0);


and



int i = GetPreference(BorderPixels[LastIndex], Possible.ToArray(), BorderPixels);
Indexs.Add(i);
indexTest.Add(i);


and of course now test with



if (!indexTest.Contains(Index))


  • Another point is the repeated indexed access of border pixels. Store the pixels in a temp. This also makes the code more readable.


  • You can merge all the if-statements into one conditional expression. Because of the Short-Circuit Evaluation In C#, the evaluation will stop at the first term evaluating to true.


  • You have duplicated some cases. You have 12 instead of 8. Reordering the conditions in a logical way makes it easier: x-1, x, x+1 combined with y-1, y, y+1.


  • The Boolean temp Working can be inlined.


  • You can initialize collections in the constructor or with collection initializers.


  • The C# naming conventions use camelCase for parameter names and local variables.


  • Since neither data nor borderPixels are altered, copying data into borderPixels seems superfluous. I simply renamed data to borderPixels. This change requires the type of the last parameter of GetPreference to be changed from List<Vector2F> to Vector2F[] and borderPixels.Count must be changed to borderPixels.Length.


The new ReorderBorder method:



private Vector2F[] ReorderBorder(Vector2F[] borderPixels)

var indexes = new List<int> 0 ;
var indexTest = new HashSet<int> 0 ;

while (indexes.Count < borderPixels.Length)
int lastIndex = indexes.Last();
Vector2F last = borderPixels[lastIndex];

var possible = new List<int>();
for (int index = 0; index < borderPixels.Length; index++)
if (!indexTest.Contains(index))
Vector2F current = borderPixels[index];
if (new Vector2F(last.X - 1, last.Y - 1) == current


int preferredIndex = GetPreference(last, possible.ToArray(), borderPixels);
indexes.Add(preferredIndex);
indexTest.Add(preferredIndex);


var vertices = new List<Vector2F>();
for (int index = 0; index < indexes.Count; index++)
vertices.Add(borderPixels[indexes[index]]);


return vertices.ToArray();



You can also apply some of these changes to GetPreference.






share|improve this answer











$endgroup$






















    1













    $begingroup$

    Use enums When in the below snippet each index really means what the numbers stand for in the comments then you definitely need at least an enum for that. This is so extremely fragile. Without these comments nobody ever would be able to decipher this logic.




    ///Relativity
    /// 0 - Up
    /// 1 - Up Right
    /// 2 - Right
    /// 3 - Down Right
    /// 4 - Down
    /// 5 - Down Left
    /// 6 - Left
    /// 7 - Up Left
    for (int Index = 0; Index < Indices.Length; Index++)






    share|improve this answer









    $endgroup$






















      1













      $begingroup$

      DRY Principle



      As a small addendum on Olivier Jacot-Descombes' answer, I would like to add you should go for DRY code.



      answer snippet:




      "You have duplicated some cases. You have 12 instead of 8. Reordering the conditions in a logical way makes it easier: x-1, x, x+1 combined
      with y-1, y, y+1."




      This..



      var current = BorderPixels[Index];
      var last = BorderPixels[LastIndex];
      if (Math.Abs(current.X - last.X) <= 1 && Math.Abs(current.Y - last.Y) <= 1)

      Possible.Add(Index);



      replaces..




      if (new Vector2F(BorderPixels[LastIndex].X + 1, BorderPixels[LastIndex].Y) == BorderPixels[Index])
      Possible.Add(Index);
      else if (new Vector2F(BorderPixels[LastIndex].X + 1, BorderPixels[LastIndex].Y + 1) == BorderPixels[Index])
      Possible.Add(Index);
      else if (new Vector2F(BorderPixels[LastIndex].X + 1, BorderPixels[LastIndex].Y - 1) == BorderPixels[Index])
      Possible.Add(Index);
      else if (new Vector2F(BorderPixels[LastIndex].X, BorderPixels[LastIndex].Y + 1) == BorderPixels[Index])
      Possible.Add(Index);
      else if (new Vector2F(BorderPixels[LastIndex].X - 1, BorderPixels[LastIndex].Y + 1) == BorderPixels[Index])
      Possible.Add(Index);
      else if (new Vector2F(BorderPixels[LastIndex].X + 1, BorderPixels[LastIndex].Y + 1) == BorderPixels[Index])
      Possible.Add(Index);
      else if (new Vector2F(BorderPixels[LastIndex].X - 1, BorderPixels[LastIndex].Y) == BorderPixels[Index])
      Possible.Add(Index);
      else if (new Vector2F(BorderPixels[LastIndex].X - 1, BorderPixels[LastIndex].Y + 1) == BorderPixels[Index])
      Possible.Add(Index);
      else if (new Vector2F(BorderPixels[LastIndex].X - 1, BorderPixels[LastIndex].Y - 1) == BorderPixels[Index])
      Possible.Add(Index);
      else if (new Vector2F(BorderPixels[LastIndex].X, BorderPixels[LastIndex].Y - 1) == BorderPixels[Index])
      Possible.Add(Index);
      else if (new Vector2F(BorderPixels[LastIndex].X - 1, BorderPixels[LastIndex].Y - 1) == BorderPixels[Index])
      Possible.Add(Index);
      else if (new Vector2F(BorderPixels[LastIndex].X + 1, BorderPixels[LastIndex].Y - 1) == BorderPixels[Index])
      Possible.Add(Index);






      share|improve this answer











      $endgroup$

















        Your Answer






        StackExchange.ifUsing("editor", function ()
        StackExchange.using("externalEditor", function ()
        StackExchange.using("snippets", function ()
        StackExchange.snippets.init();
        );
        );
        , "code-snippets");

        StackExchange.ready(function()
        var channelOptions =
        tags: "".split(" "),
        id: "196"
        ;
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function()
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled)
        StackExchange.using("snippets", function()
        createEditor();
        );

        else
        createEditor();

        );

        function createEditor()
        StackExchange.prepareEditor(
        heartbeatType: 'answer',
        autoActivateHeartbeat: false,
        convertImagesToLinks: false,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: null,
        bindNavPrevention: true,
        postfix: "",
        imageUploader:
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        ,
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        );



        );













        draft saved

        draft discarded


















        StackExchange.ready(
        function ()
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f227130%2fordering-2d-border-points%23new-answer', 'question_page');

        );

        Post as a guest















        Required, but never shown

























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        2













        $begingroup$

        You are making the test !Indexs.Contains(Index). Since Indexs is a List<int> you have a look up time of O(n). A HashSet<T> has an approximate look up time of O(1). Create a hash set for this test. Since a set is not ordered, you still need the list.



        var Indexs = new List<int>();
        var indexTest = new HashSet<int>();

        Indexs.Add(0);
        indexTest.Add(0);


        and



        int i = GetPreference(BorderPixels[LastIndex], Possible.ToArray(), BorderPixels);
        Indexs.Add(i);
        indexTest.Add(i);


        and of course now test with



        if (!indexTest.Contains(Index))


        • Another point is the repeated indexed access of border pixels. Store the pixels in a temp. This also makes the code more readable.


        • You can merge all the if-statements into one conditional expression. Because of the Short-Circuit Evaluation In C#, the evaluation will stop at the first term evaluating to true.


        • You have duplicated some cases. You have 12 instead of 8. Reordering the conditions in a logical way makes it easier: x-1, x, x+1 combined with y-1, y, y+1.


        • The Boolean temp Working can be inlined.


        • You can initialize collections in the constructor or with collection initializers.


        • The C# naming conventions use camelCase for parameter names and local variables.


        • Since neither data nor borderPixels are altered, copying data into borderPixels seems superfluous. I simply renamed data to borderPixels. This change requires the type of the last parameter of GetPreference to be changed from List<Vector2F> to Vector2F[] and borderPixels.Count must be changed to borderPixels.Length.


        The new ReorderBorder method:



        private Vector2F[] ReorderBorder(Vector2F[] borderPixels)

        var indexes = new List<int> 0 ;
        var indexTest = new HashSet<int> 0 ;

        while (indexes.Count < borderPixels.Length)
        int lastIndex = indexes.Last();
        Vector2F last = borderPixels[lastIndex];

        var possible = new List<int>();
        for (int index = 0; index < borderPixels.Length; index++)
        if (!indexTest.Contains(index))
        Vector2F current = borderPixels[index];
        if (new Vector2F(last.X - 1, last.Y - 1) == current


        int preferredIndex = GetPreference(last, possible.ToArray(), borderPixels);
        indexes.Add(preferredIndex);
        indexTest.Add(preferredIndex);


        var vertices = new List<Vector2F>();
        for (int index = 0; index < indexes.Count; index++)
        vertices.Add(borderPixels[indexes[index]]);


        return vertices.ToArray();



        You can also apply some of these changes to GetPreference.






        share|improve this answer











        $endgroup$



















          2













          $begingroup$

          You are making the test !Indexs.Contains(Index). Since Indexs is a List<int> you have a look up time of O(n). A HashSet<T> has an approximate look up time of O(1). Create a hash set for this test. Since a set is not ordered, you still need the list.



          var Indexs = new List<int>();
          var indexTest = new HashSet<int>();

          Indexs.Add(0);
          indexTest.Add(0);


          and



          int i = GetPreference(BorderPixels[LastIndex], Possible.ToArray(), BorderPixels);
          Indexs.Add(i);
          indexTest.Add(i);


          and of course now test with



          if (!indexTest.Contains(Index))


          • Another point is the repeated indexed access of border pixels. Store the pixels in a temp. This also makes the code more readable.


          • You can merge all the if-statements into one conditional expression. Because of the Short-Circuit Evaluation In C#, the evaluation will stop at the first term evaluating to true.


          • You have duplicated some cases. You have 12 instead of 8. Reordering the conditions in a logical way makes it easier: x-1, x, x+1 combined with y-1, y, y+1.


          • The Boolean temp Working can be inlined.


          • You can initialize collections in the constructor or with collection initializers.


          • The C# naming conventions use camelCase for parameter names and local variables.


          • Since neither data nor borderPixels are altered, copying data into borderPixels seems superfluous. I simply renamed data to borderPixels. This change requires the type of the last parameter of GetPreference to be changed from List<Vector2F> to Vector2F[] and borderPixels.Count must be changed to borderPixels.Length.


          The new ReorderBorder method:



          private Vector2F[] ReorderBorder(Vector2F[] borderPixels)

          var indexes = new List<int> 0 ;
          var indexTest = new HashSet<int> 0 ;

          while (indexes.Count < borderPixels.Length)
          int lastIndex = indexes.Last();
          Vector2F last = borderPixels[lastIndex];

          var possible = new List<int>();
          for (int index = 0; index < borderPixels.Length; index++)
          if (!indexTest.Contains(index))
          Vector2F current = borderPixels[index];
          if (new Vector2F(last.X - 1, last.Y - 1) == current


          int preferredIndex = GetPreference(last, possible.ToArray(), borderPixels);
          indexes.Add(preferredIndex);
          indexTest.Add(preferredIndex);


          var vertices = new List<Vector2F>();
          for (int index = 0; index < indexes.Count; index++)
          vertices.Add(borderPixels[indexes[index]]);


          return vertices.ToArray();



          You can also apply some of these changes to GetPreference.






          share|improve this answer











          $endgroup$

















            2














            2










            2







            $begingroup$

            You are making the test !Indexs.Contains(Index). Since Indexs is a List<int> you have a look up time of O(n). A HashSet<T> has an approximate look up time of O(1). Create a hash set for this test. Since a set is not ordered, you still need the list.



            var Indexs = new List<int>();
            var indexTest = new HashSet<int>();

            Indexs.Add(0);
            indexTest.Add(0);


            and



            int i = GetPreference(BorderPixels[LastIndex], Possible.ToArray(), BorderPixels);
            Indexs.Add(i);
            indexTest.Add(i);


            and of course now test with



            if (!indexTest.Contains(Index))


            • Another point is the repeated indexed access of border pixels. Store the pixels in a temp. This also makes the code more readable.


            • You can merge all the if-statements into one conditional expression. Because of the Short-Circuit Evaluation In C#, the evaluation will stop at the first term evaluating to true.


            • You have duplicated some cases. You have 12 instead of 8. Reordering the conditions in a logical way makes it easier: x-1, x, x+1 combined with y-1, y, y+1.


            • The Boolean temp Working can be inlined.


            • You can initialize collections in the constructor or with collection initializers.


            • The C# naming conventions use camelCase for parameter names and local variables.


            • Since neither data nor borderPixels are altered, copying data into borderPixels seems superfluous. I simply renamed data to borderPixels. This change requires the type of the last parameter of GetPreference to be changed from List<Vector2F> to Vector2F[] and borderPixels.Count must be changed to borderPixels.Length.


            The new ReorderBorder method:



            private Vector2F[] ReorderBorder(Vector2F[] borderPixels)

            var indexes = new List<int> 0 ;
            var indexTest = new HashSet<int> 0 ;

            while (indexes.Count < borderPixels.Length)
            int lastIndex = indexes.Last();
            Vector2F last = borderPixels[lastIndex];

            var possible = new List<int>();
            for (int index = 0; index < borderPixels.Length; index++)
            if (!indexTest.Contains(index))
            Vector2F current = borderPixels[index];
            if (new Vector2F(last.X - 1, last.Y - 1) == current


            int preferredIndex = GetPreference(last, possible.ToArray(), borderPixels);
            indexes.Add(preferredIndex);
            indexTest.Add(preferredIndex);


            var vertices = new List<Vector2F>();
            for (int index = 0; index < indexes.Count; index++)
            vertices.Add(borderPixels[indexes[index]]);


            return vertices.ToArray();



            You can also apply some of these changes to GetPreference.






            share|improve this answer











            $endgroup$



            You are making the test !Indexs.Contains(Index). Since Indexs is a List<int> you have a look up time of O(n). A HashSet<T> has an approximate look up time of O(1). Create a hash set for this test. Since a set is not ordered, you still need the list.



            var Indexs = new List<int>();
            var indexTest = new HashSet<int>();

            Indexs.Add(0);
            indexTest.Add(0);


            and



            int i = GetPreference(BorderPixels[LastIndex], Possible.ToArray(), BorderPixels);
            Indexs.Add(i);
            indexTest.Add(i);


            and of course now test with



            if (!indexTest.Contains(Index))


            • Another point is the repeated indexed access of border pixels. Store the pixels in a temp. This also makes the code more readable.


            • You can merge all the if-statements into one conditional expression. Because of the Short-Circuit Evaluation In C#, the evaluation will stop at the first term evaluating to true.


            • You have duplicated some cases. You have 12 instead of 8. Reordering the conditions in a logical way makes it easier: x-1, x, x+1 combined with y-1, y, y+1.


            • The Boolean temp Working can be inlined.


            • You can initialize collections in the constructor or with collection initializers.


            • The C# naming conventions use camelCase for parameter names and local variables.


            • Since neither data nor borderPixels are altered, copying data into borderPixels seems superfluous. I simply renamed data to borderPixels. This change requires the type of the last parameter of GetPreference to be changed from List<Vector2F> to Vector2F[] and borderPixels.Count must be changed to borderPixels.Length.


            The new ReorderBorder method:



            private Vector2F[] ReorderBorder(Vector2F[] borderPixels)

            var indexes = new List<int> 0 ;
            var indexTest = new HashSet<int> 0 ;

            while (indexes.Count < borderPixels.Length)
            int lastIndex = indexes.Last();
            Vector2F last = borderPixels[lastIndex];

            var possible = new List<int>();
            for (int index = 0; index < borderPixels.Length; index++)
            if (!indexTest.Contains(index))
            Vector2F current = borderPixels[index];
            if (new Vector2F(last.X - 1, last.Y - 1) == current


            int preferredIndex = GetPreference(last, possible.ToArray(), borderPixels);
            indexes.Add(preferredIndex);
            indexTest.Add(preferredIndex);


            var vertices = new List<Vector2F>();
            for (int index = 0; index < indexes.Count; index++)
            vertices.Add(borderPixels[indexes[index]]);


            return vertices.ToArray();



            You can also apply some of these changes to GetPreference.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited 5 hours ago

























            answered 6 hours ago









            Olivier Jacot-DescombesOlivier Jacot-Descombes

            2,91012 silver badges19 bronze badges




            2,91012 silver badges19 bronze badges


























                1













                $begingroup$

                Use enums When in the below snippet each index really means what the numbers stand for in the comments then you definitely need at least an enum for that. This is so extremely fragile. Without these comments nobody ever would be able to decipher this logic.




                ///Relativity
                /// 0 - Up
                /// 1 - Up Right
                /// 2 - Right
                /// 3 - Down Right
                /// 4 - Down
                /// 5 - Down Left
                /// 6 - Left
                /// 7 - Up Left
                for (int Index = 0; Index < Indices.Length; Index++)






                share|improve this answer









                $endgroup$



















                  1













                  $begingroup$

                  Use enums When in the below snippet each index really means what the numbers stand for in the comments then you definitely need at least an enum for that. This is so extremely fragile. Without these comments nobody ever would be able to decipher this logic.




                  ///Relativity
                  /// 0 - Up
                  /// 1 - Up Right
                  /// 2 - Right
                  /// 3 - Down Right
                  /// 4 - Down
                  /// 5 - Down Left
                  /// 6 - Left
                  /// 7 - Up Left
                  for (int Index = 0; Index < Indices.Length; Index++)






                  share|improve this answer









                  $endgroup$

















                    1














                    1










                    1







                    $begingroup$

                    Use enums When in the below snippet each index really means what the numbers stand for in the comments then you definitely need at least an enum for that. This is so extremely fragile. Without these comments nobody ever would be able to decipher this logic.




                    ///Relativity
                    /// 0 - Up
                    /// 1 - Up Right
                    /// 2 - Right
                    /// 3 - Down Right
                    /// 4 - Down
                    /// 5 - Down Left
                    /// 6 - Left
                    /// 7 - Up Left
                    for (int Index = 0; Index < Indices.Length; Index++)






                    share|improve this answer









                    $endgroup$



                    Use enums When in the below snippet each index really means what the numbers stand for in the comments then you definitely need at least an enum for that. This is so extremely fragile. Without these comments nobody ever would be able to decipher this logic.




                    ///Relativity
                    /// 0 - Up
                    /// 1 - Up Right
                    /// 2 - Right
                    /// 3 - Down Right
                    /// 4 - Down
                    /// 5 - Down Left
                    /// 6 - Left
                    /// 7 - Up Left
                    for (int Index = 0; Index < Indices.Length; Index++)







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered 39 mins ago









                    t3chb0tt3chb0t

                    38.1k7 gold badges61 silver badges142 bronze badges




                    38.1k7 gold badges61 silver badges142 bronze badges
























                        1













                        $begingroup$

                        DRY Principle



                        As a small addendum on Olivier Jacot-Descombes' answer, I would like to add you should go for DRY code.



                        answer snippet:




                        "You have duplicated some cases. You have 12 instead of 8. Reordering the conditions in a logical way makes it easier: x-1, x, x+1 combined
                        with y-1, y, y+1."




                        This..



                        var current = BorderPixels[Index];
                        var last = BorderPixels[LastIndex];
                        if (Math.Abs(current.X - last.X) <= 1 && Math.Abs(current.Y - last.Y) <= 1)

                        Possible.Add(Index);



                        replaces..




                        if (new Vector2F(BorderPixels[LastIndex].X + 1, BorderPixels[LastIndex].Y) == BorderPixels[Index])
                        Possible.Add(Index);
                        else if (new Vector2F(BorderPixels[LastIndex].X + 1, BorderPixels[LastIndex].Y + 1) == BorderPixels[Index])
                        Possible.Add(Index);
                        else if (new Vector2F(BorderPixels[LastIndex].X + 1, BorderPixels[LastIndex].Y - 1) == BorderPixels[Index])
                        Possible.Add(Index);
                        else if (new Vector2F(BorderPixels[LastIndex].X, BorderPixels[LastIndex].Y + 1) == BorderPixels[Index])
                        Possible.Add(Index);
                        else if (new Vector2F(BorderPixels[LastIndex].X - 1, BorderPixels[LastIndex].Y + 1) == BorderPixels[Index])
                        Possible.Add(Index);
                        else if (new Vector2F(BorderPixels[LastIndex].X + 1, BorderPixels[LastIndex].Y + 1) == BorderPixels[Index])
                        Possible.Add(Index);
                        else if (new Vector2F(BorderPixels[LastIndex].X - 1, BorderPixels[LastIndex].Y) == BorderPixels[Index])
                        Possible.Add(Index);
                        else if (new Vector2F(BorderPixels[LastIndex].X - 1, BorderPixels[LastIndex].Y + 1) == BorderPixels[Index])
                        Possible.Add(Index);
                        else if (new Vector2F(BorderPixels[LastIndex].X - 1, BorderPixels[LastIndex].Y - 1) == BorderPixels[Index])
                        Possible.Add(Index);
                        else if (new Vector2F(BorderPixels[LastIndex].X, BorderPixels[LastIndex].Y - 1) == BorderPixels[Index])
                        Possible.Add(Index);
                        else if (new Vector2F(BorderPixels[LastIndex].X - 1, BorderPixels[LastIndex].Y - 1) == BorderPixels[Index])
                        Possible.Add(Index);
                        else if (new Vector2F(BorderPixels[LastIndex].X + 1, BorderPixels[LastIndex].Y - 1) == BorderPixels[Index])
                        Possible.Add(Index);






                        share|improve this answer











                        $endgroup$



















                          1













                          $begingroup$

                          DRY Principle



                          As a small addendum on Olivier Jacot-Descombes' answer, I would like to add you should go for DRY code.



                          answer snippet:




                          "You have duplicated some cases. You have 12 instead of 8. Reordering the conditions in a logical way makes it easier: x-1, x, x+1 combined
                          with y-1, y, y+1."




                          This..



                          var current = BorderPixels[Index];
                          var last = BorderPixels[LastIndex];
                          if (Math.Abs(current.X - last.X) <= 1 && Math.Abs(current.Y - last.Y) <= 1)

                          Possible.Add(Index);



                          replaces..




                          if (new Vector2F(BorderPixels[LastIndex].X + 1, BorderPixels[LastIndex].Y) == BorderPixels[Index])
                          Possible.Add(Index);
                          else if (new Vector2F(BorderPixels[LastIndex].X + 1, BorderPixels[LastIndex].Y + 1) == BorderPixels[Index])
                          Possible.Add(Index);
                          else if (new Vector2F(BorderPixels[LastIndex].X + 1, BorderPixels[LastIndex].Y - 1) == BorderPixels[Index])
                          Possible.Add(Index);
                          else if (new Vector2F(BorderPixels[LastIndex].X, BorderPixels[LastIndex].Y + 1) == BorderPixels[Index])
                          Possible.Add(Index);
                          else if (new Vector2F(BorderPixels[LastIndex].X - 1, BorderPixels[LastIndex].Y + 1) == BorderPixels[Index])
                          Possible.Add(Index);
                          else if (new Vector2F(BorderPixels[LastIndex].X + 1, BorderPixels[LastIndex].Y + 1) == BorderPixels[Index])
                          Possible.Add(Index);
                          else if (new Vector2F(BorderPixels[LastIndex].X - 1, BorderPixels[LastIndex].Y) == BorderPixels[Index])
                          Possible.Add(Index);
                          else if (new Vector2F(BorderPixels[LastIndex].X - 1, BorderPixels[LastIndex].Y + 1) == BorderPixels[Index])
                          Possible.Add(Index);
                          else if (new Vector2F(BorderPixels[LastIndex].X - 1, BorderPixels[LastIndex].Y - 1) == BorderPixels[Index])
                          Possible.Add(Index);
                          else if (new Vector2F(BorderPixels[LastIndex].X, BorderPixels[LastIndex].Y - 1) == BorderPixels[Index])
                          Possible.Add(Index);
                          else if (new Vector2F(BorderPixels[LastIndex].X - 1, BorderPixels[LastIndex].Y - 1) == BorderPixels[Index])
                          Possible.Add(Index);
                          else if (new Vector2F(BorderPixels[LastIndex].X + 1, BorderPixels[LastIndex].Y - 1) == BorderPixels[Index])
                          Possible.Add(Index);






                          share|improve this answer











                          $endgroup$

















                            1














                            1










                            1







                            $begingroup$

                            DRY Principle



                            As a small addendum on Olivier Jacot-Descombes' answer, I would like to add you should go for DRY code.



                            answer snippet:




                            "You have duplicated some cases. You have 12 instead of 8. Reordering the conditions in a logical way makes it easier: x-1, x, x+1 combined
                            with y-1, y, y+1."




                            This..



                            var current = BorderPixels[Index];
                            var last = BorderPixels[LastIndex];
                            if (Math.Abs(current.X - last.X) <= 1 && Math.Abs(current.Y - last.Y) <= 1)

                            Possible.Add(Index);



                            replaces..




                            if (new Vector2F(BorderPixels[LastIndex].X + 1, BorderPixels[LastIndex].Y) == BorderPixels[Index])
                            Possible.Add(Index);
                            else if (new Vector2F(BorderPixels[LastIndex].X + 1, BorderPixels[LastIndex].Y + 1) == BorderPixels[Index])
                            Possible.Add(Index);
                            else if (new Vector2F(BorderPixels[LastIndex].X + 1, BorderPixels[LastIndex].Y - 1) == BorderPixels[Index])
                            Possible.Add(Index);
                            else if (new Vector2F(BorderPixels[LastIndex].X, BorderPixels[LastIndex].Y + 1) == BorderPixels[Index])
                            Possible.Add(Index);
                            else if (new Vector2F(BorderPixels[LastIndex].X - 1, BorderPixels[LastIndex].Y + 1) == BorderPixels[Index])
                            Possible.Add(Index);
                            else if (new Vector2F(BorderPixels[LastIndex].X + 1, BorderPixels[LastIndex].Y + 1) == BorderPixels[Index])
                            Possible.Add(Index);
                            else if (new Vector2F(BorderPixels[LastIndex].X - 1, BorderPixels[LastIndex].Y) == BorderPixels[Index])
                            Possible.Add(Index);
                            else if (new Vector2F(BorderPixels[LastIndex].X - 1, BorderPixels[LastIndex].Y + 1) == BorderPixels[Index])
                            Possible.Add(Index);
                            else if (new Vector2F(BorderPixels[LastIndex].X - 1, BorderPixels[LastIndex].Y - 1) == BorderPixels[Index])
                            Possible.Add(Index);
                            else if (new Vector2F(BorderPixels[LastIndex].X, BorderPixels[LastIndex].Y - 1) == BorderPixels[Index])
                            Possible.Add(Index);
                            else if (new Vector2F(BorderPixels[LastIndex].X - 1, BorderPixels[LastIndex].Y - 1) == BorderPixels[Index])
                            Possible.Add(Index);
                            else if (new Vector2F(BorderPixels[LastIndex].X + 1, BorderPixels[LastIndex].Y - 1) == BorderPixels[Index])
                            Possible.Add(Index);






                            share|improve this answer











                            $endgroup$



                            DRY Principle



                            As a small addendum on Olivier Jacot-Descombes' answer, I would like to add you should go for DRY code.



                            answer snippet:




                            "You have duplicated some cases. You have 12 instead of 8. Reordering the conditions in a logical way makes it easier: x-1, x, x+1 combined
                            with y-1, y, y+1."




                            This..



                            var current = BorderPixels[Index];
                            var last = BorderPixels[LastIndex];
                            if (Math.Abs(current.X - last.X) <= 1 && Math.Abs(current.Y - last.Y) <= 1)

                            Possible.Add(Index);



                            replaces..




                            if (new Vector2F(BorderPixels[LastIndex].X + 1, BorderPixels[LastIndex].Y) == BorderPixels[Index])
                            Possible.Add(Index);
                            else if (new Vector2F(BorderPixels[LastIndex].X + 1, BorderPixels[LastIndex].Y + 1) == BorderPixels[Index])
                            Possible.Add(Index);
                            else if (new Vector2F(BorderPixels[LastIndex].X + 1, BorderPixels[LastIndex].Y - 1) == BorderPixels[Index])
                            Possible.Add(Index);
                            else if (new Vector2F(BorderPixels[LastIndex].X, BorderPixels[LastIndex].Y + 1) == BorderPixels[Index])
                            Possible.Add(Index);
                            else if (new Vector2F(BorderPixels[LastIndex].X - 1, BorderPixels[LastIndex].Y + 1) == BorderPixels[Index])
                            Possible.Add(Index);
                            else if (new Vector2F(BorderPixels[LastIndex].X + 1, BorderPixels[LastIndex].Y + 1) == BorderPixels[Index])
                            Possible.Add(Index);
                            else if (new Vector2F(BorderPixels[LastIndex].X - 1, BorderPixels[LastIndex].Y) == BorderPixels[Index])
                            Possible.Add(Index);
                            else if (new Vector2F(BorderPixels[LastIndex].X - 1, BorderPixels[LastIndex].Y + 1) == BorderPixels[Index])
                            Possible.Add(Index);
                            else if (new Vector2F(BorderPixels[LastIndex].X - 1, BorderPixels[LastIndex].Y - 1) == BorderPixels[Index])
                            Possible.Add(Index);
                            else if (new Vector2F(BorderPixels[LastIndex].X, BorderPixels[LastIndex].Y - 1) == BorderPixels[Index])
                            Possible.Add(Index);
                            else if (new Vector2F(BorderPixels[LastIndex].X - 1, BorderPixels[LastIndex].Y - 1) == BorderPixels[Index])
                            Possible.Add(Index);
                            else if (new Vector2F(BorderPixels[LastIndex].X + 1, BorderPixels[LastIndex].Y - 1) == BorderPixels[Index])
                            Possible.Add(Index);







                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited 30 mins ago

























                            answered 1 hour ago









                            dfhwzedfhwze

                            10.3k2 gold badges19 silver badges67 bronze badges




                            10.3k2 gold badges19 silver badges67 bronze badges






























                                draft saved

                                draft discarded
















































                                Thanks for contributing an answer to Code Review Stack Exchange!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid


                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.

                                Use MathJax to format equations. MathJax reference.


                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function ()
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f227130%2fordering-2d-border-points%23new-answer', 'question_page');

                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                ParseJSON using SSJSUsing AMPscript with SSJS ActivitiesHow to resubscribe a user in Marketing cloud using SSJS?Pulling Subscriber Status from Lists using SSJSRetrieving Emails using SSJSProblem in updating DE using SSJSUsing SSJS to send single email in Marketing CloudError adding EmailSendDefinition using SSJS

                                Кампала Садржај Географија Географија Историја Становништво Привреда Партнерски градови Референце Спољашње везе Мени за навигацију0°11′ СГШ; 32°20′ ИГД / 0.18° СГШ; 32.34° ИГД / 0.18; 32.340°11′ СГШ; 32°20′ ИГД / 0.18° СГШ; 32.34° ИГД / 0.18; 32.34МедијиПодациЗванични веб-сајту

                                19. јануар Садржај Догађаји Рођења Смрти Празници и дани сећања Види још Референце Мени за навигацијуу