在Javascript中绘图时导致圆角更圆的数字

Chewie The Chorkie

我有一个for循环,该循环返回0到1之间的一个十进制数。我想制作一条比现在更像圆角的曲线。我还希望它仅在0.25之后开始上升。我现在还不知道该如何用我现在的数学去做。我正在使用Math.log和线性转换函数,但也许我需要与抛物线更相关的东西。

for (i = 1; i < 101; ++i) {
  var dec = i / 100
  if (dec >= 0.25) {
    console.log("dec = " + dec);
    var large = i * 100
    var log = Math.log(large);
    console.log("log = " + log);
    var linCon = applyLinearConversion(log, 2.8, 9.2104, -2.7, 1)
    console.log("linCon " + i + " = " + linCon);
    document.getElementById("graph").innerHTML += "<div style='background-color:#000000; width:" + (linCon * 1000) + "px; height:5px;'></div>";
  }
}

function applyLinearConversion(OldValue, OldMin, OldMax, NewMin, NewMax) {
  OldRange = (OldMax - OldMin)
  if (OldRange == 0)
    NewValue = NewMin
  else {
    NewRange = (NewMax - NewMin)
    NewValue = (((OldValue - OldMin) * NewRange) / OldRange) + NewMin
  }

  return NewValue

}
<div id="graph"></div>

我用更多样式的div填充div。

我的是这样的: 在此处输入图片说明

我想要我的像这样: 在此处输入图片说明

易卜拉欣·马尔里尔

您可以使用半圆图的公式为:

结果如下图:

由于您使用的是垂直堆叠的水平div来绘制图形,因此x和y坐标将被反转,圆的左四分之一将从上图中使用。

var width = 200;                             // the width of the graph
var height = 200;                            // the height of the graph
var xOffset = 0.25 * width;                  // the x offset at which the graph will start ramping up (this offset is added to the graph width)

var html = "";                               // to accumulate the generated html before assigning it to innerHTML (it's not a good idea to constantly update innerHTML)

for (i = 1; i < 101; ++i) {
  var x = 1 - i / 100;                       // the x coordinate, we are using the left side of the graph so x should be negative going from -1 to 0
  var y = Math.sqrt(1 - x * x);              // the y coordinate as per the formula (this will be used for the width)
  html += "<div style='background-color:#000000; width:" + (xOffset + y * width) + "px; height:" + (height / 100) + "px;'></div>";
}

document.getElementById("graph").innerHTML = html;
<div id="graph"></div>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章