c# - 计算具有步长的图表的轴刻度

标签 c# .net algorithm math

我已经为图表上的轴计算了步长。

我还有最小值和最大值。现在我需要计算所有刻度,以便显示我的最小值和最大值之间的所有值。

例如:

步长:1000

最小值:213

最大值:4405

预期报价:0,1000,2000,3000,4000,5000


步长:500

最小值:-1213

最大值:1405

预期报价:-1500、-1000、-500、0、500、1000、1500

到目前为止,我一直在尝试使用“试错法”来计算第一个值,例如:

bool firstStepSet = false;
double firstStep = stepSize;
do
{
    if (xValue >= (firstStep - (stepSize / 2)) && xValue <= 
    (firstStep + (stepSize / 2)))
    {
        firstStepSet = true;
        this.myBarXValues.Add(firstStep, 0);
    }
    else if (xValue > stepSize)
    {
        firstStep += stepSize;
    }
    else
    {
        firstStep -= stepSize;
    }
}
while (!firstStepSet);

然后,我将向此列表添加步骤,直到所有值都适合为止。

这对我来说似乎很脏,我想知道是否还有其他解决方案。

所以我需要的是一个计算我需要的第一个报价的解决方案。

最佳答案

此函数计算第一步和最后一步的值:

static void CalcSteps(int min, int max, int stepSize, out int firstStep, out int lastStep)
{
    if (min >= 0)
    {
        firstStep = (min / stepSize) * stepSize;
    }
    else
    {
        firstStep = ((min - stepSize + 1) / stepSize) * stepSize;
    }

    if (max >= 0)
    {
        lastStep = ((max + stepSize - 1) / stepSize) * stepSize;
    }
    else
    {
        lastStep = (max / stepSize) * stepSize;
    }
}

关于c# - 计算具有步长的图表的轴刻度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55414454/

相关文章:

c# - ASP.NET : How do I fetch data from a webserver every few minutes

c - 检查matrix[1000][1000]中重复元素的解决方法

c# - 电子签名 c# asp.net

c# - 使停靠控件可见时更改 Z 顺序

c# - .NET DataTable 正在处理 Excel 文件中的日期字段

javascript - 在字符串中配对字符的优雅方法是什么?

algorithm - 在 CUDA 代码中表示粒子的最佳方式

c# - 从 ASP.NET 网站获取 IIS 站点名称

c# - 在不将所有元素复制到新数组的情况下裁剪字符串数组中的第一个元素

c# - 以编程方式为桌面 "shortcut"创建快捷键组合