I need to retrieve five stars from a group of stars so I can hide them by turning their visible flag to “false”.
I know the x, y coordinate value of each star I am looking for. My algorithm opens a transaction and retrieves a group of stars whose x, y values falls within the extents of the five stars I am looking for.
This is faster than opening five separate transactions and looking at a particular x, y location of each star individually.
With the group of stars below what I want to do is to compare the x, y value of each star to find a match to the one of the five I am looking for.
Group of 400+ Stars to search
*********************************************
*********************************************
*********************************************
*********************************************
*********************************************
*********************************************
*********************************************
*********************************************
*********************************************
My thoughts on this is I have two choices for iterating through the collection:
(1) I can iterate through the 400+ stars one at a time, grab the x, y values of each and then iterate through the five sets of coordinates to see if it matches one of the five I am looking for.
(2) I can iterate through the “five stars to find” one at a time and then iterate the 400+ stars collection looking for a match
NB:
The stars collection has 400+ stars that fall within the extents.
The “visible” property only works for items in the stars collection.
The stars_to_find collection has the coordinates of the five stars I am looking for.
Code snippets
(1)
foreach (Star star in stars)
{
foreach (Star star_to_find in stars_to_find)
{
if (star.Coordinates.Equals(star_to_find.Coordinates))
{
star.Visible = false;
}
}
}
(2)
foreach (Star star_to_find in stars_to_find)
{
foreach (Star star in stars)
{
if (star.Coordinates.Equals(star_to_find.Coordinates))
{
star.Visible = false;
}
}
}
Is either search method, (1) or (2), faster than the other? Is there a different search algorithm that would work better?