某公司的一道机考题的解答
生活随笔
收集整理的這篇文章主要介紹了
某公司的一道机考题的解答
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
???? 昨天看到某個公司招聘出的一道題目,題目是這樣的:判斷任意三個點是否構成三角形,以及某個點是否位于指定的三角形內。
??? 關于這個問題,我給出了自己的答案,首先解決第一個問題:
???
????????///?IsTriangle?判斷集合中的頭三個點PointF是否可以構成一個三角形
????????///?</summary>????????
????????public?static?bool?IsTriangle(ArrayList?ptList)
????????{
????????????PointF?pt0?=?(PointF)ptList[0]?;
????????????PointF?pt1?=?(PointF)ptList[1]?;
????????????PointF?pt2?=?(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類解決了問題二,而且可以計算三角形的面積和各個邊長。
{
????????private?ArrayList?vertextList?=?null?;
????????private?ArrayList?lengthList??=?null?;
????????private?float?myArea?=?0?;
???????ctor#region?ctor
????????public?Triangle(ArrayList?ptList)
????????{????????????
????????????if(!?GeometryHelper.IsTriangle(ptList))
????????????{
????????????????throw?new?ArgumentException("The?points?in?list?can't?construct?a?triangle?!")?;
????????????}
????????????this.vertextList?=?ptList?;
????????????this.FillLengthList()?;
????????}
????????public?Triangle(PointF?pt0?,PointF?pt1?,PointF?pt2)
????????{????
????????????ArrayList?ptList?=?new?ArrayList()?;
????????????ptList.Add(pt0)?;
????????????ptList.Add(pt1)?;
????????????ptList.Add(pt2)?;
????????????if(!?GeometryHelper.IsTriangle(ptList))
????????????{
????????????????throw?new?ArgumentException("The?points?in?list?can't?construct?a?triangle?!")?;
????????????}
????????????this.vertextList?=?ptList?;
????????????this.FillLengthList()?;
????????}
????????private?void?FillLengthList()
????????{
????????????PointF?pt0?=?(PointF)this.vertextList[0]?;
????????????PointF?pt1?=?(PointF)this.vertextList[1]?;
????????????PointF?pt2?=?(PointF)this.vertextList[2]?;????????
????????????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))?;
????????????this.lengthList?=?new?ArrayList()?;
????????????this.lengthList.Add(length_12)?;
????????????this.lengthList.Add(length_02)?;
????????????this.lengthList.Add(length_01)?;
????????}
????????#endregion
????????Area?,GetEdgeLength#region?Area?,GetEdgeLength
????????/**////?<summary>
????????///?Area?三角形的面積
????????///?</summary>????????
????????public?float?Area
????????{
????????????get
????????????{
????????????????if(this.myArea?==?0)
????????????????{
????????????????????this.myArea?=?this.GetArea()?;
????????????????}
????????????????return?this.myArea?;
????????????}
????????}????????
????????private?float?GetArea()
????????{
????????????float?len0?=?(float)this.lengthList[0]?;
????????????float?len1?=?(float)this.lengthList[1]?;
????????????float?len2?=?(float)this.lengthList[2]?;
????????????float?p?=?(len0?+?len1?+?len2)?*?0.5f?;
????????????return?(float)Math.Sqrt(p?*?(p-len0)?*?(p-len1)?*?(p-len2))?;
????????}
????????public?float?GetEdgeLength(int?index)//0<=?index?<=2
????????{
????????????if((index?<0)?||(index?>2))
????????????{
????????????????return?0?;
????????????}
????????????return?(float)this.lengthList[index]?;
????????}
????????#endregion
????????Contains#region?Contains
????????/**////?<summary>
????????///?Contains?判斷某點是否在三角形內部
????????///?</summary>????????
????????public?bool?Contains(PointF?pt)
????????{
????????????Polygon?poly?=?new?Polygon(this.vertextList)?;
????????????
????????????return?poly.Contains(pt)?;
????????}
????????#endregion
????}
??? Polygon類的實現比較復雜,代碼也比較多,源碼就不列出來了,可以點擊這里下載。
?
?
轉載于:https://www.cnblogs.com/zhuweisky/archive/2005/10/16/255836.html
總結
以上是生活随笔為你收集整理的某公司的一道机考题的解答的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 阿克苏洁雅装饰公司怎么样
- 下一篇: 我来评百度!