2015年4月8日 星期三

[2015][Homework]Team02-Hw05

 
Program 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HW5
{
    class Program
    {
        static void Main(string[] args)
        {
            PinBoard board = new PinBoard(); //建立板子
            Triangle tri = new Triangle();   //建立三角形
            Random rand = new Random();      //產生亂數

            while (true)
            {
                //輸入板子的設定參數
                Console.WriteLine("請輸入板子有幾行:");
                board.cols = int.Parse(Console.ReadLine());
                Console.WriteLine("請輸入板子有幾列:");
                board.rows = int.Parse(Console.ReadLine());
                Console.WriteLine("請輸入板子行之間的距離:");
                board.rowInterval = Double.Parse(Console.ReadLine());
                Console.WriteLine("請輸入板子列之間的距離:");
                board.colInterval = Double.Parse(Console.ReadLine());
                //建立板子
                board.CreatePins();

            TrianglePin:
                Console.WriteLine("\n三角形的3個點是:");
                for (int i = 0; i < 3; i++)
                {
                    //以亂數產生三角形的三個點
                    tri.pointArray[i] = board.pinArray[rand.Next(board.rows), rand.Next(board.cols)];
                    Console.WriteLine("{0},{1}", tri.pointArray[i].xCoordinate, tri.pointArray[i].yCoordinate);
                }
                if (!tri.ConditionsOfTriangle())
                {
                    Console.WriteLine("產生的三點不構成三角形,重新產生三個點\n");
                    goto TrianglePin;
                }
                Console.WriteLine("\n三角形的周長是:{0}", tri.Perimeter());
                Console.WriteLine("\n三角形的面積是:{0}", tri.Area());
                Console.WriteLine("\n三角形的外接圓半徑是:{0}", tri.RadiusOfCircumcircle());

                Console.WriteLine("\n是否繼續程式?是請輸入Y,否則輸入N");
                char select = Convert.ToChar(Console.ReadLine());
                if (select != ('y') && select != ('Y'))
                {
                    break;
                }
                Console.Clear();
            }

        }
    }
}

Point 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HW5
{
    class Point
    {
        public double xCoordinate;
        public double yCoordinate;

        public double DistanceBetweenTwoPoints(Point pin) //計算該點和某點之間的距離
        {
            double Distance = Math.Sqrt(Math.Pow((pin.xCoordinate - this.xCoordinate), 2) + Math.Pow((pin.yCoordinate - this.yCoordinate), 2));
            return Distance;
        }
    }
}

PinBoard  
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HW5
{
    class PinBoard
    {
        public int rows = 0;
        public int cols = 0;
        public Double rowInterval = 0;
        public Double colInterval = 0;
        public Point[,] pinArray = null;

        public void CreatePins() //產生矩陣各點之位置,並依行列間距計算出各點x.y之座標,
        {
            pinArray = new Point[rows, cols];
            for (int i = 0; i < rows; i++)
            {
                for (int j = 0; j < cols; j++)
                {
                    pinArray[i, j] = new Point();
                    pinArray[i, j].xCoordinate = (i * rowInterval);
                    pinArray[i, j].yCoordinate = (j * colInterval);
                }
            }
        }

    }
}

Triangle
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HW5
{
    class Triangle
    {
        public Point[] pointArray = new Point[3]; //建立三角形的三個點

        public bool ConditionsOfTriangle()//判斷是否符合構成三角形的條件
        {
            double lengthA = pointArray[0].DistanceBetweenTwoPoints(pointArray[1]);
            double lengthB = pointArray[1].DistanceBetweenTwoPoints(pointArray[2]);
            double lengthC = pointArray[2].DistanceBetweenTwoPoints(pointArray[0]);
            double temp = 0;


            while (true)
            {
                if (lengthB > lengthA) //將lengthA變為最長邊
                {
                    temp = lengthB;
                    lengthB = lengthA;
                    lengthA = temp;
                }
                else if (lengthC > lengthB)//lengthC變為最短邊
                {
                    temp = lengthC;
                    lengthC = lengthB;
                    lengthB = temp;
                }
                else
                {
                    break;
                }
            }
            if (lengthA < (lengthB + lengthC)) //三角形兩邊和大於第三邊
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public double Perimeter() //計算三角形的周長
        {
            //由DistanceTo()計算兩點間的距離算出三角形三邊之邊長
            double lengthA = pointArray[0].DistanceBetweenTwoPoints(pointArray[1]); 
            double lengthB = pointArray[1].DistanceBetweenTwoPoints(pointArray[2]);
            double lengthC = pointArray[2].DistanceBetweenTwoPoints(pointArray[0]);

            double Perimeter = lengthA + lengthB + lengthC; 
            return Perimeter;
        }

        public double Area()
        {
            double lengthA = pointArray[0].DistanceBetweenTwoPoints(pointArray[1]); 
            double lengthB = pointArray[1].DistanceBetweenTwoPoints(pointArray[2]); 
            double lengthC = pointArray[2].DistanceBetweenTwoPoints(pointArray[0]);

            double Perimeter = lengthA + lengthB + lengthC; 
            //以海龍公式計算三角形面積
            double Area = Math.Sqrt((0.5 * Perimeter) * (0.5 * Perimeter - lengthA) * (0.5 * Perimeter - lengthB) * (0.5 * Perimeter - lengthC));
            return Area;
        }

               public double RadiusOfCircumcircle()
        {
            double lengthA = pointArray[0].DistanceBetweenTwoPoints(pointArray[1]); 
            double lengthB = pointArray[1].DistanceBetweenTwoPoints(pointArray[2]); 
            double lengthC = pointArray[2].DistanceBetweenTwoPoints(pointArray[0]);

            //由餘弦公式計算出角A之餘弦值
            double cosA = (lengthB * lengthB + lengthC * lengthC - lengthA * lengthA) / (2 * lengthB * lengthC);
            double sinA = Math.Sqrt(1 - cosA * cosA);
            //求得外接圓半徑
            double radius = 0.5 * lengthA / sinA; 
            return radius;
        }
    }
}

沒有留言:

張貼留言