using System;
using System.Collections.Generic;
using System.Linq;
namespace TProduct.OpenDto
{
public class ValueCoeffParas
{
public ValueCoeffParas() { }
///
/// 方法
///
public int Method { get; set; }
///
/// 相乘
///
public double Multiply1 { get; set; } = 1;
///
/// 相加
///
public double Add1 { get; set; } = 0;
///
/// 插值方法 0 不插值 1 插值
///
public int InterpolationType { get; set; } = 0;
///
///
///
public List RealValues { get; set; }
///
///
///
public List Multiplys { get; set; }
///
///
///
public List Adds { get; set; }
public double CalcRealValue(double v)
{
if (Method == 0)
return v;
else if (Method == 1)
return v * Multiply1 + Add1;
else if (Method == 2)
return CalcRealValue2(v);
else
return v;
}
private double CalcRealValue2(double v)
{
if (this.RealValues == null || this.RealValues.Count() < 2)
return v;
if (this.Multiplys == null || this.Multiplys.Count() < 2)
return v;
if (this.Adds == null || this.Adds.Count() < 2)
return v;
if (this.InterpolationType == 0)
{//不插值
for (int i = 0; i < this.RealValues.Count; i++)
{
if (v <= this.RealValues[i])
return v * this.Multiplys[i] + this.Adds[i];
}
return v * this.Multiplys.Last() + this.Adds.Last();
}
else
{//插值
if (v <= this.RealValues[0])
return v * this.Multiplys[0] + this.Adds[0];
for (int i = 1; i < this.RealValues.Count; i++)
{
if (v <= this.RealValues[i])
{//
if (this.RealValues[i] == this.RealValues[i - 1])
return this.RealValues[i] * this.RealValues[i] + this.Adds[i];
var lll = this.RealValues[i - 1] * this.RealValues[i - 1] + this.Adds[i - 1];
var ccc = this.RealValues[i] * this.RealValues[i] + this.Adds[i];
return lll + (v - this.RealValues[i - 1]) * (ccc - lll)
/ (this.RealValues[i] - this.RealValues[i - 1]);
}
}
return v * this.Multiplys.Last() + this.Adds.Last();
}
}
}
}