I have the method that does a calculation and adds the data to a collection list. Below is my code
IterateDtColl iterateDataList = new IterateDtColl();
double answer;
//Array to hold the previous initail tempreture
double[] TempArray = new double[9];
for (int tVal = 0; tVal < exdatalist.count(); tVal++)
{
ValidateDataColl validatelist = new ValidateDataColl();
//Assign my initial tempreture
if (iterateDataList.count() > 0)
{
TempArray[0] = iterateDataList.getexceldata(tVal - 1).Pi1;
TempArray[1] = iterateDataList.getexceldata(tVal - 1).Pi2;
TempArray[2] = iterateDataList.getexceldata(tVal - 1).Pi3;
TempArray[3] = iterateDataList.getexceldata(tVal - 1).Pi4;
TempArray[4] = iterateDataList.getexceldata(tVal - 1).Pi5;
TempArray[5] = iterateDataList.getexceldata(tVal - 1).Pi6;
TempArray[6] = iterateDataList.getexceldata(tVal - 1).Pi7;
TempArray[7] = iterateDataList.getexceldata(tVal - 1).Pi8;
TempArray[8] = iterateDataList.getexceldata(tVal - 1).Pi9;
}
else
{
TempArray[0] = Convert.ToDouble(initProbTxtBx.Text);
TempArray[1] = TempArray[0];
TempArray[2] = TempArray[0];
TempArray[3] = TempArray[0];
TempArray[4] = TempArray[0];
TempArray[5] = TempArray[0];
TempArray[6] = TempArray[0];
TempArray[7] = TempArray[0];
TempArray[8] = TempArray[0];
}
//Holds Value for last calculated values..
double[] LastCalValue = new double[9];
for (int iVal = 0; iVal < 9; iVal++)
{
answer = 0.0;
if (iVal == 0)
{
answer = (TempArray[iVal] + (r_val(dxval(lval, nval)) * (exdatalist.getexceldata(tVal).CentreTemp + initVal))) / (1 + (2 * r_val(dxval(lval, nval))));
}
else if (iVal == 8)
{
answer = (TempArray[iVal] + (r_val(dxval(lval, nval)) * (LastCalValue[iVal - 1] + exdatalist.getexceldata(tVal).SurfaceTemp))) / (1 + (2 * r_val(dxval(lval, nval))));
}
else
{
answer = (TempArray[iVal] + (r_val(dxval(lval, nval)) * (LastCalValue[iVal - 1] + initVal))) / (1 + (2 * r_val(dxval(lval, nval))));
}
//Hold the values at the present index so it can be used at the next index
LastCalValue[iVal] = answer;
//Adds the data to be validated to my collection list
validatelist.addvaliddt(new ValidateDataCl(initVal, LastCalValue[iVal], TempArray[iVal]));
}
I want to check that the difference between the initVal and LastCalValue[index] in the validatelist is less than 0.0001, if it is not, then do the calculation below till the condition is met.
for (int check = 0; check < validate.count(); check++)
{
double newAnswer;
double NewVal = 0.0;
if (check == 0)
{
//Set my new value to the value at the next index(Which then becomes my guess)
NewVal = validate.getvaliddata(check + 1).NewValue;
newAnswer = (validate.getvaliddata(check).InitTemp + (r_val(dxval(lval, nval)) * (CentreTemp + NewVal))) / (1 + (2 * r_val(dxval(lval, nval))));
}
else if (check == 8)
{
NewVal = OldVal[check - 1];
newAnswer = (validate.getvaliddata(check).InitTemp + (r_val(dxval(lval, nval)) * (NewVal + SurfaceTemp))) / (1 + (2 * r_val(dxval(lval, nval))));
}
else
{
NewVal = validate.getvaliddata(check + 1).NewValue;
newAnswer = (validate.getvaliddata(check).InitTemp + (r_val(dxval(lval, nval)) * (OldVal[check - 1] + NewVal))) / (1 + (2 * r_val(dxval(lval, nval))));
}
validate.getvaliddata(check).InitGuess = NewVal;
validate.getvaliddata(check).NewValue = newAnswer;
OldVal[check] = newAnswer;
}
//ValidateDataColl ValidatedList = CalNewData(validatelist, exdatalist.getexceldata(tVal).CentreTemp, exdatalist.getexceldata(tVal).SurfaceTemp);
iterateDataList.additerdt(new IterateClass(exdatalist.getexceldata(tVal).CentreTemp, ValidatedList.getvaliddata(0).NewValue, ValidatedList.getvaliddata(1).NewValue, ValidatedList.getvaliddata(2).NewValue, ValidatedList.getvaliddata(3).NewValue, ValidatedList.getvaliddata(4).NewValue, ValidatedList.getvaliddata(5).NewValue, ValidatedList.getvaliddata(6).NewValue, ValidatedList.getvaliddata(7).NewValue, ValidatedList.getvaliddata(8).NewValue, exdatalist.getexceldata(tVal).SurfaceTemp));
}
return iterateDataList;
How can I do that?
Related
I am trying to implement the Transvoxel algorithm for a game I am working on but I'm noticing some weird issues in the mesh generated:
Not sure why there are gaps and why the sphere has fins behind it.
For some reason, the mesh has gaps between chunks, and it has these strange fins extending out behind it. I haven't been able to figure out why this is happening but I have an inkling it may have to do with either the function that generates the density values for each Chunk (16x16x16 Block of cells):
private float SampleDensity(float3 _point)
{
var _worldPos = (_point / (m_WorldRadius - 1.0f) - 0.5f) * m_VolumeFieldSize;
var _halfS = m_VolumeFieldSize / 2;
var _maxD= Magnitude(new float3(_halfS, _halfS, _halfS));
var _fudge = 1f;
var _density = Magnitude(_worldPos) / (_maxD + _fudge) - 0.5f;
var _noise = CalculateNoise(_worldPos);
_density += _noise;
return _density;
}
public void Execute()
{
for (int _z = 0; _z < m_ChunkSize; _z++)
{
for (int _y = 0; _y < m_ChunkSize; _y++)
{
for (int _x = 0; _x < m_ChunkSize; _x++)
{
var _point = (new float3(_x, _y, _z) + (m_GPosition)); // Multiplying or dividing this value adjusts the final mesh's scale.
var _valueAtPoint = SampleDensity(_point);
m_Cells[_z * m_ChunkSize * m_ChunkSize + _y * m_ChunkSize + _x] = _valueAtPoint;
}
}
}
}
, or the mesh generating code itself:
for (int _z = 0; _z < _chunk.m_ChunkSize; _z++)
{
for (int _y = 0; _y < _chunk.m_ChunkSize; _y++)
{
for (int _x = 0; _x < _chunk.m_ChunkSize; _x++)
{
var _cubeConfiguration = 0;
for (int _i = 0; _i < 8; _i++)
{
_cornerPos = CornerIndex[_i];
_densityValues[_i] = _chunk.GetCell(_x + _cornerPos.x, _y + _cornerPos.y, _z + _cornerPos.z);
if (_densityValues[_i] < _chunk.m_World.m_ISOLevel) _cubeConfiguration |= (1 << _i);
}
var _caseCode = (byte) _cubeConfiguration;
if (_caseCode == 0) continue;
for (int _i = 0; _i < _cornerNormals.Length; _i++)
{
_cornerPos = CornerIndex[_i];
_cornerNormals[_i] = new Vector3(
_chunk.GetCell(_x + _cornerPos.x - 1, _y + _cornerPos.y, _z + _cornerPos.z) - _chunk.GetCell(_x + _cornerPos.x + 1, _y + _cornerPos.y, _z + _cornerPos.z),
_chunk.GetCell(_x + _cornerPos.x, _y + _cornerPos.y - 1, _z + _cornerPos.z) - _chunk.GetCell(_x + _cornerPos.x, _y + _cornerPos.y + 1, _z + _cornerPos.z),
_chunk.GetCell(_x + _cornerPos.x, _y + _cornerPos.y, _z + _cornerPos.z - 1) - _chunk.GetCell(_x + _cornerPos.x, _y + _cornerPos.y, _z + _cornerPos.z + 1)
);
}
var _cellClass = Transvoxel.regularCellClass[_caseCode];
Debug.Log($"Cell Class {_cellClass}");
var _vertexLocations = Transvoxel.RegularVertexData[_caseCode];
var _cellData = Transvoxel.RegularCellDatas[_cellClass];
var _vertexCount = _cellData.GetVertexCount();
var _triangleCount = _cellData.GetTriangleCount();
var _indexOffset = _cellData.vertexIndex;
for (int _i = 0; _i < _vertexCount; _i++)
{
ushort _edge = (ushort)(_vertexLocations[_i] & 255);
byte _v0 = (byte)((_edge >> 4) & 0x0F); //First Corner Index
byte _v1 = (byte)(_edge & 0x0F); //Second Corner Index
float _t = (_densityValues[_v1]) / (_densityValues[_v1] - _densityValues[_v0]);
Vector3 _p0 = new Vector3((_x + CornerIndex[_v0].x), (_y + CornerIndex[_v0].y), (_z + CornerIndex[_v0].z));
Vector3 _p1 = new Vector3((_x + CornerIndex[_v1].x), (_y + CornerIndex[_v1].y), (_z + CornerIndex[_v1].z));
var _position = (_p0 * _t) + ((1 - _t) * _p1);
var _n0 = CalculateNormal(_chunk, new Vector3Int((int) _p0.x, (int) _p0.y, (int) _p0.z));
var _n1 = CalculateNormal(_chunk, new Vector3Int((int) _p1.x, (int) _p1.y, (int) _p1.z));
var _normal = (_n0 + _t * (_n1 - _n0)).normalized;
m_Normals.Add(_normal);
m_Vertices.Add(_position);
m_UVs.Add(UvOffset[m_VCase]);
m_VCase = (byte)(m_VCase == 3 ? 0 : m_VCase + 1);
m_LocalVertexMapping[_i] = (ushort)(m_Vertices.Count - 1);
}
for (int _t = 0; _t < _triangleCount; _t++)
{
int _tm = _t * 3;
m_Triangles.Add(m_LocalVertexMapping[_indexOffset[_tm]]);
m_Triangles.Add(m_LocalVertexMapping[_indexOffset[_tm + 1]]);
m_Triangles.Add(m_LocalVertexMapping[_indexOffset[_tm + 2]]);
}
}
}
}
I am at a complete loss and would really appreciate some help as I've been racking my brains for close to 2 months on this, along with reading several papers on different MC Algorithms (Lengyel's included) for potential inspiration. Thank You.
I want to copy excel to word as a picture without using the clipboard.
The clipboard is too easy for the user to manipulate while the program is running and it sometimes misses items.
How do I do this?
Heres How I handle it now
foreach (Worksheet WS in WB.Worksheets)
{
progresspercent += steps;
worker.ReportProgress(progresspercent);
if (WS.UsedRange.Count > 1 && WS != null)
{
Thread.Sleep(1000);
var height = WS.UsedRange.Height;
var width = WS.UsedRange.Width;
var MaxHeight = ((double)width * 8.5) / 6.5;
var heightRatio= (double)height/MaxHeight;
double TotalHeight = 0;
int RowsSoFar = 0;
int TotalRows = WS.UsedRange.Rows.Count;
int RowStart = 1;
int RowEnd = RowStart + (int)Math.Floor(TotalRows/heightRatio);
if (RowEnd > TotalRows + 1) { RowEnd = TotalRows + 1; }
///Calculate Image sizes and size copy for page
while (TotalRows > RowsSoFar)
{
Range CopyRange = WS.Range["A" + RowStart.ToString() + ":F" + RowEnd.ToString()];
double SelectionHeight = (double)CopyRange.Height;
while (SelectionHeight > MaxHeight )
{
//***************************************//
RowEnd = RowEnd - 1;
CopyRange = WS.Range["A" + RowStart.ToString() + ":F" + RowEnd.ToString()];
SelectionHeight = (double)CopyRange.Height;
}
FullWordDoc.Words.Last.InsertBreak(Microsoft.Office.Interop.Word.WdBreakType.wdPageBreak);
string ContinuedPrompt = "";
if (RowStart!=1) { ContinuedPrompt = " (Continued)"; }
FullWordDoc.Words.Last.InsertAfter(WS.Name + ContinuedPrompt + "\r");
CopyRange.CopyPicture();
FullWordDoc.Words.Last.Paste();
Console.WriteLine(WS.Name);
RowsSoFar = RowsSoFar + RowEnd - RowStart + 1;
RowStart = RowEnd + 1;
RowEnd = RowStart + (int)Math.Floor(TotalRows / heightRatio);
if (RowEnd > TotalRows + 1) { RowEnd = TotalRows + 1; }
}
}
}
I have two project: a class library and a winform and all of the calculations is in the class library. now I want to use a progress bar for one of my forms when the user click on Run botton. I tried the below code but the progress bar filled as soon as loading data (about 6000 or more) in datagridview.
Code in Winform project:
private void BtnRun_Click(object sender, EventArgs e)
{
progressBar1.Maximum = TTools.Depth.Count;
progressBar1.Step = 1;
foreach (double doub in TLitho.CalcPercent().Item3)
{
progressBar1.PerformStep();
}
if (TTools.Depth != null)
{
dataGridView1.AllowUserToAddRows = false;
List<double> DolomitePer = TLitho.CalcPercent().Item3;
List<double> Depth = TTools.Depth;
var sourceD = new BindingSource
{
DataSource = Depth
};
for (int i = 0; i < (sourceD.Count); i++)
{
dataGridView1.Rows.Add();
dataGridView1.Rows[i].Cells[0].Value = sourceD[i];
}
var sourcePD = new BindingSource
{
DataSource = DolomitePer
};
for (int i = 0; i < (sourcePD.Count); i++)
{
dataGridView1.Rows.Add();
dataGridView1.Rows[i].Cells[1].Value = sourcePD[i];
}
}
}
code in class library project:
public static Tuple<List<double>, List<double>, List<double>> CalcPercent()
{
for (int j = 0; j < TTools.Depth.Count; j++)
{
MinimumIndexQC.Add(80000);
MinimumIndexQC[j] = lQC[j].IndexOf(lQC[j].Min());
MinimumIndexCD.Add(80000);
MinimumIndexCD[j] = lCD[j].IndexOf(lCD[j].Min());
MinimumIndexQD.Add(80000);
MinimumIndexQD[j] = lQD[j].IndexOf(lQD[j].Min());
CDandND.Add(Math.Abs(lCD[j][MinimumIndexCD[j]] - TPorosity.CalculateNDPorosity()[j]));
QCandND.Add(Math.Abs(lQC[j][MinimumIndexQC[j]] - TPorosity.CalculateNDPorosity()[j]));
QDandND.Add(Math.Abs(lQD[j][MinimumIndexQD[j]] - TPorosity.CalculateNDPorosity()[j]));
if (CDandND[j] < QCandND[j] && CDandND[j] < QDandND[j])
{
DistanceCD.Add((Math.Abs(TPorosity.BulkDensity[j] + (0.0173 * TPorosity.Neutron[j]) - 2.9162) / Math.Sqrt(Math.Pow(0.0173, 2) + 1))
+ (Math.Abs(TPorosity.BulkDensity[j] + (0.0173 * TPorosity.Neutron[j]) - 2.7031) / Math.Sqrt(Math.Pow(0.0173, 2) + 1)));
DolomitePercent.Add((Math.Abs(TPorosity.BulkDensity[j] + (0.0173 * TPorosity.Neutron[j]) - 2.7031) / Math.Sqrt(Math.Pow(0.0173, 2) + 1)) / DistanceCD[j]);
CalcitePercent.Add((Math.Abs(TPorosity.BulkDensity[j] + (0.0173 * TPorosity.Neutron[j]) - 2.9162) / Math.Sqrt(Math.Pow(0.0173, 2) + 1)) / DistanceCD[j]);
QuartzPercent.Add(0);
DistanceQC.Add(0);
DistanceQD.Add(0);
}
else if (QCandND[j] < CDandND[j] && QCandND[j] < QDandND[j])
{
DistanceQC.Add((Math.Abs(TPorosity.BulkDensity[j] + (0.0192 * TPorosity.Neutron[j]) - 2.5818) / Math.Sqrt(Math.Pow(0.0192, 2) + 1))
+ (Math.Abs(TPorosity.BulkDensity[j] + (0.0173 * TPorosity.Neutron[j]) - 2.7031) / Math.Sqrt(Math.Pow(0.0173, 2) + 1)));
QuartzPercent.Add((Math.Abs(TPorosity.BulkDensity[j] + (0.0173 * TPorosity.Neutron[j]) - 2.7031) / Math.Sqrt(Math.Pow(0.0173, 2) + 1)) / DistanceQC[j]);
CalcitePercent.Add((Math.Abs(TPorosity.BulkDensity[j] + (0.0192 * TPorosity.Neutron[j]) - 2.5818) / Math.Sqrt(Math.Pow(0.0192, 2) + 1)) / DistanceQC[j]);
DolomitePercent.Add(0);
DistanceCD.Add(0);
DistanceQD.Add(0);
}
else if (QDandND[j] < CDandND[j] && QDandND[j] < QCandND[j])
{
DistanceQD.Add((Math.Abs(TPorosity.BulkDensity[j] + (0.0192 * TPorosity.Neutron[j]) - 2.5818) / Math.Sqrt(Math.Pow(0.0192, 2) + 1))
+ (Math.Abs(TPorosity.BulkDensity[j] + (0.0173 * TPorosity.Neutron[j]) - 2.9162) / Math.Sqrt(Math.Pow(0.0173, 2) + 1)));
QuartzPercent.Add((Math.Abs(TPorosity.BulkDensity[j] + (0.0173 * TPorosity.Neutron[j]) - 2.9162) / Math.Sqrt(Math.Pow(0.0173, 2) + 1)) / DistanceQD[j]);
DolomitePercent.Add((Math.Abs(TPorosity.BulkDensity[j] + (0.0192 * TPorosity.Neutron[j]) - 2.5818) / Math.Sqrt(Math.Pow(0.0192, 2) + 1)) / DistanceQD[j]);
CalcitePercent.Add(0);
DistanceCD.Add(0);
DistanceQC.Add(0);
}
else
{
DistanceCD.Add(0);
DistanceQC.Add(0);
DistanceQD.Add(0);
DolomitePercent.Add(0);
CalcitePercent.Add(0);
QuartzPercent.Add(0);
}
if (DolomitePercent[j]==0 && CalcitePercent[j]==0 && QuartzPercent[j]==0 && TPorosity.BulkDensity[j] < (-0.0173 * TPorosity.Neutron[j]) + 2.9162)
{
DolomitePercent[j] = 100;
}
if (DolomitePercent[j] == 0 && CalcitePercent[j] == 0 && QuartzPercent[j] == 0 && TPorosity.BulkDensity[j]> 2.5943 * Math.Pow(Math.E, (-0.009 * TPorosity.Neutron[j])))
{
QuartzPercent[j] = 100;
}
}
return Tuple.Create(DolomitePercent, CalcitePercent, QuartzPercent);
}
}
Hope anyone help me.
thank you
I have nearly finished a project I've been working on for over three months. This is my first program I've ever made, so it may not be the cleanest code but for the most part works. The only issue I'm having now is getting the times to line up between specific times (In this instance 8am and 3pm). I am getting times outside of these times instead of inside. My current code is
if (DateTime.Now.AddMinutes(timetoadd).Hour < 8)
{
MessageBox.Show("Too early_Button Pressed");
var now = DateTime.Now;
var today8am = now.AddDays(0).Date.AddHours(8);
double totalHours = (today8am - now).TotalHours;
MessageBox.Show("totalHours=" + totalHours);
double hourstoadd = 0;// = timetoadd / 60;
MessageBox.Show("TTA = " + timetoadd.ToString());
do
{
hourstoadd++;
if (DateTime.Now.AddHours(hourstoadd).Hour == 15)
{
hourstoadd = hourstoadd + 17;
}
timetoadd = timetoadd - 60;
System.Diagnostics.Debug.WriteLine(hourstoadd.ToString());
}
while (timetoadd >= 60);
MessageBox.Show("TTA2 = " + timetoadd.ToString());
do
{
hourstoadd = hourstoadd - 8;
daystoadd++;
if (DateTime.Now.DayOfWeek.ToString() == "Saturday")
{
daystoadd = daystoadd + 2;
}
else if (DateTime.Now.DayOfWeek.ToString() == "Sunday")
{
daystoadd = daystoadd + 1;
}
}
while (hourstoadd > 8);
if (DateTime.Now.AddDays(daystoadd).AddHours(hourstoadd).Date.AddHours(hourstoadd + now.Hour).AddMinutes(timetoadd + now.Minute).DayOfWeek.ToString() == "Saturday")
{
daystoadd = daystoadd + 2;
}
else if (DateTime.Now.AddDays(daystoadd).AddHours(hourstoadd).Date.AddHours(hourstoadd + now.Hour).AddMinutes(timetoadd + now.Minute).DayOfWeek.ToString() == "Sunday")
{
daystoadd = daystoadd + 1;
}
EstimatedCompleteDate = DateTime.Now.AddDays(daystoadd).AddHours(hourstoadd).Date.AddHours(hourstoadd + now.Hour).AddMinutes(timetoadd + now.Minute).ToString();
MessageBox.Show("ETA = " + EstimatedCompleteDate);
}
else if (DateTime.Now.AddMinutes(timetoadd).Hour > 15)
{
MessageBox.Show("Too Late_Button Pressed");
var now = DateTime.Now;
var tomorrow8am = now.AddDays(1).Date.AddHours(8);
double totalHours = (tomorrow8am - now).TotalHours;
MessageBox.Show("totalHours=" + totalHours);
double hourstoadd = 0;// = timetoadd / 60;
MessageBox.Show("TTA = " + timetoadd.ToString());
do
{
hourstoadd++;
if (DateTime.Now.AddHours(hourstoadd).Hour == 15)
{
hourstoadd = hourstoadd + 17;
}
timetoadd = timetoadd - 60;
System.Diagnostics.Debug.WriteLine(hourstoadd.ToString());
}
while (timetoadd >= 60);
MessageBox.Show("TTA2 = " + timetoadd.ToString());
do
{
hourstoadd = hourstoadd - 8;
daystoadd++;
if (DateTime.Now.DayOfWeek.ToString() == "Saturday")
{
daystoadd = daystoadd + 2;
}
else if (DateTime.Now.DayOfWeek.ToString() == "Sunday")
{
daystoadd = daystoadd + 1;
}
}
while (hourstoadd > 8);
if (DateTime.Now.AddDays(daystoadd).AddHours(hourstoadd).Date.AddHours(hourstoadd + now.Hour).AddMinutes(timetoadd + now.Minute).DayOfWeek.ToString() == "Saturday")
{
daystoadd = daystoadd + 2;
}
else if (DateTime.Now.AddDays(daystoadd).AddHours(hourstoadd).Date.AddHours(hourstoadd + now.Hour).AddMinutes(timetoadd + now.Minute).DayOfWeek.ToString() == "Sunday")
{
daystoadd = daystoadd + 1;
}
EstimatedCompleteDate = DateTime.Now.AddDays(daystoadd).AddHours(hourstoadd).Date.AddHours(hourstoadd + now.Hour).AddMinutes(timetoadd + now.Minute).ToString();
MessageBox.Show("ETA = " + EstimatedCompleteDate);
}
else if (DateTime.Now.AddMinutes(timetoadd).Hour <= 15 && DateTime.Now.AddMinutes(timetoadd).Hour >= 8)
{
MessageBox.Show("Regular hours");
var now = DateTime.Now;
var tomorrow8am = now.AddDays(1).Date.AddHours(8);
double totalHours = 0;
MessageBox.Show("totalHours=" + totalHours);
double hourstoadd = 0 + now.Minute;// = timetoadd / 60;
MessageBox.Show("TTA = " + timetoadd.ToString());
do
{
hourstoadd++;
if (DateTime.Now.AddHours(hourstoadd).Hour == 15)
{
hourstoadd = hourstoadd + 17;
}
timetoadd = timetoadd - 60;
System.Diagnostics.Debug.WriteLine(hourstoadd.ToString());
}
while (timetoadd >= 60);
MessageBox.Show("TTA2 = " + timetoadd.ToString());
do
{
hourstoadd = hourstoadd - 8;
daystoadd++;
if (DateTime.Now.DayOfWeek.ToString() == "Saturday")
{
daystoadd = daystoadd + 2;
}
else if (DateTime.Now.DayOfWeek.ToString() == "Sunday")
{
daystoadd = daystoadd + 1;
}
}
while (hourstoadd > 8);
if (DateTime.Now.AddDays(daystoadd).AddHours(hourstoadd).Date.AddHours(hourstoadd + now.Hour).AddMinutes(timetoadd + now.Minute).DayOfWeek.ToString() == "Saturday")
{
daystoadd = daystoadd + 2;
}
else if (DateTime.Now.AddDays(daystoadd).AddHours(hourstoadd).Date.AddHours(hourstoadd + now.Hour).AddMinutes(timetoadd + now.Minute).DayOfWeek.ToString() == "Sunday")
{
daystoadd = daystoadd + 1;
}
EstimatedCompleteDate = DateTime.Now.AddDays(daystoadd).AddHours(hourstoadd).Date.AddHours(hourstoadd + now.Hour).AddMinutes(timetoadd + now.Minute).ToString();
MessageBox.Show("ETA = " + EstimatedCompleteDate);
}
timetoadd = sum + current_ticket_time in minutes.
sum = all previous ticket times.
I am using Messagebox's to provide me a form of immediate feedback.
The other night I was able to get a script that works for exactly what's needed.
var soonh = 0;
var now = DateTime.Now;
var soonm = 0;
double minutestoadd = timetoadd;
int hourstoadd = 0;
if (minutestoadd >= 60)
{
do
{
minutestoadd = minutestoadd - 60;
hourstoadd++;
}
while (minutestoadd > 60);
}
soonh = 6 - (Convert.ToInt32(now.Hour));
soonm = 60 - ((Convert.ToInt32(now.Minute)));
if (soonm < 0)
{
var temp = soonm;
soonm = 0;
soonh = soonh - 1;
soonm = 60 + temp;
Console.WriteLine("Sample");
}
double totalmin = soonm + now.Minute + minutestoadd;
if (totalmin >= 60)
{
do
{
if (totalmin >= 60)
{
totalmin = totalmin - 60;
hourstoadd = hourstoadd + 1;
}
}
while (totalmin >= 60);
if (soonh < 0)
{
soonh = 0;
}
int totalhour = soonh + now.Hour + hourstoadd;
int totalday = 0;
bool sample = false;
do
{
if (totalhour >= 8)
{
totalhour = totalhour - 7;
totalday = totalday + 1;
sample = true;
}
}
while (totalhour >= 8);
if (sample == true)
{
totalday = totalday - 1;
}
totalhour = totalhour + 7;
if (totalhour + DateTime.Today.Hour >= 15)
{
totalhour = totalhour - 7;
}
if (DateTime.Today.Date.AddDays(totalday) == DateTime.Today)
{
totalday++;
}
EstimatedCompleteDate = (DateTime.Today.Date.AddDays(totalday).AddHours(totalhour).AddMinutes(totalmin).ToString());
}
I am trying to find the value corresponding to maximum value in another column in my array, but get an error with the present code saying Unable to cast object of type WhereSelectArrayIterator2[Main.AllClasses.MaxMin,System.Int32] to type 'System.IConvertible'.`
I am assigning a list of type MaxMin to an array and then trying to calculate the maxima and its corresponding element in the second column.
Here's the code:
MaxMin finalMax = new MaxMin();
if (maxima.Count != 0)
{
MaxMin[] maxArray = maxima.ToArray();
finalMax.val = maxArray.Select(x => x.val).Max();
finalMax.cnt = Convert.ToInt32(maxArray.Where(x => x.val.Equals(finalMax.val)).Select(x => x.cnt));
}
Here's the full code:
public MaxMin Maxima(float[] values, MaxMin drop)
{
float[] T = new float[5] { 0, 0, 0, 0, 0 };
float[] dT = new float[4] { 0, 0, 0, 0 };
int count = 0;
List<MaxMin> maxima = new List<MaxMin>();
count = values.Length;
try
{
for (int i = 99; i < count - 100; i++)
{
MaxMin max = new MaxMin();
T[0] = values[i - 1];
T[1] = values[i];
T[2] = values[i + 1];
T[3] = values[i + 2];
T[4] = values[i + 3];
dT[0] = T[1] - T[0];
dT[1] = T[2] - T[1];
dT[2] = T[3] - T[2];
dT[3] = T[4] - T[3];
if (i > drop.cnt) //cutoff time above which calculation of minima will be initiated
{
if (dT[1] > 0 && dT[3] < 0)
{
float[] diff = new float[8] { 0, 0, 0, 0, 0, 0, 0, 0 };
diff[0] = (values[i + 1] - values[i - 19]);
diff[1] = (values[i + 1] - values[i + 19]);
diff[2] = (values[i + 1] - values[i - 49]);
diff[3] = (values[i + 1] - values[i + 49]);
diff[4] = (values[i + 1] - values[i - 69]);
diff[5] = (values[i + 1] - values[i + 69]);
diff[6] = (values[i + 1] - values[i - 99]);
diff[7] = (values[i + 1] - values[i + 99]);
if (diff[0] < 20 && diff[1] < 20 && diff[2] < 20 && diff[3] < 20 && diff[2] > 0 && diff[3] > 0 && diff[4] > 0.5 && diff[5] > 0.5 && diff[6] > 2 && diff[7] > 2)
{
max.cnt = i + 1;
max.val = values[i + 1];
maxima.Add(max);
}
}
}
}
MaxMin finalMax = new MaxMin();
if (maxima.Count != 0)
{
MaxMin[] maxArray = maxima.ToArray();
finalMax.val = maxArray.Select(x => x.val).Max();
finalMax.cnt = Convert.ToInt32(maxArray.Where(x => x.val.Equals(finalMax.val)).Select(x => x.cnt));
}
else
{
finalMax.val = values.Max();
finalMax.cnt = Array.IndexOf(values, values.Max());
}
return finalMax;
}
catch (Exception ex)
{
throw ex;
}
}
You're trying to convert results of Select method call, which returns IEnumerable<T>, to int. That's not gonna work.
Use First instead of Where/Select:
finalMax.cnt = maxArray.First(x => x.val.Equals(finalMax.val)).cnt;