首页 > 解决方案 > 获取角度两行svg反应js

问题描述

您好,可以在反应 js 中使用 svg 而不使用 svg set 属性来获取角度两条线吗?

已经尝试了几个堆栈教程,但没有一个真正返回两条线之间的角度,是的,只有这条线的角度,我试过了。

findAngle(p0,p1,p2) {
  var a = Math.pow(10,2) + Math.pow(100,2),
      b = Math.pow(10,2) + Math.pow(10,2),
      c = Math.pow(10,2) + Math.pow(70,2);
  var aa = Math.acos( (a+b-c) / Math.sqrt(4*a*b) );

  console.log(aa);

}

obs:这些值在我的两行中。

标签: javascriptlines

解决方案


基于这个答案,你想要这样的东西:

// helper function: make a point
function point(x,y){
   return {'x':x,'y':y};
}

// helper function: get distance between points a and b (by Pythagoras)
function d(a,b) {
   return Math.sqrt(d2(a,b));
}

// helper function: get square of distance between points a and b
function d2(a,b) {
   return (a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y);
}

// helper function: convert radians to degrees
function rad2deg(angleInRadians) {
   return angleInRadians/(Math.PI/180);
}

// get the angle in degrees between ab and ac, using the cosine rule:
function angle_deg(a,b,c) {
   return rad2deg(Math.acos((d2(a,b) + d2(a,c) - d2(c,b)) / (2 * d(a,b) * d(a,c))));
}

p0 = point(0,0);
p1 = point(1,0);
p2 = point(0,1);

console.log(angle_deg(p0,p1,p2)); // 90


推荐阅读