Demo entry 6633530

151

   

Submitted by 34188 on Aug 02, 2017 at 15:38
Language: C++. Code size: 1.7 kB.

#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <cstdlib>
using namespace std;

const double esp = 1e-10;

struct Point
{
	double x, y;
	Point(double xx = 0, double yy = 0) : x(xx), y(yy) {}
};

typedef Point Vector;

//向量 + 向量 = 向量, 点 + 向量 = 点
Vector operator + (Vector A, Vector B)
{
	return Vector(A.x + B.x, A.y + B.y);
}

//点 - 点 = 向量
Vector operator - (Point A, Point B)
{
	return Vector(A.x - B.x, A.y - B.y);
}

//向量数乘
Vector operator * (Vector A, double p)
{
	return Vector(p * A.x, p * A.y);
}

//向量 / 数 = 向量
Vector operator / (Vector A, double p)
{
	return Vector(A.x / p, A.y / p);
}

bool operator < (Point A, Point B)
{
	return make_pair(A.x, A.y) < make_pair(B.x, B.y);
}

//浮点数符号函数
int dcmp(double x)
{
	if (fabs(x) < esp)
		return 0;
	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));
}

//计算向量夹角 A到B逆时针旋转的角
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;
}

//向量绕起点逆时针旋转rad弧度
//x' = x*cos(rad) - y*sin(rad)
//y' = x*sin(rad) - y*cos(rad)
Vector Rotate(Vector A, double rad)
{
	return Vector(A.x*cos(rad) - A.y*sin(rad), A.x*sin(rad) - A.y*cos(rad));
}

//计算向量的单位法向量
//左转90度再单位化
Vector Normal(Vector A)
{
	double l = Length(A);
	//单独处理零向量
	return l == 0 ? Vector(0, 0) : Vector(-A.y / l, A.x / l);
}

int main()
{

	return 0;
}

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).