C++ 几何算法 - 向量点乘,叉乘及其应用

ops/2024/10/19 3:34:50/

一:点乘介绍

        1. 向量点乘:

        

        2. 向量点乘的性质:

        

       3. 向量点乘公式:

        

        

        

      4. 向量的点乘的属性:

        (1):向量与自身做点乘,会得到向量长度的平方:

        (2):向量长度,为向量与自身点乘后再求平方根:

        (3):向量投影,将a向量投影到向量b上:

        (4):向量夹角:

二:叉乘介绍:

        1. 向量叉乘:

           

        2. 向量叉乘公式:

         

        3. 向量叉乘的属性:

                判断三个向量是否共面:

三:应用1 - 求两直线的交点:

        (1)2D直线方程:

        (2)将直线1带入直线2中:  ,叉乘等于0,意味着两向量共线。

        (3)求交点:

                

四:应用2 - 求三个平面的交点:

        (1):三个平面方程:

                

        (2):三个平面方程,三个未知数,利用克拉默法则求解即可。

三:实现

#ifndef _POINT_H_
#define _POINT_H_#include <iostream>
#include <cmath>class Point2D
{
public:float x, y;Point2D() {}Point2D(float x, float y) : x(x), y(y) {}Point2D &operator+=(const Point2D &t){x += t.x;y += t.y;return *this;}Point2D &operator-=(const Point2D &t){x -= t.x;y -= t.y;return *this;}Point2D &operator*=(float t){x *= t;y *= t;return *this;}Point2D &operator/=(float t){x /= t;y /= t;return *this;}Point2D operator+(const Point2D &t) const{return Point2D(*this) += t;}Point2D operator-(const Point2D &t) const{return Point2D(*this) -= t;}Point2D operator*(float t) const{return Point2D(*this) *= t;}Point2D operator/(float t) const{return Point2D(*this) /= t;}float dot(const Point2D& b) const{return x * b.x + y * b.y;}friend std::ostream &operator<<(std::ostream &out, const Point2D &t){out << '(' << t.x << ',' << t.y << ')';return out;}};Point2D operator*(float a, const Point2D &b)
{return b * a;
}float dot(const Point2D& a, const Point2D& b)
{return a.dot(b);
}float norm(const Point2D& a)
{return dot(a, a);
}double abs(const Point2D& a) {return sqrt(norm(a));
}double proj(const Point2D& a, const Point2D& b)
{return dot(a, b) / abs(b);
}double angle(const Point2D& a, const Point2D& b)
{return acos(dot(a, b) / abs(a) / abs(b));
}float cross(const Point2D& a, const Point2D& b)
{return a.x * b.y - a.y * b.x;
}Point2D intersect(const Point2D& a1, const Point2D& d1, const Point2D& a2, const Point2D& d2)
{return a1 + cross(a2 - a1, d2) / cross(d1, d2) * d1;
}class Point3D: public Point2D
{
public:float z;Point3D() {}Point3D(float x, float y, float z) : Point2D(x, y), z(z) {}Point3D &operator+=(const Point3D &t){x += t.x;y += t.y;z += t.z;return *this;}Point3D &operator-=(const Point3D &t){x -= t.x;y -= t.y;z -= t.z;return *this;}Point3D &operator*=(float t){x *= t;y *= t;z *= t;return *this;}Point3D &operator/=(float t){x /= t;y /= t;z /= t;return *this;}Point3D operator+(const Point3D &t) const{return Point3D(*this) += t;}Point3D operator-(const Point3D &t) const{return Point3D(*this) -= t;}Point3D operator*(float t) const{return Point3D(*this) *= t;}Point3D operator/(float t) const{return Point3D(*this) /= t;}float dot(const Point3D &t) const{return x * t.x + y * t.y + z * t.z;}friend std::ostream &operator<<(std::ostream &out, const Point3D &t){out << '(' << t.x << ',' << t.y << ',' << t.z << ')';return out;}
};Point3D operator*(float a, Point3D b)
{return b * a;
}float dot(const Point3D& a, const Point3D& b) 
{return a.dot(b);
}float norm(const Point3D& a) 
{return dot(a, a);
}double abs(const Point3D& a) {return sqrt(norm(a));
}double proj(const Point3D& a, const Point3D& b)
{return dot(a, b) / abs(b);
}double angle(const Point3D& a, const Point3D& b)
{return acos(dot(a, b) / abs(a) / abs(b));
}Point3D cross(const Point3D& a, const Point3D& b)
{return Point3D(a.y * b.z - a.z * b.y,a.z * b.x - a.x * b.z,a.x * b.y - a.y * b.x);
}float triple(const Point3D& a, const Point3D& b, const Point3D& c) 
{return dot(a, cross(b, c));
}Point3D intersect(const Point3D& a1, const Point3D& n1, const Point3D& a2, const Point3D& n2, const Point3D& a3, const Point3D& n3) 
{Point3D x(n1.x, n2.x, n3.x);Point3D y(n1.y, n2.y, n3.y);Point3D z(n1.z, n2.z, n3.z);Point3D d(dot(a1, n1), dot(a2, n2), dot(a3, n3));return Point3D(triple(d, y, z),triple(x, d, z),triple(x, y, d)) / triple(n1, n2, n3);
}#endif


