UVA 11178 /// 向量旋转 两向量夹角

题目大意:

https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2119

 

向量旋转的推导

#include <bits/stdc++.h>
using namespace std;

const double eps=1e-10;
double add(double a,double b) {
    if(abs(a+b)<eps*(abs(a)+abs(b))) return 0;
    return a+b;
}
struct P {
    double x,y;
    P(){}
    P(double _x,double _y):x(_x),y(_y){}
    P operator - (P p) {
        return P(add(x,-p.x),add(y,-p.y)); }
    P operator + (P p) {
        return P(add(x,p.x),add(y,p.y)); }
    P operator / (double d) {
        return P(x/d,y/d); }
    P operator * (double d) {
        return P(x*d,y*d); }
    double dot (P p) {
        return add(x*p.x,y*p.y); }
    double det (P p) {
        return add(x*p.y,-y*p.x); }
    bool operator <(const P& p)const {
        return x<p.x || (x==p.x && y<p.y);
    }
    bool operator ==(const P& p)const {
        return abs(x-p.x)<eps && abs(y-p.y)<eps;
    }
    void read(){
        scanf("%lf%lf",&x,&y); }
    void ptf() {
        printf("%.6f %.6f ",&x,&y); }
};

double lenP(P a) {
    return sqrt(a.dot(a));
}
double Angle(P a,P b) {
    return acos(a.dot(b)/lenP(a)/lenP(b));
} /// a.dot(b)=|a||b|cos(ang)
P Rotate(P a,double rad) {
    P r=P(sin(rad),cos(rad));
    return P(a.det(r),a.dot(r));
}
P ins(P a,P v1,P b,P v2) {
    P v=a-b;
    double t=v2.det(v)/v1.det(v2);
    return a+v1*t;
}
P insGet(P a,P b,P c) {
    double a1=Angle(a-b,c-b);
    P v1=Rotate(c-b,a1/3.0); /// 向量bc绕b逆时针旋转a1/3角度

    double a2=Angle(a-c,b-c);
    P v2=Rotate(b-c,-a2/3.0); /// 向量cb绕c顺时针旋转a2/3角度
    return ins(b,v1,c,v2);
}

int main()
{
    int t; scanf("%d",&t);
    while(t--) {
        P a,b,c,d,e,f;
        a.read(), b.read(), c.read();
        d=insGet(a,b,c);
        e=insGet(b,c,a);
        f=insGet(c,a,b);
        d.ptf(), e.ptf(), f.ptf();
        printf("\n");
    }

    return 0;
}

 

posted @ 2018-09-29 13:59  _Jessie  阅读(228)  评论(0编辑  收藏  举报