给出2个三角形边长。确定第二个三角形是否适合第一个三角形?
有关详细信息,请阅读以下完整的问题陈述:
http://acm.timus.ru/problem.aspx?space=1&num=1566&locale=en
下面的实现尝试了对齐三角形底边的所有(3!)^ 2种可能的组合。然后它会尝试在第一个三角形内移动第二个三角形,同时检查第二个三角形的底边是否超过第一个三角形的底边。
但我一直得到错误答案(WA)#16。
我给出的案例是第二张图片。很明显,如果旋转PQR以对齐长度为2.77和3.0的边,则第三个顶点将不在三角形ABC内。长度为4.2的边只能沿len 5的边对齐。因此,只有在第二张图像中显示的配置才能满足此情况。
你能帮我找到这个bug,建议一些我的算法崩溃的测试用例。也欢迎替代算法。
#include <cmath>
#include <iostream>
using namespace std;
const double PI = atan(1.0)* 4;
// Traingle ABC (Envelope)
double xa, ya, xb, yb, xc, yc;
// Traingle PQR (Postcard)
double xp, yp, xq, yq, xr, yr;
// Angle between sides AB and AC
double theta;
double signWrtLine(double x1, double y1, double x2, double y2, double x, double y)
{
double A = y2 - y1;
double B = x1 - x2;
double C = -(A * x1 + B * y1);
return (A * x + B * y + C);
}
bool fit()
{
if ((xr > xc) || (yq > yb)) return false;
if (signWrtLine(xa, ya, xb, yb, xq, yq) < 0) {
double d = (yq / tan(theta)) - xq;
return (xr + d <= xc);
}
return (signWrtLine(xa, ya, xb, yb, xq, yq) >= 0 &&
signWrtLine(xb, yb, xc, yc, xq, yq) >= 0 &&
signWrtLine(xc, yc, xa, ya, xq, yq) >= 0);
}
bool fit(double a[], double b[])
{
// generate the 3! permutations of the envelope
// loops i,k
for (int i = 0; i < 3; i++) {
double angle;
double u = a[i], v = a[(i + 1) % 3], w = a[(i + 2) % 3];
for (int k = 0; k < 2; k++) {
switch (k) {
case 0:
xa = 0, ya = 0;
angle = theta = acos((u * u + v * v - w * w) / (2 * u * v));
xb = v * cos(angle), yb = v * sin(angle);
xc = u, yc = 0;
break;
case 1:
// reflect envelope
swap(v, w);
angle = theta = acos((u * u + v * v - w * w) / (2 * u * v));
xb = v * cos(angle), yb = v * sin(angle);
break;
}
// generate the 3! permutations of the postcard
// loops j,k
for (int j = 0; j < 3; j++) {
double angle;
double u = b[j], v = b[(j + 1) % 3], w = b[(j + 2) % 3];
for (int k = 0; k < 2; k++) {
switch (k) {
case 0:
xp = 0, yp = 0;
angle = acos((u * u + v * v - w * w) / (2 * u * v));
xq = v * cos(angle), yq = v * sin(angle);
xr = u, yr = 0;
break;
case 1:
// reflect postcard
swap(v, w);
angle = acos((u * u + v * v - w * w) / (2 * u * v));
xq = v * cos(angle), yq = v * sin(angle);
break;
}
if (fit()) return true;
}
}
}
}
return false;
}
int main()
{
double a[3], b[3];
for (int i = 0; i < 3; i++) cin >> a[i];
for (int i = 0; i < 3; i++) cin >> b[i];
if(fit(a, b)) cout << "YES" << endl;
else cout << "NO" << endl;
return 0;
}
答案 0 :(得分:2)
重心坐标!详细说明:
让“包络”三角形具有顶点A,B,C;在不失一般性的情况下,您可以将顶点A放置在原点,并将AB侧与+ x轴对齐。使用包络三角形的边长来找到顶点A处的角度,即边AB和AC之间的角度。使用此角度,您可以定义新的坐标系(u,v);在这个坐标系中,顶点坐标是A =(0,0),B =(1,0)和C =(0,1)。
现在,取另一个带顶点A',B',C'的三角形,首先找到3个顶点的XY坐标:(A'B',B'C',A'C' )与+ x坐标轴对齐。对于每个这样的对齐,将其他两个顶点转换为由包络三角形确定的UV坐标系。如果发生两个其他顶点具有(u,v)坐标,其中0 <= u,v <= 1且u + v <= 1,则三角形适合包络三角形。
两面之间的角度可以通过平面三角形的正弦定理获得;虽然你必须要小心,如果顶点的角度是钝角(> PI / 2),因为正弦函数在区间[0,PI]上围绕PI / 2对称。要检查角度是否为钝角,您还需要使用余弦定理,尽管您不需要计算余弦本身:if | AB | ^ 2 + | AC | ^ 2&gt; | BC | ^ 2,A处的角度是钝的。
我认为总结一下。
答案 1 :(得分:1)
//23/08/11 13:56
//determine if a triangle will fit inside a second triangle
#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;
const double pi= 3.1414926;
const double tri=180;//deg in triangle
double find_B_ang(double a,double b,double c);
double find_C_ang(double a,double b,double c);
double movetri_r , tc_ghosthor_B;
int main()
{double a=0.0,b=0.0,c=0.0,x=0.0,y=0.0,z=0.0;
double A=0.0,B=0.0,C=0.0,A1=0.0,B1=0.0,C1=0.0;// L&R base angles
double te_vert_B=0.0,te_hor_B=0.0,te_hor_C=0.0;
double tc_vert_B=0.0,tc_hor_B=0.0,tc_hor_C=0.0;
//points B and B1 are considered co-incedent
cout<<"\n\ndetermine if a triangular card will fit inside\n"
<<"a triangular envelope\n";
//envelope dimensions
cout<<"\nenter lengths of the sides of envelope (space between)\n";
cout<<"ensure longest of them is less than sum of other two\n";
do
{
cin>>a>>b>>c;//the e sides
if(c>a)swap(a,c);//sort sides in decending order
if(b>a)swap(a,b);
if(c>b)swap(b,c);
if(a >(b+c))
cout<<"WRONG...b+c must be greater than a";
}while(a >(b+c));
cout<<"\nthe sides of the envelope are "<<a<<','<<b<<','<<c<<endl;
B=find_B_ang(a,b,c);
C=find_C_ang(a,b,c);
te_vert_B=c*sin(B*pi/tri);//apex to base vertical line
te_hor_B=te_vert_B/tan(B*pi/tri);//B to vertical line
te_hor_C=a-te_hor_B;//C to vertical line
cout<<"-------------------------------------------\n";
//card dimensions
do
{
cout<<"\nenter lengths of sides of card (space between) \n";
cout<<"ensure longest of them is less than sum of other two\n";
do
{
cin>>x>>y>>z;//the c sides
if(z>x)swap(z,x);//sort sides in decending order
if(y>x)swap(y,x);
if(z>y)swap(y,z);
if(x>(y+z))
cout<<"WRONG...y+z must be greater than x\n";
}while(x>(y+z));
cout<<"\nthe sides of card are "<<x<<','<<y<<','<<z<<endl;//x is base
B1=find_B_ang(x,y,z);
C1=find_C_ang(x,y,z);
tc_vert_B=z*sin(B1*pi/tri);//apex to base vertical line
tc_hor_B=tc_vert_B/tan(B1*pi/tri);//B to vertical line
tc_hor_C=x-tc_hor_B;//C to vertical line
tc_ghosthor_B=tc_vert_B/tan(B*pi/tri);
movetri_r= abs(tc_ghosthor_B-tc_hor_B);
cout<<"------------------------------------------------\n";
//determine and advise if card fits within envelope
if(B1<B && tc_vert_B <(tc_hor_C + a-x)*tan(C*pi/tri))cout<<"\ntrue";
else if(B1<B && tc_hor_B< te_hor_B && tc_vert_B<te_vert_B)cout<<"true";
else if(B1>B && movetri_r<a-x && tc_vert_B<te_vert_B)cout<<"true";
else cout<<"\nfalse";
} while(x>0);
cout<<"\npress any key...";
cin.ignore();
cin.get();
return 0;
}
double find_B_ang(double a,double b,double c)
{
double X=0.0;
X=((a*a)+(c*c)-(b*b));
X/=2*a*c;
X=acos(X);
X*=tri/pi;
return X; //degrees
}
double find_C_ang(double a,double b,double c)
{
double X=0.0;
X=((a*a)+(b*b)-(c*c));
X/=2*a*b;
X=acos(X);
X*=tri/pi;
return X;//degrees
}
答案 2 :(得分:0)
可以从这里尝试 - http://www.springerlink.com/content/t10266u5832477w7/。到目前为止,问题似乎尚未得到解决,所以最好选择一些启发式方法来获得简单的案例(例如检查刻录/限定圆圈,对齐边界等)并希望获得最佳效果。
答案 3 :(得分:0)
比较双打时使用epsilon(1e-10)!