计算几何 矢量表示

#include<iostream>
#include<cmath>
using namespace std;

struct Vector{
	double x;   //表示从0点到 (x,y)的矢量。 
	double y;   //对矢量只关心方向和长度,不关心(位置)起点终点
	
	Vector(){   //构造函数  无参构造 
	}
	Vector(double xx,double yy):x(xx),y(yy){  //两参构造 
	}
	
	//矢量加法 
	Vector operator+(const Vector &p)const{
		return Vector(p.x+x,p.y+y);
	} 
	
	//矢量减法
	Vector operator-(const Vector &p)const{
	return Vector(x-p.x,y-p.y);
	}
	
	
	//矢量点乘
	double operator*(const Vector &p)const{
		return p.x*x+p.y*y;
	} 
	
	//矢量叉乘
	double operator^(const Vector &p)const{
		return p.x*y-p.y*x;
	} 
	
	//矢量模长
	double length(){
		return sqrt(x*x+y*y);
	} 
	
	//矢量单位化
	Vector unit(){
		return Vector(x/length(),y/length());
	} 
	
	//矢量投影长度 
	double project(const Vector &p){
		return p*unit();
	} 
}; 



int main(){
	Vector A(3,4),B(1,0),C;
	double c;
	C = A + B;
	printf("%.2lf %.2lf\n",C.x,C.y);
	C = A - B;
	printf("%.2lf %.2lf\n",C.x,C.y);
	c = A * B;
	printf("%.2lf\n",c);
	c = A ^ B;
	printf("%.2lf\n",c);
	printf("%.2lf %.2lf\n",A.length(),B.length());
	C = A.unit();
	printf("%.2lf %.2lf\n",C.x,C.y);
	c = B.project(A);//A在B方向的投影 
	printf("%.2lf\n",c);
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值