确定3个坐标之间的角度

Bla*_*ack 2 javascript math

正如您在我的屏幕截图中看到的,我尝试计算坐标AB和BC之间的角度,在这种情况下,角度显然是90°.但是如何用javascript代码确定这个角度呢?

我试着确定AB和BC之间的角度

我找到了这个线程并尝试了接受的解决方案,但我总是得到1.5790而不是我预期的.我重写了原始函数,因为我不明白如何将参数传递给它.

请原谅我糟糕的油漆和数学技巧.

	function find_angle(Ax,Ay,Bx,By,Cx,Cy)
	{
		var AB = Math.sqrt(Math.pow(Bx-Ax,2)	+	Math.pow(By-Ay,2));    
		var BC = Math.sqrt(Math.pow(Bx-Cx,2)	+ 	Math.pow(By-Cy,2)); 
		var AC = Math.sqrt(Math.pow(Cx-Ax,2)	+ 	Math.pow(Cy-Ay,2));
		
		return Math.acos((BC*BC+AB*AB-AC*AC)	/	(2*BC*AB));
	}
	var angle = find_angle
				(
					4 ,		//Ax
					3 ,		//Ay
					
					4 ,		//Bx
					2 ,		//By
					
					0 ,		//Cx
					2		//Cy
				)
				
	alert ( angle );
Run Code Online (Sandbox Code Playgroud)

new*_*zad 6

答案在该线程的弧度中给出.

1.57弧度是90度(pi/2).您可以将答案转换为度数,将其乘以180/pi.

A = { x: 4, y: 3 };
B = { x: 4, y: 2 };
C = { x: 0, y: 2 };

alert(find_angle(A,B,C));

function find_angle(A,B,C) {
    var AB = Math.sqrt(Math.pow(B.x-A.x,2)+ Math.pow(B.y-A.y,2));    
    var BC = Math.sqrt(Math.pow(B.x-C.x,2)+ Math.pow(B.y-C.y,2)); 
    var AC = Math.sqrt(Math.pow(C.x-A.x,2)+ Math.pow(C.y-A.y,2));
    
    return Math.acos((BC*BC+AB*AB-AC*AC) / (2*BC*AB)) * (180 / Math.PI);   
}
Run Code Online (Sandbox Code Playgroud)