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

How to handle inventory and story of a player leaving

Why does Sauron not permit his followers to use his name?

Scaling arrows.meta with tranform shape

Why is there no willingness in the international community to step in between Pakistan and India?

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

Create a list of snaking numbers under 50,000

How can I fix cracks between the bathtub and the wall surround?

Why do presidential pardons exist in a country having a clear separation of powers?

What caused the end of cybernetic implants?

What is the sound/audio equivalent of "unsightly"?

Don't look at what I did there

How to understand payment due date for credit card?

Do universities maintain secret textbooks?

'Horseshoes' for Deer?

Pen test results for web application include a file from a forbidden directory that is not even used or referenced

Ordering 2D Border Points

Why didn't Doc believe Marty was from the future?

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

Was a six-engine 747 ever seriously considered by Boeing?

Fixing a blind bolt hole when the first 2-3 threads are ruined?

having problems with greek characters in a table using csvsimple

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

Is it recommended to point out a professor's mistake during their lecture?

Where should I draw the line on follow up questions from previous employer



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
    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$
    @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
    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$
    @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 35 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
    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$
    @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
    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$
    @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
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
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$
@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))


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


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


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


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


            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 31 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 22 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

                                Invision Community Contents History See also References External links Navigation menuProprietaryinvisioncommunity.comIPS Community ForumsIPS Community Forumsthis blog entry"License Changes, IP.Board 3.4, and the Future""Interview -- Matt Mecham of Ibforums""CEO Invision Power Board, Matt Mecham Is a Liar, Thief!"IPB License Explanation 1.3, 1.3.1, 2.0, and 2.1ArchivedSecurity Fixes, Updates And Enhancements For IPB 1.3.1Archived"New Demo Accounts - Invision Power Services"the original"New Default Skin"the original"Invision Power Board 3.0.0 and Applications Released"the original"Archived copy"the original"Perpetual licenses being done away with""Release Notes - Invision Power Services""Introducing: IPS Community Suite 4!"Invision Community Release Notes

                                Canceling a color specificationRandomly assigning color to Graphics3D objects?Default color for Filling in Mathematica 9Coloring specific elements of sets with a prime modified order in an array plotHow to pick a color differing significantly from the colors already in a given color list?Detection of the text colorColor numbers based on their valueCan color schemes for use with ColorData include opacity specification?My dynamic color schemes

                                Ласкавець круглолистий Зміст Опис | Поширення | Галерея | Примітки | Посилання | Навігаційне меню58171138361-22960890446Bupleurum rotundifoliumEuro+Med PlantbasePlants of the World Online — Kew ScienceGermplasm Resources Information Network (GRIN)Ласкавецькн. VI : Літери Ком — Левиправивши або дописавши її