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

来源:互联网 发布:微商辅助软件 编辑:程序博客网 时间:2024/06/05 04:30

#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)); }


0 0
原创粉丝点击