某公司的一道机考题的解答

昨天看到某个公司招聘出的一道题目,题目是这样的:判断任意三个点是否构成三角形,以及某个点是否位于指定的三角形内。
关于这个问题,我给出了自己的答案,首先解决第一个问题:
/// <summary>
/// IsTriangle判断集合中的头三个点PointF是否可以构成一个三角形
/// </summary>
public static bool IsTriangle(ArrayListptList)
{
PointFpt0 = (PointF)ptList[ 0 ];
PointFpt1
= (PointF)ptList[ 1 ];
PointFpt2
= (PointF)ptList[ 2 ];

//如果有两个点相同
if (pt0.Equals(pt1) || pt0.Equals(pt2) || pt1.Equals(pt2))
{
return false ;
}

float length_01 = ( float )Math.Sqrt((pt0.X - pt1.X) * (pt0.X - pt1.X) + (pt0.Y - pt1.Y) * (pt0.Y - pt1.Y));
float length_02 = ( float )Math.Sqrt((pt0.X - pt2.X) * (pt0.X - pt2.X) + (pt0.Y - pt2.Y) * (pt0.Y - pt2.Y));
float length_12 = ( float )Math.Sqrt((pt2.X - pt1.X) * (pt2.X - pt1.X) + (pt2.Y - pt1.Y) * (pt2.Y - pt1.Y));

bool result0 = (length_01 + length_02 <= length_12);
bool result1 = (length_01 + length_12 <= length_02);
bool result2 = (length_02 + length_12 <= length_01);

if (result0 || result1 || result2)
{
return false ;
}

return true ;
}
该解答分为两步,首先判断是否有重点,接着以两边之和大于第三边作为构成三角形的依据。

关于第二个问题稍微复杂些,不过幸好我在早期研究过并解决了一个更常见的问题,那就是判断一个点是否位于某个多边形内,而且即使这个多边形是凹多边形。这个功能在EnterpriseServerBase.XMath.Geometry.Polygon类中实现。
对于问题二的解答,我封装了Triangle类,它不仅借助Polygon类解决了问题二,而且可以计算三角形的面积和各个边长。
None.gif public class Triangle
ExpandedBlockStart.gif
{
InBlock.gif
privateArrayListvertextList=null;
InBlock.gif
privateArrayListlengthList=null;
InBlock.gif
privatefloatmyArea=0;
InBlock.gif
ContractedSubBlock.gifContractedBlock.gifExpandedBlockStart.gifctor#regionctor
InBlock.gif
publicTriangle(ArrayListptList)
ExpandedSubBlockStart.gif
{
InBlock.gif
if(!GeometryHelper.IsTriangle(ptList))
ExpandedSubBlockStart.gif
{
InBlock.gif
thrownewArgumentException("Thepointsinlistcan'tconstructatriangle!");
ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gif
this.vertextList=ptList;
InBlock.gif
this.FillLengthList();
ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gif
publicTriangle(PointFpt0,PointFpt1,PointFpt2)
ExpandedSubBlockStart.gif
{
InBlock.gifArrayListptList
=newArrayList();
InBlock.gifptList.Add(pt0);
InBlock.gifptList.Add(pt1);
InBlock.gifptList.Add(pt2);
InBlock.gif
InBlock.gif
if(!GeometryHelper.IsTriangle(ptList))
ExpandedSubBlockStart.gif
{
InBlock.gif
thrownewArgumentException("Thepointsinlistcan'tconstructatriangle!");
ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gif
this.vertextList=ptList;
InBlock.gif
this.FillLengthList();
ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gif
privatevoidFillLengthList()
ExpandedSubBlockStart.gif
{
InBlock.gifPointFpt0
=(PointF)this.vertextList[0];
InBlock.gifPointFpt1
=(PointF)this.vertextList[1];
InBlock.gifPointFpt2
=(PointF)this.vertextList[2];
InBlock.gif
InBlock.gif
floatlength_01=(float)Math.Sqrt((pt0.X-pt1.X)*(pt0.X-pt1.X)+(pt0.Y-pt1.Y)*(pt0.Y-pt1.Y));
InBlock.gif
floatlength_02=(float)Math.Sqrt((pt0.X-pt2.X)*(pt0.X-pt2.X)+(pt0.Y-pt2.Y)*(pt0.Y-pt2.Y));
InBlock.gif
floatlength_12=(float)Math.Sqrt((pt2.X-pt1.X)*(pt2.X-pt1.X)+(pt2.Y-pt1.Y)*(pt2.Y-pt1.Y));
InBlock.gif
InBlock.gif
this.lengthList=newArrayList();
InBlock.gif
this.lengthList.Add(length_12);
InBlock.gif
this.lengthList.Add(length_02);
InBlock.gif
this.lengthList.Add(length_01);
ExpandedSubBlockEnd.gif}

InBlock.gif
ExpandedBlockEnd.gif
#endregion
InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif
Area,GetEdgeLength#regionArea,GetEdgeLength
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**////<summary>
InBlock.gif
///Area三角形的面积
ExpandedSubBlockEnd.gif
///</summary>

InBlock.gifpublicfloatArea
ExpandedSubBlockStart.gif
{
InBlock.gif
get
ExpandedSubBlockStart.gif
{
InBlock.gif
if(this.myArea==0)
ExpandedSubBlockStart.gif
{
InBlock.gif
this.myArea=this.GetArea();
ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gif
returnthis.myArea;
ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gif
privatefloatGetArea()
ExpandedSubBlockStart.gif
{
InBlock.gif
floatlen0=(float)this.lengthList[0];
InBlock.gif
floatlen1=(float)this.lengthList[1];
InBlock.gif
floatlen2=(float)this.lengthList[2];
InBlock.gif
InBlock.gif
floatp=(len0+len1+len2)*0.5f;
InBlock.gif
InBlock.gif
return(float)Math.Sqrt(p*(p-len0)*(p-len1)*(p-len2));
ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gif
InBlock.gif
publicfloatGetEdgeLength(intindex)//0<=index<=2
ExpandedSubBlockStart.gif
{
InBlock.gif
if((index<0)||(index>2))
ExpandedSubBlockStart.gif
{
InBlock.gif
return0;
ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gif
return(float)this.lengthList[index];
ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif
Contains#regionContains
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**////<summary>
InBlock.gif
///Contains判断某点是否在三角形内部
ExpandedSubBlockEnd.gif
///</summary>

InBlock.gifpublicboolContains(PointFpt)
ExpandedSubBlockStart.gif
{
InBlock.gifPolygonpoly
=newPolygon(this.vertextList);
InBlock.gif
InBlock.gif
returnpoly.Contains(pt);
ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif
#endregion

ExpandedBlockEnd.gif}

Polygon类的实现比较复杂,代码也比较多,源码就不列出来了,可以点击 这里下载。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值