程式分為
1.建立點型態
2.建立板子
3.建立三角形
4.視窗
5.主程式
點型態
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Quiz4
{
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;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Quiz4
{
class PinBoard
{
public int rows;
public int columns;
public Double rowInterval;
public Double columnInterval;
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;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Quiz4
{
class TriangleFunction
{
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;
}
}
}
視窗
using System;
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();
public Form1()
{
InitializeComponent();
}
private void btnGenerateTriengle_Click(object sender, EventArgs e)
{
TriangleFunction tri = new TriangleFunction();
Random rand = new Random();
board.rows = int.Parse(txtRow.Text);
board.columns = int.Parse(txtColumn.Text);
board.rowInterval = Double.Parse(txtXInterval.Text);
board.columnInterval = Double.Parse(txtYInterval.Text);
int[] rowRand = new int[3];
int[] columnRand = new int[3];
board.CreatePins();
board.SetPinsPosition();
TrianglePin:
for (int i = 0; i < 3; i++)
{
rowRand[i] = rand.Next(board.rows);
columnRand[i] = rand.Next(board.columns);
tri.pointArray[i] = board.pinArray[rowRand[i], columnRand[i]];
}
if (!tri.IsRight())
{
goto TrianglePin;
}
txtPoint1Row.Text = rowRand[0].ToString();
txtPoint1Column.Text = columnRand[0].ToString();
txtPoint2Row.Text = rowRand[1].ToString();
txtPoint2Column.Text = columnRand[1].ToString();
txtPoint3Row.Text = rowRand[2].ToString();
txtPoint3Column.Text = columnRand[2].ToString();
txtPoint1X.Text = (Double.Parse(txtPoint1Column.Text) * Double.Parse(txtXInterval.Text)).ToString();
txtPoint1Y.Text = (Double.Parse(txtPoint1Row.Text) * Double.Parse(txtYInterval.Text)).ToString();
txtPoint2X.Text = (Double.Parse(txtPoint2Column.Text) * Double.Parse(txtXInterval.Text)).ToString();
txtPoint2Y.Text = (Double.Parse(txtPoint2Row.Text) * Double.Parse(txtYInterval.Text)).ToString();
txtPoint3X.Text = (Double.Parse(txtPoint3Column.Text) * Double.Parse(txtXInterval.Text)).ToString();
txtPoint3Y.Text = (Double.Parse(txtPoint3Row.Text) * Double.Parse(txtYInterval.Text)).ToString();
txtTriangleSolution.Text = "周長:" + tri.Perimeter().ToString();
txtTriangleSolution.Text += Environment.NewLine;
txtTriangleSolution.Text += "面積:" + tri.Area().ToString();
txtTriangleSolution.Text += Environment.NewLine;
txtTriangleSolution.Text += "外接圓:" + tri.RadiusOfCircumcircle().ToString();
txtTriangleSolution.Text += Environment.NewLine;
}
private void btnCalculate_Click(object sender, EventArgs e)
{
TriangleFunction tri = new TriangleFunction();
for (int i = 0; i < 3;i++ )
{
tri.pointArray[i]=new Point();
}
board.rows = int.Parse(txtRow.Text);
board.columns = int.Parse(txtColumn.Text);
board.rowInterval = Double.Parse(txtXInterval.Text);
board.columnInterval = Double.Parse(txtYInterval.Text);
txtPoint1X.Text = (Double.Parse(txtPoint1Column.Text) * Double.Parse(txtXInterval.Text)).ToString();
txtPoint1Y.Text = (Double.Parse(txtPoint1Row.Text) * Double.Parse(txtYInterval.Text)).ToString();
txtPoint2X.Text = (Double.Parse(txtPoint2Column.Text) * Double.Parse(txtXInterval.Text)).ToString();
txtPoint2Y.Text = (Double.Parse(txtPoint2Row.Text) * Double.Parse(txtYInterval.Text)).ToString();
txtPoint3X.Text = (Double.Parse(txtPoint3Column.Text) * Double.Parse(txtXInterval.Text)).ToString();
txtPoint3Y.Text = (Double.Parse(txtPoint3Row.Text) * Double.Parse(txtYInterval.Text)).ToString();
tri.pointArray[0].xCoordinate = double.Parse(txtPoint1X.Text);
tri.pointArray[0].yCoordinate = double.Parse(txtPoint1Y.Text);
tri.pointArray[1].xCoordinate = double.Parse(txtPoint2X.Text);
tri.pointArray[1].yCoordinate = double.Parse(txtPoint2Y.Text);
tri.pointArray[2].xCoordinate = double.Parse(txtPoint3X.Text);
tri.pointArray[2].yCoordinate = double.Parse(txtPoint3Y.Text);
if (!tri.IsRight())
{
txtTriangleSolution.Text = "不是三角形";
}
else
{
txtTriangleSolution.Text = "周長:" + tri.Perimeter().ToString();
txtTriangleSolution.Text += Environment.NewLine;
txtTriangleSolution.Text += "面積:" + tri.Area().ToString();
txtTriangleSolution.Text += Environment.NewLine;
txtTriangleSolution.Text += "外接圓:" + tri.RadiusOfCircumcircle().ToString();
txtTriangleSolution.Text += Environment.NewLine;
}
}
}
}
主程式
using System;
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());
}
}
}
沒有留言:
張貼留言