I hope you can help me out on this one. I have to do some "special sorting" of a list of points (x,y,z coordinates) as a part of a larger code.
i) Always have a column with all the values equal zero.
ii) The first and the last point will always be the same.
The sorting of the list will depend on which column is equal to zero. I can identify this column myself with no problem (see code) but I am struggling when it comes with the sorting bit. First I have to find a the point that meet some specific criteria, then reorganise the list based on this. I have explained the sorting criteria as comments in the code (see sections 2 and 3).
If you could provide assistance on 2) and 3) that would be great (the first part is ok I think).
using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
[STAThread]
public static void Main(string[] args)
{
//Populating the list
var ListPoints = new List();
double[] P0 = { 10, 10, 0 };
double[] P1 = { 10, 0, 0 };
double[] P2 = { 0, 0, 0 };
double[] P3 = { 0, 10, 0 };
ListPoints.Add(P0);
ListPoints.Add(P1);
ListPoints.Add(P2);
ListPoints.Add(P3);
ListPoints.Add(P0);
///This list (by definition) will:
/// i) Always have a column with all the values equal zero
/// ii) The first and the last point will always be the same.
///We need to detect the column with all values = zero, because the list sorting will depend on this.
/// 1) Detect which columns has all values equal to zero using the columnZero variable
var counterX = new List();
var counterY = new List();
var counterZ = new List();
for (int i = 0; i < ListPoints.Count - 1; i++)
{
//First column with all values equal zero
if (ListPoints[i][0] == 0 && ListPoints[i][0] == ListPoints[i + 1][0]) { counterX.Add(1); }
//Second column with all values equal zero
if (ListPoints[i][1] == 0 && ListPoints[i][1] == ListPoints[i + 1][1]) { counterY.Add(1); }
//Third column with all values equal zero
if (ListPoints[i][2] == 0 && ListPoints[i][2] == ListPoints[i + 1][2]) { counterZ.Add(1); }
}
if (counterX.Count == ListPoints.Count - 1)
{ Console.WriteLine("all points of the 1st column are zero");}
if (counterY.Count == ListPoints.Count - 1)
{ Console.WriteLine("all points of the 2nd column are zero");}
if (counterZ.Count == ListPoints.Count - 1)
{ Console.WriteLine("all points of the 3rd column are zero");}
/// 2) Now a point "Q" must be found in the list according to this:
/// 2.1/ If the first column has all values = zero:
/// Find the row index of the smallest value in the second column.
/// If there are several rows in the second column with the same minimum value go and find between those which one has the smallest value in the third column.
/// If there is only one value in the second column keep that one.
/// 2.2/ If the second column has all values = zero:
/// Find the row index of the smallest value in the first column.
/// If there are several rows in the first column with the same minimum value go and find between those which one has the smallest value in the third column.
/// If there is only one value in the first column keep that one.
/// 2.3/ If the third column has all values = zero:
/// Find the row index of the smallest value in the first column.
/// If there are several rows in the first column with the same minimum value go and find between those which one has the smallest value in the second column.
/// If there is only one value in the first column keep that one.
///
/// 3) Once this value has been found we have to put the list starting by this point "Q", then copy the previous values at the end of the list and finally add again "Q".
/// Example:The column with all values = 0 is column 3 and the generic point "Q" is the point "P2" in my list. The input is P0-P1-P2-P3-P0 but I want to have it P2-P3-P0-P1-P2.
}
}
VulpesPosted Jan 22, 2015, 3:02 PM
xlApp.Visible = false;
HappyCode HappyCodePosted Jan 22, 2015, 12:41 PM
VulpesPosted Jan 20, 2015, 3:21 PM
Anyway I've sorted all this out and it seems to be working OK now.
Here's the revised code with the changes highlighted:
HappyCode HappyCodePosted Jan 20, 2015, 11:56 AM
VulpesPosted Jan 19, 2015, 8:00 PM
There's some code in this thread which looks like it should work though I don't have time just now to test it:
https://social.msdn.microsoft.com/Forums/vstudio/en-US/cb9e0445-fc71-4eae-8742-80b2c831ed22/how-to-append-existing-excel-file-using-c-?forum=vstest
If you can't get it to work as you want it to, I'll have another look tomorrow.
HappyCode HappyCodePosted Jan 19, 2015, 12:17 PM
Many thanks for this. The possibility of having more than one column=0 is not a problem because if I have two columns equal to zero (the three of them will never be equal to zero at the same time by definition) the remaining one will be organised based on min value basis (desired result) and this is indirectly specified in the conditions you have kindly set in the code. In other words, it is working perfectly as is! :)
On the other hand, I would like to ask you about an efficient way to print out "programatically" this list "ListPoints" to an existent excel file without deleting the previous content in the excel. The reason is because this list as part of a larger code will be filled and cleared programatically and I want to keep a copy of the values of the list in an excel file before the list is cleared.
The final excel should look like this (including the words Start and End in column A):
A B C
Start //List1 is copied below
15 18 0
20 40 50
12 15 16
End //List1 is cleared
Start
7 90 16 //New List1 is copied below
20 15 50
12 15 8
End //List1 is cleared
...Do you have any idea?
Thank you for your kind help again!
PS: Regarding the triangle problem... I have decided to move on and put this problem aside for the moment (I will have to be back solving this).
VulpesPosted Jan 15, 2015, 4:47 PM
Did you ever figure out the problem with the triangles? I seem to recall that there was a problem with everything we tried :(
Anyway this problem looks more tractable. Here's my attempt at 2) & 3) :
The output is:
Incidentally, I've assumed that there will be just one column whose values are all zero as I'm not sure what should happen if there's more than one.