呵,大版主当然不用参与,参与了也没你的奖品,哈哈!
\N现在把我用C++写的源码粘贴出来,谨供大家参考!至于源码中行缩进无法在贴子中正常显示的问题我就偷个懒不处理了,大家多花点心思慢慢看吧!
\N#include <iostream.h>
#include <math.h>
\Nclass Point
{
public:
double x;
double y;
};
\Nclass Triangle
{
private:
Point vertexA;
Point vertexB;
Point vertexC;
Point vertexD;
public:
Triangle()
{
vertexA.x=0;
vertexA.y=0;
vertexB.x=0;
vertexB.y=0;
vertexC.x=0;
vertexC.y=0;
vertexD.x=0;
vertexD.y=0;
}
void SetA(Point vertex)
{
vertexA.x=vertex.x;
vertexA.y=vertex.y;
}
void SetB(Point vertex)
{
vertexB.x=vertex.x;
vertexB.y=vertex.y;
}
void SetC(Point vertex)
{
vertexC.x=vertex.x;
vertexC.y=vertex.y;
}
void SetD(Point vertex)
{
vertexD.x=vertex.x;
vertexD.y=vertex.y;
}
\N //判断三点可否构成三角形
bool IsTriangle()
{
//用几何学中的直线方程(两点式)判断
if(((vertexC.y-vertexA.y)*(vertexB.x-vertexA.x))!=(vertexC.x-vertexA.x)*(vertexB.y-vertexA.y))
return true;
return false;
}
\N //*******************************************
//判断第四点是否在三角形内
//*******************************************
bool IsInside()
{
if(IsLineSegmentInside(vertexA,vertexB,vertexC))
{
if(IsLineSegmentInside(vertexA,vertexC,vertexB))
return true;
}
return false;
}
\Nprivate:
//*******************************************************************
//判断一个顶点与第四点连成的直线,和该顶点的对边的交点是否在对边(线段)上
//*******************************************************************
bool IsLineSegmentInside(Point vertexA,Point vertexB,Point vertexC)
{
double denominator,p1,p2,a1,a2,b1,b2;
Point intersection;
//用线性代数中的克拉默法则解方程组求两线交点
//直线方程(两点式)的线性方程通式是:(y1-y2)x+(x2-x1)y=x2*y1-x1*y2
//a1,a2,b1,b2分别是两个直线方程中x,y的系数,p1,p2则是两方程的常量
a1=vertexA.y-vertexB.y; //通式中的(y1-y2)
a2=vertexC.y-vertexD.y;
b1=vertexB.x-vertexA.x; //通式中的(x2-x1)
b2=vertexD.x-vertexC.x;
denominator=a1*b2-a2*b1;
p1=vertexB.x*vertexA.y-vertexA.x*vertexB.y;
p2=vertexD.x*vertexC.y-vertexC.x*vertexD.y;
if(denominator!=0)
{
intersection.x=(p1*b2-p2*b1)/denominator;
intersection.y=(a1*p2-a2*p1)/denominator;
}
//求出交点后,判断交点是否在边的线段上
double d1,d2,d;
d=sqrt(pow((vertexB.x-vertexA.x),2)+pow((vertexB.y-vertexA.y),2));
d1=sqrt(pow((vertexB.x-intersection.x),2)+pow((vertexB.y-intersection.y),2));
d2=sqrt(pow((intersection.x-vertexA.x),2)+pow((intersection.y-vertexA.y),2));
if(d1>d || d2>d)
return false;
return true;
}
};
void main()
{
Triangle* triangle=new Triangle();
Point* vertex=new Point();
char m_continue='Y';
//do循环控制用户的重复计算需求
do
{
for(int i=1;i<=4;i++)
{
cout<<"Please input the "<<i<<" coordinates( total 4)"<<endl;
cin>>vertex->x>>vertex->y;
switch(i)
{
case 1:
triangle->SetA(*vertex);
break;
case 2:
triangle->SetB(*vertex);
break;
case 3:
triangle->SetC(*vertex);
break;
case 4:
triangle->SetD(*vertex);
\N }
}
if(triangle->IsTriangle()) //判断是否可构成三角形
{
cout<<"These three tops can constitute the triangle"<<endl;
if(triangle->IsInside()) //判断第四点是否在三角形内
cout<<" and the fourth top is in the triangle"<<endl;
else
cout<<",but the fourth top is not in the triangle"<<endl;
}
else
{
cout<<"These three tops can't constitute the triangle"<<endl;
}
cout<<"Do you still need to go on to calculate other tops?(Y/N)"<<endl;
cin>>m_continue;
cout<<"\n\n\n"<<endl;
}while(m_continue=='Y' || m_continue=='y');
}
\N这道题有很多种解法,但如何做到在题中尽可能的体现自己的功底就要看各人的修为了!哈,我也不知道自己做得好不好。