程式分為四個部分, 包含主程式及3個class
MyPoint 定義兩點以及之間的距離
Triangle 定義三點座標、三角形是否合理、求初期周長 面積 以及外接圓半徑
PinBoard 定義板子大小(行列 間隔數)把板子都先填上虛擬點
Program
MyPoint 定義兩點以及之間的距離
Triangle 定義三點座標、三角形是否合理、求初期周長 面積 以及外接圓半徑
PinBoard 定義板子大小(行列 間隔數)把板子都先填上虛擬點
Program
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HW5
{
class Program
{
static void Main(string[] args)
{
PinBoard board = new PinBoard();
Triangle tri = new Triangle();
MyPoint point = new MyPoint();
Random rand = new Random();
while (true)
{
Console.WriteLine("請決定點鎮的大小:");
RowsLabel:
Console.WriteLine("幾列:?");
board.rows = int.Parse(Console.ReadLine());
if (board.rows <= 0)
{
Console.WriteLine("列數必須要大於0!");
goto RowsLabel;
}
ColsLabel:
Console.WriteLine("幾行:?");
board.columns = int.Parse(Console.ReadLine());
if (board.columns <= 0)
{
Console.WriteLine("行數必須要大於0!");
goto ColsLabel;
}
Console.WriteLine("請決定點陣間隔");
XIntervalLabel:
Console.WriteLine("橫向間隔?");
board.rowInterval = int.Parse(Console.ReadLine());
if (board.rowInterval <= 0)
{
Console.WriteLine("橫向間隔必須要大於0!");
goto XIntervalLabel;
}
YIntervalLabel:
Console.WriteLine("縱向間隔?");
board.rowInterval = int.Parse(Console.ReadLine());
if (board.columnInterval <= 0)
{
Console.WriteLine("縱向間隔必須要大於0!");
goto YIntervalLabel;
}
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())
{
Console.WriteLine("此三點不構成三角形!");
goto TrianglePin;
}
for (int i = 0; i < 3; i++)
{
Console.WriteLine("({0},{1})",tri.pointArray[i].x,tri.pointArray[i].y);
}
Console.WriteLine("此三角形的周長為:{0}",tri.Perimeter());
Console.WriteLine("此三角形的面積為:{0}",tri.Area());
Console.WriteLine("此三角形的外接圓半徑為:{0}", tri.RadiusOfCircumcircle());
string select = Console.ReadLine();
Console.WriteLine("Again?");
if (("y" != select) && ("Y" == select))
break;
}
Console.Read();
}
}
}
MyPoint
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HW5
{
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;
}
}
}
Triangle
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HW5
{
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;
}
}
}
PinBoard
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HW5
{
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);
}
}
}
}
}
沒有留言:
張貼留言