http://acm.timus.ru/problem.aspx?space=1&num=1710
题意:给定一三角形A1B1C1,求是否存在三角形A2B2C2满足:
-
A1B1 =A2B2,
-
B1C1 =B2C2,
-
∠B1A1C1 =∠ B2A2C2.
且两三角形不全等。存在则输出三角形A2B2C2。
解法:只有当|AB|>|BC|且角C不为90度时才不全等。利用计算几何找三角形A2B2C2。
#include <cstdio>
#include <cstring>
#include <cmath>
#include <map>
#include <set>
#include <vector>
#include <iostream>
#include <algorithm>
#include <iomanip> //要用到格式控制符
using namespace std;
#define POINT struct point
#define LINESEG struct line
#define N 100005
struct point
{
double x,y;
}a,b,c,q,c1;
struct line
{
point s;
point e;
}l1;
int dis(struct point q,struct point p)
{
return (q.x-p.x)*(q.x-p.x)+(q.y-p.y)*(q.y-p.y);
}
double dist(POINT p1,POINT p2) // 返回两点之间欧氏距离
{
return( sqrt( (p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y) ) );
}
/*****************************************************************************
*
r=multiply(sp,ep,op),得到(sp-op)*(ep-op)的叉积
r>0:ep在矢量opsp的逆时针方向;
r=0:opspep三点共线;
r<0:ep在矢量opsp的顺时针方向
******************************************************************************
*/
double multiply(POINT sp,POINT ep,POINT op)
{
return((sp.x-op.x)*(ep.y-op.y)-(ep.x-op.x)*(sp.y-op.y));
}
/*****************************************************************************
**
r=dotmultiply(p1,p2,op),得到矢量(p1-op)和(p2-op)的点积,如果两个矢量都非零矢量
r<0:两矢量夹角为锐角;r=0:两矢量夹角为直角;r>0:两矢量夹角为钝角
******************************************************************************
*/
double dotmultiply(POINT p1,POINT p2,POINT p0)
{
return ((p1.x-p0.x)*(p2.x-p0.x)+(p1.y-p0.y)*(p2.y-p0.y));
}
// 返回点p以点o为圆心逆时针旋转alpha(单位:弧度)后所在的位置
POINT rotate(POINT o,double alpha,POINT p)
{
POINT tp;
p.x-=o.x;
p.y-=o.y;
tp.x=p.x*cos(alpha)-p.y*sin(alpha)+o.x;
tp.y=p.y*cos(alpha)+p.x*sin(alpha)+o.y;
return tp;
}
/* 返回顶角在o点,起始边为os,终止边为oe的夹角(单位:弧度)
角度小于pi,返回正值
角度大于pi,返回负值
可以用于求线段之间的夹角
*/
double angle(POINT o,POINT s,POINT e)
{
double cosfi,fi,norm;
double dsx = s.x - o.x;
double dsy = s.y - o.y;
double dex = e.x - o.x;
double dey = e.y - o.y;
cosfi=dsx*dex+dsy*dey;
norm=(dsx*dsx+dey*dey)*(dex*dex+dey*dey);
cosfi /= sqrt( norm );
if (cosfi >= 1.0 ) return 0;
if (cosfi <= -1.0 ) return -3.1415926;
fi=acos(cosfi);
if (dsx*dey-dsy*dex>0) return fi; // 说明矢量os 在矢量 oe的顺时针方向
return -fi;
}
/*****************************\
* *
* 线段及直线的基本运算 *
* *
\*****************************/
double relation(POINT p,LINESEG l)
{
LINESEG tl;
tl.s=l.s;
tl.e=p;
return dotmultiply(tl.e,l.e,l.s)/(dist(l.s,l.e)*dist(l.s,l.e));
}
// 求点C到线段AB所在直线的垂足 P
POINT perpendicular(POINT p,LINESEG l)
{
double r=relation(p,l);
POINT tp;
tp.x=l.s.x+r*(l.e.x-l.s.x);
tp.y=l.s.y+r*(l.e.y-l.s.y);
return tp;
}
/* 求点p到线段l的最短距离,并返回线段上距该点最近的点np
注意:np是线段l上到点p最近的点,不一定是垂足 */
double ptolinesegdist(POINT p,LINESEG l,POINT &np)
{
double r=relation(p,l);
if(r<0)
{
np=l.s;
return dist(p,l.s);
}
if(r>1)
{
np=l.e;
return dist(p,l.e);
}
np=perpendicular(p,l);
return dist(p,np);
}
// 求点p到线段l所在直线的距离,请注意本函数与上个函数的区别
double ptoldist(POINT p,LINESEG l)
{
return abs(multiply(p,l.e,l.s))/dist(l.s,l.e);
}
int main()
{
freopen("a","r",stdin);
int ax,ay,bx,by,cx,cy;
while (cin>>ax>>ay)
{
cin>>bx>>by;
cin>>cx>>cy;
int lab,lbc,lac;
lab=(ax-bx)*(ax-bx)+(ay-by)*(ay-by);
lbc=(bx-cx)*(bx-cx)+(by-cy)*(by-cy);
lac=(ax-cx)*(ax-cx)+(ay-cy)*(ay-cy);
if(lab==(lbc+lac))
{
cout<<"YES"<<endl;
continue;
}
a.x=ax;
a.y=ay;
b.x=bx;
b.y=by;
c.x=cx;
c.y=cy;
if (lab>lbc)
{
l1.e=a;
l1.s=c;
q=perpendicular(b,l1);
c1.x=q.x*2-c.x;
c1.y=q.y*2-c.y;
cout<<"NO"<<endl;
cout<<setprecision(15)<<a.x<<' '<<a.y<<endl<<b.x<<' '<<b.y<<endl<<c1.x<<' '<<c1.y;//#include <iomanip> //要用到格式控制符
//printf("%.15lf %.15lf\n%.15lf %.15lf\n%.15lf %.15lf\n",a.x,a.y,b.x,b.y,c1.x,c1.y);
}
else cout<<"YES"<<endl;
}
return 0;
}