mercredi 27 juillet 2016

Why I am getting two different results for the same design?

Quick question:

I have a bunch (cloud) of coordinates and I tend to find the four corner coordinates of the whole bunch. And by corner I mean:

MyDesiredResult = { SmallestX, BiggestY, BiggestX, SmallestY }

I use this Old Routine to get my values:

double smallestX = MyCloud[0].X;       // assing X of first element to my result controller
var tempCoordPoint = MyCloud[0];       // assign first element to my result controller

for (int i = 0; i < MyCloud.Count; i++)
{
    if (smallestX > MyCloud[i].X)      // find minimum X
    {
        smallestX = MyCloud[i].X;
        tempCoordPoint = MyCloud[i];
    }
}

MyResult.Add(tempCoordPoint);           // add to my list

However, I would need to that Four Times (for the Four results). So I am trying to optimize my code by changing it to this New Routine which I use only once:

List<CoordPoint> MySortedList = MyCloud.Select(c => new CoordPoint { X = c.X, Y = c.Y, Z = c.Z, Color = c.Color }).ToList();

MySortedList.Sort((c1, c2) => c1.X.CompareTo(c2.X));     // sort on X
var temp = MySortedList[MySortedList.Count - 1];         // hold biggest X in a temp variable
MyResult.Add(MySortedList[0]);                           // add smallest X to my result

MySortedList.Sort((c1, c2) => c1.Y.CompareTo(c2.Y)); ;   // sort on Y
MyResult.Add(MySortedList[MySortedList.Count - 1]);      // add biggest Y to my result 
MyResult.Add(temp);                                      // add biggest X to my result
MyResult.Add(MySortedList[0]);                           // add smallest Y to my result

But it gives different results. I would want to show a sample input, current output and a desired output. I can skip the sample input (huge load) and show the results. Could anyone point me towards what I am doing wrong?

P.S The old routine is the correct one!

For the same input:

Result From Old Routine:

(0, 4), (15, 12), (23, 6), (19, 0)

Result From New Routine:

(0, 4), (18, 12), (23, 6), (18, 0)

enter image description here

Aucun commentaire:

Enregistrer un commentaire