js, 向量夹角的cos值,三个点求中间点夹角的cos值

记录一下开发过程用到的向量方法

向量夹角的cos值
/**
 * 向量夹角的cos值
 * @param { [number, number] } a
 * @param { [number, number] } b
 * @returns
 */
export function vectorAngleCos(a, b) {
  return (
    (a[0] * b[0] + a[1] * b[1]) /
    (Math.sqrt(a[0] ** 2 + a[1] ** 2) * Math.sqrt(b[0] ** 2 + b[1] ** 2))
  );
}
向量夹角的一半的cos值
/**
 * 向量夹角的一半的cos值
 * @param { [number, number] } a
 * @param { [number, number] } b
 * @returns
 */
export function vectorAngleCosHalf(a, b) {
  return Math.sqrt((1 + vectorAngleCos(a, b)) / 2);
}
将点绕原点旋转
/**
 * 将点绕原点旋转
 * @param { [number, number] } param0
 * @param { number } angle
 * @returns { [number, number] }
 */
export function rotateVector([x1, y1], angle) {
  let x2 = x1 * Math.cos(angle) - y1 * Math.sin(angle);
  let y2 = y1 * Math.cos(angle) + x1 * Math.sin(angle);

  return [x2, y2];
}
三个点求中间点夹角的cos值,
/**
 * 三个点求中间点夹角的cos值,
 * @param { { x: number; y: number } | number[] } p1
 * @param { { x: number; y: number } | number[] } p2
 * @param { { x: number; y: number } | number[] } p3
 * @param { string | number } x
 * @param { string | number } y
 * @returns
 */
export function vectorAngleCos(p1, p2, p3, x = "x", y = "y") {
  let a = [p2[x] - p1[x], p2[y] - p1[y]];
  let b = [p3[x] - p2[x], p3[y] - p2[y]];

  return (
    (a[0] * b[0] + a[1] * b[1]) /
    (Math.sqrt(a[0] ** 2 + a[1] ** 2) * Math.sqrt(b[0] ** 2 + b[1] ** 2))
  );
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
### 回答1: 您好,以下是求两个向量夹角余弦的 C 语言程序: #include <stdio.h> #include <math.h> int main() { float x1, y1, x2, y2, cos_value, dot_product, length1, length2; printf("请输入第一个向量的坐标(x1, y1):"); scanf("%f %f", &x1, &y1); printf("请输入第二个向量的坐标(x2, y2):"); scanf("%f %f", &x2, &y2); dot_product = x1 * x2 + y1 * y2; length1 = sqrt(x1 * x1 + y1 * y1); length2 = sqrt(x2 * x2 + y2 * y2); cos_value = dot_product / (length1 * length2); printf("两个向量夹角余弦为:%f\n", cos_value); return ; } 希望能帮到您! ### 回答2: 要实现求两个向量夹角的余弦,可以使用C语言编写一个函数来实现。 首先,我们可以定义一个结构体来表示向量,在该结构体中,定义两个浮型的成员变量表示向量的坐标。 ```c typedef struct { float x; float y; } Vector; ``` 接下来,我们可以编写一个函数来计算两个向量夹角的余弦。该函数接受两个向量作为参数,并返回一个浮型的结果。 ```c #include <stdio.h> #include <math.h> float cosine(Vector v1, Vector v2) { float dotProduct = v1.x * v2.x + v1.y * v2.y; float magnitude1 = sqrt(v1.x * v1.x + v1.y * v1.y); float magnitude2 = sqrt(v2.x * v2.x + v2.y * v2.y); float cosineValue = dotProduct / (magnitude1 * magnitude2); return cosineValue; } ``` 在主函数中,我们可以定义两个向量,并通过调用上述函数计算它们的夹角的余弦。最后,我们可以输出结果。 ```c int main() { Vector v1 = {3, 4}; Vector v2 = {1, 2}; float cosineValue = cosine(v1, v2); printf("两个向量夹角的余弦为:%f\n", cosineValue); return 0; } ``` 运行这个程序,就可以得到两个向量夹角的余弦。 注意,上述代码只适用于二维向量。如果想要适用于更高维度的向量,需要进行相应的修改。 ### 回答3: 要实现求两个向量夹角的余弦,可以使用C语言编写以下程序: ```c #include <stdio.h> #include <math.h> double dotProduct(double *vectorA, double *vectorB, int size) { double product = 0.0; for (int i = 0; i < size; i++) { product += vectorA[i] * vectorB[i]; } return product; } double magnitude(double *vector, int size) { double sum = 0.0; for (int i = 0; i < size; i++) { sum += vector[i] * vector[i]; } return sqrt(sum); } double cosineAngle(double *vectorA, double *vectorB, int size) { double dot = dotProduct(vectorA, vectorB, size); double magnitudeA = magnitude(vectorA, size); double magnitudeB = magnitude(vectorB, size); return dot / (magnitudeA * magnitudeB); } int main() { int size; printf("请输入向量的维度:"); scanf("%d", &size); double vectorA[size], vectorB[size]; printf("请输入向量A的%d个分量:", size); for (int i = 0; i < size; i++) { scanf("%lf", &vectorA[i]); } printf("请输入向量B的%d个分量:", size); for (int i = 0; i < size; i++) { scanf("%lf", &vectorB[i]); } double cosine = cosineAngle(vectorA, vectorB, size); printf("两个向量夹角的余弦为:%lf\n", cosine); return 0; } ``` 在该程序中,首先定义了三个辅助函数:`dotProduct`用于计算两个向量积,`magnitude`用于计算向量的模长,`cosineAngle`用于计算两个向量夹角的余弦。 然后,在`main`函数中,首先要求用户输入向量的维度,并根据输入的维度定义两个向量的数组。然后,分别要求用户逐个输入向量A和向量B的分量。最后,调用`cosineAngle`函数计算两个向量夹角的余弦,并输出结果。 这段程序能够通过输入两个向量的分量来计算它们的夹角的余弦

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

v西瓜

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值