gpt4 book ai didi

javascript - 查找坐标以在直线的末端绘制箭头(等腰三 Angular 形)

转载 作者:行者123 更新时间:2023-11-29 16:57:04 25 4
gpt4 key购买 nike

我正在尝试创建一个函数,该函数将返回我想在直线末端绘制的箭头(等腰三 Angular 形)的 3 点坐标。

挑战在于线的方向( Angular )在象限中可以在 0 到 360 度之间变化。

我有以下值:

//start coordinates of the line
var x0 = 100;
var y0 = 100;

//end coordinates of the line
var x1 = 200;
var y1 = 200;

//height of the triangle
var h = 10;
//width of the base of the triangle
var w = 30 ;

到目前为止,这是我的函数,它返回三 Angular 形底部的两个点坐标:

var drawHead = function(x0, y0, x1, y1, h, w){
var L = Math.sqrt(Math.pow((x0 - x1),2)+Math.pow((y0 - y1),2));

//first base point coordinates
var base_x0 = x1 + (w/2) * (y1 - y0) / L;
var base_y0 = y1 + (w/2) * (x0 - x1) / L;

//second base point coordinates
var base_x1 = x1 - (w/2) * (y1 - y0) / L;
var base_y1 = y1 - (w/2) * (x0 - x1) / L;

//now I have to find the last point coordinates ie the top of the arrow head
}

如何根据直线的 Angular 确定三 Angular 形顶部的坐标?

最佳答案

箭头的头部将与箭头的主体位于同一条线上。因此,(x1, y1) 和 (head_x, head_y) 之间的线段的斜率将与 (x0, y0) 和 (x1, y1) 之间的线段的斜率相同。假设 dx = head_x - x1 和 dy = head_y - y1 和斜率 = (y1 - y0)/(x1 - x0)。因此,dy/dx = 斜率。我们还知道 dx^2 + dy^2 = h^2。我们可以根据斜率和 h 求解 dx。那么,dy = dx * 斜率。一旦你有了 dx 和 dy,你就可以将它们添加到 x1 和 y1 以获得起点。一些伪代码:

if x1 == x0: #avoid division by 0
dx = 0
dy = h
if y1 < y0:
dy = -h #make sure arrow head points the right way
else:
dy = h
else:
if x1 < x0: #make sure arrow head points the right way
h = -h
slope = (y1 - y0) / (x1 - x0)
dx = h / sqrt(1 + slope^2)
dy = dx * slope
head_x = x1 + dx
head_y = y1 + dy

关于javascript - 查找坐标以在直线的末端绘制箭头(等腰三 Angular 形),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31462791/

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