当前位置: 代码迷 >> 综合 >> 点和向量的表示和基本计算(刘汝佳版)
  详细解决方案

点和向量的表示和基本计算(刘汝佳版)

热度:41   发布时间:2023-09-23 04:10:44.0

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
struct Point{double x,y;Point(double x=0,double y=0):x(x),y(y){}
};
typedef Point Vector;
//向量+向量=向量,点+向量=点
Vector operator + (Vector A, Vector B) { return Vector(A.x+B.x, A.y+B.y); } 
//点-点=向量
Vector operator - (Vector A, Vector B) { return Vector(A.x-B.x, A.y-B.y); } 
//向量*数=向量
Vector operator * (Vector A, double p) { return Vector(A.x*p, A.y*p); }
//向量/数=向量
Vector operator / (Vector A, double p) { return Vector(A.x/p, A.y/p); }
bool operator < (const Point& a, const Point& b) {return a.x < b.x || (a.x == b.x && a.y < b.y);
}
//比较
const double eps = 1e-10;
int dcmp(double x){if(fabs(x) < eps) return 0;else return x < 0 ? -1: 1;
}
bool operator == (const Point& a,const Point& b) {return dcmp(a.x-b.x)==0&&dcmp(a.y-b.y)==0;
}
//基本计算
double Dot(Vector A, Vector B) { return A.x*B.x + A.y*B.y; }
double Length(Vector A) { return sqrt(Dot(A,A)); }
double Angle(Vector A, Vector B) { return acos(Dot(A, B)/Length(A)/Length(B)); } 
double Cross(Vector A, Vector B) { return A.x*B.y - A.y*B.x; } //叉积
double Area2(Point A, Point B, Point C) { return Cross(B-A,C-A); } //有向面积
//A向量逆时针旋转α rad 
//x'=xcosα-ysinα;
//y'=xsinα+ycosα;
Vector Rotate(Vector A, double rad){return Vector(A.x*cos(rad)-A.y*sin(rad),A.x*sin(rad)+A.y*cos(rad));
}
//A的单位法线,也就是逆时针90°,长度变为1,注意A要非零向量
Vector Normal(Vector A){ double L=Length(A);return Vector(-A.y/L,A.x/L);
}
//利用复数,可以更加简单的实现
#include <complex>
typedef complex<double> Point;
typedef Point Vector;
bool cmp(const Point& a, const Point& b){return real(a) < real(b) || (real(a) == real(b) && imag(a) < imag(b));
}
double Dot(Vector A, Vector B) { return real(conj(A)*B); }
double Cross(Vector A, Vector B) { return imag(conj(A)*B); }
Vector Rotate(Vector A, double rad) { return A*exp(Point(0,rad)); }






模板一:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const double eps = 1e-10;
struct Point{double x,y;Point(double x=0,double y=0):x(x),y(y){}
};
typedef Point Vector;int dcmp(double x){if(fabs(x) < eps) return 0;else return x < 0 ? -1: 1;
}
bool operator == (const Point& a,const Point& b) {return dcmp(a.x-b.x)==0&&dcmp(a.y-b.y)==0;
}
bool operator < (const Point& a, const Point& b) {return a.x < b.x || (a.x == b.x && a.y < b.y);
}Vector operator + (Vector A, Vector B) { return Vector(A.x+B.x, A.y+B.y); } 
Vector operator - (Vector A, Vector B) { return Vector(A.x-B.x, A.y-B.y); } 
Vector operator * (Vector A, double p) { return Vector(A.x*p, A.y*p); }
Vector operator / (Vector A, double p) { return Vector(A.x/p, A.y/p); }double Dot(Vector A, Vector B) { return A.x*B.x + A.y*B.y; }
double Length(Vector A) { return sqrt(Dot(A,A)); }
double Angle(Vector A, Vector B) { return acos(Dot(A, B)/Length(A)/Length(B)); } 
double Cross(Vector A, Vector B) { return A.x*B.y - A.y*B.x; } //叉积
double Area2(Point A, Point B, Point C) { return Cross(B-A,C-A); } //有向面积Vector Rotate(Vector A, double rad){ //A向量逆时针旋转α rad return Vector(A.x*cos(rad)-A.y*sin(rad),A.x*sin(rad)+A.y*cos(rad));
}
Vector Normal(Vector A){ //A的单位法线,也就是逆时针90°,长度变为1,注意A要非零向量double L=Length(A);return Vector(-A.y/L,A.x/L);
}


模板二:

// 模板二:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <complex>
#include <cmath>
using namespace std;
typedef complex<double> Point;
typedef Point Vector;
const double eps = 1e-10;
int dcmp(double x){if(fabs(x) < eps) return 0;else return x < 0 ? -1: 1;
}
bool cmp(const Point& a, const Point& b){return real(a) < real(b) || (real(a) == real(b) && imag(a) < imag(b));
}
double Dot(Vector A, Vector B) { return real(conj(A)*B); }
double Cross(Vector A, Vector B) { return imag(conj(A)*B); }
Vector Rotate(Vector A, double rad) { return A*exp(Point(0,rad)); }


  相关解决方案