【问题标题】:Triangle fit inside another triangle三角形适合另一个三角形
【发布时间】:2011-08-14 12:15:41
【问题描述】:

给定 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 的边对齐。因此这种情况仅在第二张图中显示的配置中满足。

你能帮我找出错误,建议一些我的算法失败的测试用例。也欢迎替代算法。

#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;
}

【问题讨论】:

  • 对齐底座的方向可能不是让一个三角形适合另一个三角形的唯一方法。该链接上的图像暗示了不同的方向。
  • 似乎是一种相当复杂的方法。为什么不使用一些几何图形并计算三角形的高度和各种角度。然后沿着第一个三角形的长边移动第二个最长的边,直到它的高度适合。比较角度并确保第一个三角形最长边有足够的空间容纳第二个(我可能已经解释得更好了)。
  • 你能说明比较哪些角度吗?
  • 您的算法对齐 2 个三角形的最长边,然后尝试根据高度找到合适的。但是对齐的碱基不必是适合退出的最大碱基。尝试以下测试用例:(3 3 2; 2 2 2)。如果我们将第一个三角形(3)的最长边与第二个三角形(2)的最长边对齐,则不合适。但是,如果对齐每个三角形的长度为 2 的基数,则显然存在拟合。
  • 我认为该图像暗示了不适合的情况。如果图像表示有效拟合,则无法按照问题所述在 2 个边缘处密封信封。

标签: c++ computational-geometry


【解决方案1】:

重心坐标!详细:

让“包络”三角形有顶点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

通过平面三角形的正弦定理可以得到两条边之间的夹角;尽管如果顶点处的角度是钝角(> PI / 2),您必须小心一点,因为正弦函数在区间 [0,PI] 上围绕 PI/2 对称。要检查角度是否为钝角,您还需要使用余弦定理,尽管您不需要计算余弦本身:如果 |AB|^2 + |AC|^2 > |BC|^2,则角度在 A 处是钝的。

我想总结一下。

【讨论】:

  • 您能更清楚地说明其他两个顶点是什么意思吗?如果我是正确的,那么如果 AC 和 A'C' 对齐,那么其他两个顶点指的是 B 和 B'。对吗?
  • 不,ABC 是参考点。如果对齐 AC 和 A'C',则需要转换 B' 和 C'。 (只有 A' 将具有已知坐标 (0,0),因为它与 A 重合。)
  • 让两个三角形分别是 ABC 和 A'B'C'。假设 AB 和 A'B' 沿 +x 轴在 A 处对齐。您的算法检查 B' 和 C' 是否在三角形 ABC 内。但是,当 C' 在 AC 的左侧时,情况会怎样。您的 alg 返回 false,但如果我们移动三角形 A'B'C',则可能存在拟合。尝试以下测试用例:(5,5,5);(3,3,2)。在 A 处对齐长度为 5 和 2 的碱基。
  • 您必须尝试对齐 A'B'C' 的所有三个边,并且必须在 UV 坐标 中执行检查。 (或者,您可以尝试计算 XY 坐标中的所有内容,并检查 C' 是否在 ABC 中。)
  • 我的代码基本上做同样的事情。你能帮我找出我的代码中的错误吗?您可以使用我在 timus 创建的模拟账户。我将不胜感激。
【解决方案2】:
//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
} 

【讨论】:

    【解决方案3】:

    可以从这里尝试 - http://www.springerlink.com/content/t10266u5832477w7/。到目前为止,问题似乎尚未解决,因此最好采用一些启发式方法来获得简单的情况(例如检查内接/外接圆、对齐边界等)并希望获得最好的结果。

    【讨论】:

    • 它不是未解决的 - 您链接的论文的摘要描述了一个解决方案:)
    • 没有找到这个问题的数学约束(插件公式)。但这并不意味着算法不存在。我提供的 URL 也有大约 100 个被接受的解决方案。
    • 啊,但是问题并不要求两个三角形全等。 (我猜这篇论文错误地将“congruent”用于“similar”)
    【解决方案4】:

    比较双精度时使用 epsilon (1e-10)!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-11
      • 1970-01-01
      • 1970-01-01
      • 2014-05-20
      相关资源
      最近更新 更多