使用Two.js绘制抛物线

huangapple go评论58阅读模式
英文:

Drawing a Parabola Using Two.js

问题

I want to create a parabola using the library Two.js. Here is the code that I am using:

<!-- begin snippet: js hide: false console: false babel: false -->

<!-- language: lang-js -->
// 创建一个 Two.js 实例
var two = new Two({
  width: 300,
  height: 300
}).appendTo(document.getElementById("parabola"));

var path = two.makePath(0, two.height / 2);

path.stroke = "#000";
path.linewidth = 3;

for (var i = 0; i < two.width; i++) {
  var x = i;
  var y = Math.pow((x - two.width / 2), 2) / (2 * 50) + two.height / 2;
  console.log(`x: ${x} and y: ${y}`);
  path.vertices.push(new Two.Vector(x, y));
}

two.update();

<!-- language: lang-html -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/two.js/0.8.10/two.min.js"></script>
<div id="parabola"></div>

<!-- end snippet -->

I should draw a parabola that starts at the left end and branches out at the right end. However, the result I get is just two lines.

Am I doing something wrong mathematically or failing to close the path somehow?

英文:

I want to create a parabola using the library Two.js. Here is the code that I am using:

<!-- begin snippet: js hide: false console: false babel: false -->

<!-- language: lang-js -->

// Create a Two.js instance
var two = new Two({
  width: 300,
  height: 300
}).appendTo(document.getElementById(&quot;parabola&quot;));

var path = two.makePath(0, two.height / 2);

path.stroke = &quot;#000&quot;;
path.linewidth = 3;

for (var i = 0; i &lt; two.width; i++) {
  var x = i;
  var y = Math.pow((x - two.width / 2), 2) / (2 * 50) + two.height / 2;
  console.log(`x: ${x} and y: ${y}`);
  path.vertices.push(new Two.Vector(x, y));
}

two.update();

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/two.js/0.8.10/two.min.js&quot;&gt;&lt;/script&gt;
&lt;div id=&quot;parabola&quot;&gt;&lt;/div&gt;

<!-- end snippet -->

I should draw a parabola that starts at the left end and branches out at the right end. However, the result I get is just two lines.

Am I doing something wrong mathematically or failing to close the path somehow?

答案1

得分: 1

你没有关闭路径。

此外,你的 y 计算绘制的图像太低,你的起始 y 在中间。

提示: 尝试创建一个函数,根据给定的 x 返回 y 坐标。

英文:

You did not close your path.

Also, your y calculation is drawing the image too low, and your starting y is in the middle.

Tip: Try creating a function that returns the y coordinate for a given x.

<!-- begin snippet: js hide: false console: false babel: false -->

<!-- language: lang-js -->

// Create a Two.js instance
const two = new Two({ width: 300, height: 300 })
  .appendTo(document.getElementById(&quot;parabola&quot;));

const
  xOffset = two.width / 2,
  yOffset = two.height / 2;

const path = two.makePath(0, yOffset);

path.fill = &#39;red&#39;;
path.stroke = &quot;#000&quot;;
path.linewidth = 3;

const getY = (x) =&gt; (x - xOffset)**2 / yOffset - yOffset;

for (let x = 0; x &lt; two.width; x++) {
  let y = getY(x);
  console.log(`x: ${x} and y: ${y}`);
  path.vertices.push(new Two.Vector(x, y));
}

// Connect the last vertex to the first to close the path...
path.vertices.push(new Two.Vector(0, getY(0)));

two.update();

<!-- language: lang-css -->

#parabola {
  background: lightgrey;
}

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/two.js/0.8.10/two.min.js&quot;&gt;&lt;/script&gt;
&lt;div id=&quot;parabola&quot;&gt;&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年2月18日 02:24:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/75488018.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定