2015年4月9日 星期四

[2015][Homework]Team03 - Hw05

Program為進入程式點,輸入與輸出 Point為類別,計算兩點距離 PinBoard為類別,產生PinBoard格點座標 Triangle為類別,產生triangle並計算周長、面積、外接圓半徑
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");
                goto Columns;
            }
 
        Rows:
            Console.WriteLine("請輸入板子有幾列:");
            board.rows = int.Parse(Console.ReadLine());
            if (board.rows < 1)
            {
                Console.WriteLine("列數不得小於1");
                goto Rows;
            }
 
        RowInterval: 
            Console.WriteLine("請輸入板子行之間的距離:");
            board.rowInterval = Double.Parse(Console.ReadLine());
            if (board.rowInterval < 0)
            {
                Console.WriteLine("距離不得小於0");
                goto RowInterval;
            }
 
            Console.WriteLine("請輸入板子列之間的距離:");
 
        ColumnInterval:
            board.columnInterval = Double.Parse(Console.ReadLine());
            if (board.columnInterval < 0)
            {
                Console.WriteLine("距離不得小於0");
                goto ColumnInterval;
            }
 
            board.CreatePins();
            board.SetPinsPosition();
 
        TrianglePin:
            Console.WriteLine("三角形的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("產生的三點不構成三角形! 再試一次!");
                goto TrianglePin;
            }
            Console.WriteLine("\n三角形的周長是:{0}",tri.Perimeter());
            Console.WriteLine("三角形的面積是:{0}", tri.Area());
            Console.WriteLine("三角形的外接圓半徑是:{0}", tri.RadiusOfCircumcircle());
            Console.ReadKey();
        }
    }
}
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;
        }
    }
}
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);
                }
            }
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace Board
{
    class Triangle
    {
        public Point[] pointArray = new Point[3];
 
        public double Perimeter()      //計算周長
        {
            double length1 = pointArray[0].DistanceTo(pointArray[1]);
            double length2 = pointArray[1].DistanceTo(pointArray[2]);
            double length3 = pointArray[2].DistanceTo(pointArray[0]);
            return (length1 + length2 + length3);
        }
 
        public double Area()      //計算面積
        {
            double length1 = pointArray[0].DistanceTo(pointArray[1]);
            double length2 = pointArray[1].DistanceTo(pointArray[2]);
            double length3 = pointArray[2].DistanceTo(pointArray[0]);
            double length = (length1 + length2 + length3) / 2;
            return Math.Sqrt(length * (length - length1) * (length - length2) * (length - length3));
        }
 
        public double RadiusOfCircumcircle()      //計算外接圓半徑
        {
            double length1 = pointArray[0].DistanceTo(pointArray[1]);
            double length2 = pointArray[1].DistanceTo(pointArray[2]);
            double length3 = pointArray[2].DistanceTo(pointArray[0]);
            double cosAngle = (length2 * length2 + length3 * length3 - length1 * length1) / (2 * length2 * length3);
            double sinAngle = Math.Sqrt(1 - cosAngle * cosAngle);
            return 0.5 * length1 / sinAngle;
        }
    }
}

沒有留言:

張貼留言