http://www.ppmy.cn/ops/90118.html

相关文章

excel中有些以文本格式存储的数值如何批量转换为数字

一、背景 1.1 文本格式存储的数值特点 在平时工作中有时候会从别地方导出来表格&#xff0c;表格中有些数值是以文本格式存储的&#xff08;特点&#xff1a;单元格的左上角有个绿色的小标&#xff09;。 1.2 文本格式存储的数值在排序时不符合预期 当我们需要进行排序的时候…

Nginx自动安装配置脚本

一、Nginx介绍 Nginx 是一个流行的开源 Web 服务器软件。它最初由 Igor Sysoev 开发&#xff0c;并于 2004 年首次发布。 Nginx的 目标是提供高性能、高可靠性和低内存消耗的 Web服务器&#xff0c;同时也可以用作反向代理服务器和负载均衡器。 Nginx 以其优越的性能和高度可…

《学会 SpringMVC 系列 · 参数解析器 ArgumentResolvers》

&#x1f4e2; 大家好&#xff0c;我是 【战神刘玉栋】&#xff0c;有10多年的研发经验&#xff0c;致力于前后端技术栈的知识沉淀和传播。 &#x1f497; &#x1f33b; CSDN入驻不久&#xff0c;希望大家多多支持&#xff0c;后续会继续提升文章质量&#xff0c;绝不滥竽充数…

前端八股速通(持续更新中...)

1、深拷贝和浅拷贝的区别 浅拷贝&#xff1a;浅拷贝是拷贝一层&#xff0c;引用类型共享地址。 如果属性是基本类型&#xff0c;拷贝的就是基本类型的值。 如果属性是引用类型&#xff0c;拷贝的就是内存地址。 意思是&#xff0c;当进行浅拷贝时&#xff0c;对于对象的每一…

(19)SSM-MyBatis

环境配置 第一步创建一个web工程 导入mybatis包、lombok包、jdbc包 <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.16</version></dependency><!-- https://mvnrepo…

C#初级——字典Dictionary

字典 字典是C#中的一种集合&#xff0c;它存储键值对&#xff0c;并且每个键与一个值相关联。 创建字典 Dictionary<键的类型, 值的类型> 字典名字 new Dictionary<键的类型, 值的类型>(); Dictionary<int, string> dicStudent new Dictionary<int, str…

【java基础】徒手写Hello, World!程序

文章目录 前提&#xff1a;java环境变量配置使用vscode编写helloworld解析 前提&#xff1a;java环境变量配置 https://blog.csdn.net/xzzteach/article/details/140869188 使用vscode编写helloworld code .为什么用code看下图 报错了&#xff01;&#xff01;&#xff01;&…

【深度学习】【语音】TTS,Phoneme-Level BERT (PL-BERT),抛弃词级别或超语素级别的预训练模型!

https://github.com/yl4579/PL-BERT 这篇文章的技术重点是提出了一种名为**Phoneme-Level BERT (PL-BERT)**的新模型,用于增强文本到语音(Text-to-Speech,TTS)合成中的韵律,通过语素预测来提高合成语音的自然度。 技术亮点及优势 语素级别的BERT模型: 现有的TTS模型通常…