javascript - 获取角度两行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:这些值在我的两行中。
解决方案
基于这个答案,你想要这样的东西:
// 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
推荐阅读
- c++ - 在 docker 容器中安装了 ffmpeg,如何编译 c++ 程序?
- javascript - Angular Form 验证器仅在输入文本框时有效
- node.js - 为 PPC 架构编译 NodeJS
- python - 如何为 vscode 的数据科学模式设置环境变量?
- python - 检查对象是否是库的实例
- python-2.7 - 'QWidget' 对象没有属性 'set_status_message'
- android - 如何将数据从广播接收器发送到片段?
- android - 如何修复 ListView 上的 OnItemClickListener
- python - 如何从功率计上的原始数据计算能量读数
- apache-kafka - Kafka 代理自动扩展