2015年6月18日 星期四

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

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

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

        public double DistanceTo(MyPoint pin)//兩點之間距離
        {
            double Distance = Math.Sqrt(Math.Pow((pin.x - x), 2) + Math.Pow((pin.y - y), 2));
            return Distance;
        }
    }
}

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

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

        public void CreatePins() //產生矩陣各點的位置
        {
            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 SetPinPosition() //依行列間距計算出各點x.y之座標
        {
            for (int i = 0; i < rows; i++)
            {
                for (int j = 0; j < columns; j++)
                {
                    pinArray[i, j].x = (i * rowInterval);
                    pinArray[i, j].y = (j * columnInterval);
                }
            }
        }
    }
}

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

namespace WindowsFormsApplication1
{
    class Triangle
    {
        public MyPoint[] pointArray = new MyPoint[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 isQualified()//判斷三角形
        {
            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;


            while (true) //將Length1設定為最大邊
            {
                if (Length1 < Length2)
                {
                    temp = Length1;
                    Length1 = Length2;
                    Length2 = temp;
                }
                else if (Length2 < Length3)
                {
                    temp = Length2;
                    Length2 = Length3;
                    Length3 = temp;
                }
                else
                    break;
            }

            if (Length1 < (Length2 + Length3))//兩邊和大於第三邊
                return true;
            else
                return false;

        }

        public double RadiusOfCircumcircle()//求三角形外接圓半徑
        {
            double Length1 = pointArray[0].DistanceTo(pointArray[1]);//用DistanceTo計算三邊長
            double Length2 = pointArray[1].DistanceTo(pointArray[2]);
            double Length3 = pointArray[2].DistanceTo(pointArray[0]);

            double cosA = (Length3 * Length3 + Length2 * Length2 - Length1 * Length1) / (2 * Length2 * Length3);
            double sinA = Math.Sqrt(1 - cosA * cosA);

            double radius = 0.5 * Length1 / sinA;
            return radius;
        }
    }
}

Forms
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 WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        PinBoard board = new PinBoard();
        Triangle tri = new Triangle();
        MyPoint point = new MyPoint();
        Random rand = new Random();
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void btnGenerateTriangle_Click(object sender, EventArgs e)
        {
            txtClear();//清理textbox
            board.rows = int.Parse(txtRow.Text);
            board.columns = int.Parse(txtColumn.Text);
            board.rowInterval = double.Parse(txtXInterval.Text);
            board.columnInterval = double.Parse(txtYinterval.Text);
            if (board.rows <= 0 || board.columns <= 0 || board.rowInterval <= 0 || board.columnInterval <= 0)//任一值為負即不符條件
            {
                txtOutput.AppendText("數值不得為負!");
                return;
            }
            board.CreatePins();
            board.SetPinPosition();
            TrianglePin:
            for (int i = 0; i < 3; i++)
            {
                tri.pointArray[i] = board.pinArray[rand.Next(board.rows), rand.Next(board.columns)];//隨機抓取三個點
            }
            if (!tri.isQualified())
            {
                goto TrianglePin;//若無法構成三角形即重來
            }
            txtCoordinateShow();
            txtRowsAndColumnsShow();
        }
      
        private void btnCaculate_Click(object sender, EventArgs e)
        {
            readRowsAndColumn();
            txtClear();
            txtRowsAndColumnsShow();
            txtCoordinateShow();
            txtShowProperties();
        } 
        private void txtClear()
        {
            txtOutput.Clear();
            txtPoint1Column.Clear();
            txtPoint1Row.Clear();
            txtPoint1XCoordinate.Clear();
            txtPoint1YCoordinate.Clear();
            txtPoint2Column.Clear();
            txtPoint2Row.Clear();
            txtPoint2XCoordinate.Clear();
            txtPoint2YCoordinate.Clear();
            txtPoint3Column.Clear();
            txtPoint3Row.Clear();
            txtPoint3XCoordinate.Clear();
            txtPoint3YCoordinate.Clear();
        }
        private void txtCoordinateShow()
        {
            txtPoint1XCoordinate.AppendText(tri.pointArray[0].x.ToString());
            txtPoint1YCoordinate.AppendText(tri.pointArray[0].y.ToString());
            txtPoint2XCoordinate.AppendText(tri.pointArray[1].x.ToString());
            txtPoint2YCoordinate.AppendText(tri.pointArray[1].y.ToString());
            txtPoint3XCoordinate.AppendText(tri.pointArray[2].x.ToString());
            txtPoint3YCoordinate.AppendText(tri.pointArray[2].y.ToString());
        }
        private void txtRowsAndColumnsShow()
        {
            txtPoint1Row.AppendText((tri.pointArray[0].x/board.rowInterval+1).ToString());
            txtPoint1Column.AppendText((tri.pointArray[0].y/board.columnInterval+1).ToString());
            txtPoint2Row.AppendText((tri.pointArray[1].x/board.rowInterval+1).ToString());
            txtPoint2Column.AppendText((tri.pointArray[1].y/board.columnInterval+1).ToString());
            txtPoint3Row.AppendText((tri.pointArray[2].x/board.rowInterval+1).ToString());
            txtPoint3Column.AppendText((tri.pointArray[2].y/board.columnInterval+1).ToString());
        }
        private void readRowsAndColumn()
        {
            tri.pointArray[0].x = (int.Parse(txtPoint1Row.Text) - 1) * board.rowInterval;
            tri.pointArray[0].y = (int.Parse(txtPoint1Column.Text) - 1) * board.columnInterval;
            tri.pointArray[1].x = (int.Parse(txtPoint2Row.Text) - 1) * board.rowInterval;
            tri.pointArray[1].y = (int.Parse(txtPoint2Column.Text) - 1) * board.columnInterval;
            tri.pointArray[2].x = (int.Parse(txtPoint3Row.Text) - 1) * board.rowInterval;
            tri.pointArray[2].y = (int.Parse(txtPoint3Column.Text) - 1) * board.columnInterval;
        }
        private void txtShowProperties()
        {
            txtOutput.AppendText("周長為");
            txtOutput.AppendText(tri.Perimeter().ToString());
            txtOutput.AppendText("\r\n面積為");
            txtOutput.AppendText(tri.Area().ToString());
            txtOutput.AppendText("\r\n外接圓半徑為");
            txtOutput.AppendText(tri.RadiusOfCircumcircle().ToString());
        }
    }
}

沒有留言:

張貼留言