首页 > 其他 > 详细

计算几何(更新中)

时间:2015-05-25 18:44:07      阅读:117      评论:0      收藏:0      [点我收藏+]
#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
const double eps = 1e-10;
typedef struct P
{
    double x, y;
    P(double x=0, double y=0):x(x), y(y){}
}V; // V means vector and P means point.

V operator + (V A, V B) {return V(A.x + B.x, A.y + B.y); }
V operator - (P A, P B) {return P(A.x - B.x, A.y - B.y); }
V operator * (V A, double t) {return V(A.x * t, A.y * t); }
V operator / (V A, double t) {return P(A.x / t, A.y / t); }

bool operator < (const P &A, P &B){return A.x < B.x || (A.x == B.x && A.y < B.y);}

int dcmp(double x)//三态函数
{
    if(fabs(x) < eps) return 0;
    else return x < 0 ? -1 : 1;
}

bool operator == (const P &A, const P &B)
{
    return dcmp(A.x - B.x) == 0 && dcmp(A.y - B.y) == 0;
}

double Dot(V A, V B) {return A.x * B.x + A.y * B.y; }//点积
double Length(V A) {return sqrt(Dot(A, A)); }
double Angle(V A, V B) {return acos(Dot(A, B) / Length(A) / Length(B)); }
double Cross(V A, V B) {return A.x * B.y - B.x * A.y; }
double Area(P A, P B, P C) {return Cross(B - A, C - A); }//twice of triangle area
V Rotate(V A, double rad) //rad is rotate angle
{
   return V(A.x * cos(rad) - A.y * sin(rad), A.x * sin(rad) + A.y * cos(rad));
}
P LineIntersection(P p, V v, P Q, V w)
{
    V u = p - Q;
    double t = Cross(w, u) / Cross(v, w);
    return p + v * t;
}

int main()
{
    P p1(0, 1), p2(1, 0), p3(1, 1);
//    cout<<Area2(p1, p2, p3)<<endl;
//    printf("%lf\n", Area2(p1, p2, p3));
//    cout<<Angle(p1, p2)<<endl;
//    cout<<pi / 2<<endl;
//    double a = 1;
//    printf("%.20lf\n", 2 * Angle(p1, p2));
//    printf("%.20lf\n", pi / 2);
    p2 = Rotate(p1, pi);
    printf("%.5lf + %.5lf\n", p2.x, p2.y);
    cout<<p2.x<<" "<<p2.y<<endl;
    cout<<sin(pi / 2)<<endl;
    cout<<cos(pi / 2)<<endl;
    cout<<cos(3.14 / 2)<<endl;
    return 0;
}

计算几何(更新中)

原文:http://blog.csdn.net/dojintian/article/details/45971955

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!