2015年4月12日 星期日

[2015][Quiz][Week07]Quiz04-40173032H

Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Quiz4
{
    public partial class Form1 : Form
    {
        PinBoard board = new PinBoard();
        TriangleInformation trinagle = new TriangleInformation();
        Random metricGenerator = new Random();
        int[] row = new int [3];
        int[] colunm = new int[3];


        public Form1()
        {
            InitializeComponent();
        }

        private void btnGenerateTriangle_Click(object sender, EventArgs e)
        {
            board.rows = int.Parse(txtRow.Text);
            board.columns = int.Parse(txtColunm.Text);
            board.xInterval = Convert.ToDouble(txtXInterval.Text);
            board.yInterval = Convert.ToDouble(txtYInterval.Text);

            board.CreateArray();
            board.SetPinBoardArray();

            while (true)
            {
                for (int i = 0; i <= 2; i++)
                {
                    row[i] = metricGenerator.Next(board.rows);
                    colunm[i] = metricGenerator.Next(board.columns);
                    trinagle.pointArray[i] = board.pinArray[row[i],colunm[i]];
                }
                if (trinagle.IsLegal())
                {
                    break;
                }
            }

            txtPoint1Row.Text = row[0].ToString();
            txtPoint1Colunm.Text = colunm[0].ToString();
            txtPoint1X.Text = trinagle.pointArray[0].x.ToString();
            txtPoint1Y.Text = trinagle.pointArray[0].y.ToString();

            txtPoint2Row.Text = row[1].ToString();
            txtPoint2Colunm.Text = colunm[1].ToString();
            txtPoint2X.Text = trinagle.pointArray[1].x.ToString();
            txtPoint2Y.Text = trinagle.pointArray[1].y.ToString();

            txtPoint3Row.Text = row[2].ToString();
            txtPoint3Colunm.Text = colunm[2].ToString();
            txtPoint3X.Text = trinagle.pointArray[2].x.ToString();
            txtPoint3Y.Text = trinagle.pointArray[2].y.ToString();

            btnCalculate.Enabled = true;
            
        }

        private void btnCalculate_Click(object sender, EventArgs e)
        {
            trinagle.pointArray[0] = board.pinArray[int.Parse(txtPoint1Row.Text),int.Parse(txtPoint1Colunm.Text)];
            trinagle.pointArray[1] = board.pinArray[int.Parse(txtPoint2Row.Text),int.Parse(txtPoint2Colunm.Text)];
            trinagle.pointArray[2] = board.pinArray[int.Parse(txtPoint3Row.Text),int.Parse(txtPoint3Colunm.Text)];

            txtPoint1X.Text = trinagle.pointArray[0].x.ToString();
            txtPoint1Y.Text = trinagle.pointArray[0].y.ToString();

            txtPoint2X.Text = trinagle.pointArray[1].x.ToString();
            txtPoint2Y.Text = trinagle.pointArray[1].y.ToString();

            txtPoint3X.Text = trinagle.pointArray[2].x.ToString();
            txtPoint3Y.Text = trinagle.pointArray[2].y.ToString();

            if (!trinagle.IsLegal())
            {
                return;
            }
            if (trinagle.pointArray[0].x > board.rows || trinagle.pointArray[0].y > board.columns)
            {
                return;
            }
            if (trinagle.pointArray[1].x > board.rows || trinagle.pointArray[1].y > board.columns)
            {
                return;
            }
            if (trinagle.pointArray[2].x > board.rows || trinagle.pointArray[2].y > board.columns)
            {
                return;
            }


            txtMessage.Text = "周長:" + trinagle.Perimeter().ToString();
            txtMessage.Text += Environment.NewLine;
            txtMessage.Text += ("外接圓半徑:Environment.NewLine" + trinagle.RadiusOfCircle().ToString()) + Environment.NewLine;
            
            txtMessage.Text += ("面積:" + trinagle.Area().ToString());
        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }
    }
}

MyPoint.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Quiz4
{
    public class MyPoint
    {
        public double x;
        public double y;

        public double DistanceTo(MyPoint P)
        {
            double Distance;
            Distance = Math.Sqrt((x - P.x) * (x - P.x) + (y - P.y) * (y - P.y));
            return Distance;
        }
    }
}

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

namespace Quiz4
{
    public class PinBoard
    {
        public int rows;
        public int columns;
        public double xInterval;
        public double yInterval;
        public MyPoint[,] pinArray = null;

        public void CreateArray()
        {
            pinArray = new MyPoint[rows, columns];
            for (int i = 0; i < rows; i++)
            {
                for (int j = 0; j < columns; j++)
                {
                    pinArray[i, j] = new MyPoint();
                }
            }
        }

        public void SetPinBoardArray()
        {
            double XCoordinate = 0;
            double YCoordinate;

            for (int i = 0; i < rows; i++)
            {
                YCoordinate = 0;
                for (int j = 0; j < columns; j++)
                {
                    pinArray[i, j].x = XCoordinate;
                    pinArray[i, j].y = YCoordinate;
                    YCoordinate += yInterval;
                }
                XCoordinate += xInterval;
            }
        }
    }
}

TriangleInformation.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Quiz4
{
    public class TriangleInformation
    {
        public MyPoint[] pointArray = new MyPoint[3];

        public void SetArray(MyPoint[] Metric)
        {
            for (int i = 0; i <= 2; i++)
            {
                pointArray[i] = Metric[i];
            }
        }

        public double Perimeter()
        {
            double LengthA = pointArray[0].DistanceTo(pointArray[1]);
            double LengthB = pointArray[1].DistanceTo(pointArray[2]);
            double LengthC = pointArray[2].DistanceTo(pointArray[0]);

            return LengthA + LengthB + LengthC;
        }

        public double Area()
        {
            double LengthA = pointArray[0].DistanceTo(pointArray[1]);
            double LengthB = pointArray[1].DistanceTo(pointArray[2]);
            double LengthC = pointArray[2].DistanceTo(pointArray[0]);
            double AverageLength = (LengthA + LengthB + LengthC) / 2;
            double AreaOfTriangle = Math.Sqrt(AverageLength * (AverageLength - LengthA) * (AverageLength - LengthB) * (AverageLength - LengthC));

            return AreaOfTriangle;
        }

        public double RadiusOfCircle()
        {
            double LengthA = pointArray[0].DistanceTo(pointArray[1]);
            double LengthB = pointArray[1].DistanceTo(pointArray[2]);
            double LengthC = pointArray[2].DistanceTo(pointArray[0]);
            double CosTheda = (Math.Pow(LengthB, 2) + Math.Pow(LengthC, 2) - Math.Pow(LengthA, 2)) / (2 * LengthB * LengthC);
            double SinTheda = Math.Sqrt((1 - Math.Pow(CosTheda, 2)));

            return LengthA / 2 / SinTheda;
        }

        public bool IsLegal()
        {
            double LengthA = pointArray[0].DistanceTo(pointArray[1]);
            double LengthB = pointArray[1].DistanceTo(pointArray[2]);
            double LengthC = pointArray[2].DistanceTo(pointArray[0]);

            if (LengthA + LengthB > LengthC && LengthB + LengthC > LengthA && LengthA + LengthC > LengthB)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}

Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Quiz4
{
    static class Program
    {
        /// 
        /// 應用程式的主要進入點。
        /// 
        [STAThread]
        static void Main()
        {            
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}

沒有留言:

張貼留言