主函式
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Homework5
{
class Program
{
static void Main(string[] args)
{
PinBoard board = new PinBoard();
TriangleInformation trinagle = new TriangleInformation();
Random metricGenerator = new Random();
char selection;
while(true)
{
rowInput:
Console.WriteLine("Row =");
board.Rows = int.Parse(Console.ReadLine());
if(board.Rows <= 1)
{
Console.WriteLine("輸入不合法,請從新輸入");
goto rowInput;
}
colunmInput:
Console.WriteLine("Colunm =");
board.Columns = int.Parse(Console.ReadLine());
if (board.Columns <= 1)
{
Console.WriteLine("輸入不合法,請從新輸入");
goto colunmInput;
}
xInput:
Console.WriteLine("橫向間距 = ");
board.XInterval = Convert.ToDouble(Console.ReadLine());
if(board.XInterval <= 0)
{
Console.WriteLine("輸入不合法,請從新輸入");
goto xInput;
}
yInput:
Console.WriteLine("縱向間距 = ");
board.YInterval = Convert.ToDouble(Console.ReadLine());
if (board.YInterval <= 0)
{
Console.WriteLine("輸入不合法,請從新輸入");
goto yInput;
}
board.CreateArray();
board.SetPinBoardArray();
while(true)
{
for (int i = 0; i <= 2; i++)
{
trinagle.PointArray[i] = board.PinArray[metricGenerator.Next(board.Rows), metricGenerator.Next(board.Columns)];
}
if(trinagle.IsLegal())
{
break;
}
}
Console.WriteLine("三角形三點座標分別為:");
for (int i = 0; i <= 2; i++)
{
Console.WriteLine("({0},{1})", trinagle.PointArray[i].X, trinagle.PointArray[i].Y);
}
Console.WriteLine("三角形周長 = {0}",trinagle.Perimeter());
Console.WriteLine("三角形面積 = {0}",trinagle.Area());
Console.WriteLine("三角形外接圓半徑 = {0}", trinagle.RadiusOfCircle());
Console.WriteLine("是否繼續執行?");
selection = Convert.ToChar(Console.ReadLine());
if (selection != 'Y' && selection != 'y')
{
break;
}
else
{
Console.Clear();
}
}
}
}
}
Mypoint
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Homework5
{
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Homework5
{
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Homework5
{
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;
}
}
}
}
沒有留言:
張貼留言