gpt4 book ai didi

javascript - Canvas - 如何让线穿过一个点

转载 作者:行者123 更新时间:2023-11-30 08:00:33 25 4
gpt4 key购买 nike

This是我现在拥有的:

How it is

我想让它像this :

How is should be

如您所见,蓝线穿过中心 (400,400)。蓝线的起点不固定,会根据用户输入的数据移动。

如何添加这条蓝线并使其穿过中心?

抱歉我的英语不好,正在努力:)

半场

最佳答案

Use mathematics .

设中心坐标为 (Cx, Cy),在您的情况下为 (400, 400)。设用户坐标为(Ux, Uy)

(Ux, Uy) 穿过中心的线的方程式将由以下方程式给出:

y - Cy = slope * (x - Cx),其中 slope = (Cy - Uy)/(Cx - Ux)

现在,要从 Ux 到某个 x 坐标绘制直线,例如 Px,只需使用等式计算 Py = Slope * (Px - Cx) + Cy,然后绘制从 (Ux, Uy)(Px, Py) 的直线。

context.beginPath();
context.moveTo(Ux, Uy);
context.lineTo(Px, Py);
context.stroke();

关于javascript - Canvas - 如何让线穿过一个点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29282501/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com