2015年4月3日 星期五

[2015][Homework]Team06 - Hw05

程式分為四個部分
Program為主程式
Point為類別 點的x.y座標和到另一點的距離
PinBoard為類別 板子的大小.間距.創造點和設定點的位置
Triangle為類別 三個大頭針的位置.計算是否構成三角形.其周長.面積.外接圓半徑

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

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

        Columns:
            Console.WriteLine("請輸入板子有幾行:");
            board.columns = int.Parse(Console.ReadLine());
            if (board.columns < 1)
            {
                Console.WriteLine("行數不得小於1\n");
                goto Columns;
            }

        Rows:
            Console.WriteLine("請輸入板子有幾列:");
            board.rows = int.Parse(Console.ReadLine());
            if (board.rows < 1)
            {
                Console.WriteLine("列數不得小於1\n");
                goto Rows;
            }

        RowInterval: 
            Console.WriteLine("請輸入板子行之間的距離:");
            board.rowInterval = Double.Parse(Console.ReadLine());
            if (board.rowInterval < 0)
            {
                Console.WriteLine("距離不得小於0\n");
                goto RowInterval;
            }

            Console.WriteLine("請輸入板子列之間的距離:");

        ColumnInterval:
            board.columnInterval = Double.Parse(Console.ReadLine());
            if (board.columnInterval < 0)
            {
                Console.WriteLine("距離不得小於0\n");
                goto ColumnInterval;
            }

            board.CreatePins();
            board.SetPinsPosition();

        TrianglePin:
            Console.WriteLine("\n三角形的3個點是:");
            for (int i = 0; i < 3; i++)
            {
                tri.pointArray[i] = board.pinArray[rand.Next(board.rows), rand.Next(board.columns)];         //以亂數產生三角形的三個點
                Console.WriteLine("{0},{1}", tri.pointArray[i].xCoordinate, tri.pointArray[i].yCoordinate);
            }
            if (!tri.IsRight())
            {
                Console.WriteLine("產生的三點不構成三角形! 再試一次!\n");
                goto TrianglePin;
            }
            Console.WriteLine("\n三角形的周長是:{0}",tri.Perimeter());
            Console.WriteLine("三角形的面積是:{0}", tri.Area());
            Console.WriteLine("三角形的外接圓半徑是:{0}", tri.RadiusOfCircumcircle());
            Console.ReadKey();
        }
    }
}
Point
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

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

        public double DistanceTo(Point pin) //計算該點和某點之間的距離
        {
            double Distance = Math.Sqrt(Math.Pow((pin.xCoordinate - xCoordinate), 2) + Math.Pow((pin.yCoordinate - yCoordinate), 2));
            return Distance;
        }
    }
}
PinBoard
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Board
{
    class PinBoard
    {
        public int rows = 10;
        public int columns = 10;
        public Double rowInterval = 10;
        public Double columnInterval = 10;
        public Point[,] pinArray = null;

        public void CreatePins() //產生矩陣各點之位置
        {
            pinArray = new Point[rows, columns];
            for (int i = 0; i < rows; i++)
            {
                for (int j = 0; j < columns; j++)
                {
                    pinArray[i, j] = new Point();
                }
            }
        }

        public void SetPinsPosition() //依行列間距計算出各點x.y之座標
        {
            for (int i = 0; i < rows; i++)
            {
                for (int j = 0; j < columns; j++)
                {
                    pinArray[i, j].xCoordinate = (i * rowInterval);
                    pinArray[i, j].yCoordinate = (j * columnInterval);
                }
            }
        }
    }
}
Triangle
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

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

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

            double Perimeter = length1 + length2 + length3; //三邊和為周長
            return Perimeter;
        }

        public double Area()
        {
            double length1 = pointArray[0].DistanceTo(pointArray[1]); //由DistanceTo()計算兩點間的距離
            double length2 = pointArray[1].DistanceTo(pointArray[2]); //算出三角形三邊之邊長
            double length3 = pointArray[2].DistanceTo(pointArray[0]);

            double Perimeter = length1 + length2 + length3; //已求得三邊長度以海龍公式計算三角形面積
            double Area = Math.Sqrt((0.5 * Perimeter) * (0.5 * Perimeter - length1) * (0.5 * Perimeter - length2) * (0.5 * Perimeter - length3));
            return Area;
        }

        public bool IsRight()
        {
            double length1 = pointArray[0].DistanceTo(pointArray[1]); //由DistanceTo()計算兩點間的距離
            double length2 = pointArray[1].DistanceTo(pointArray[2]); //算出三角形三邊之邊長
            double length3 = pointArray[2].DistanceTo(pointArray[0]);
            double temp = 0;
            double copyLength1 = length1; //複製三邊邊長以便依大小排序
            double copyLength2 = length2;
            double copyLength3 = length3;

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

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

            double cosAlpha = (lengthB * lengthB + lengthC * lengthC - lengthA * lengthA) / (2 * lengthB * lengthC); //由餘弦公式計算出Alpha角之餘弦值
            double sinAlpha = Math.Sqrt(1 - cosAlpha * cosAlpha); //由萬用公式計算出Alpha角之正弦值
            double radius = 0.5 * lengthA / sinAlpha; //求得外接圓半徑

            return radius;
        }
    }
}

沒有留言:

張貼留言