Form1為視窗程式,包含輸入與輸出
Point為類別,計算兩點距離
PinBoard為類別,產生格點與格點座標
Triangle為類別,產生triangle三點座標,計算周長、面積與外接圓半徑
Program
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());
}
}
}
Form1
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();
txtRow.Text = board.rows.ToString();
txtColumn.Text = board.columns.ToString();
txtXInterval.Text = board.xInterval.ToString();
txtYInterval.Text = board.yInterval.ToString();
}
private void btnTriangle_Click(object sender, EventArgs e)
{
board.rows = int.Parse(txtRow.Text);
board.columns = int.Parse(txtColumn.Text);
board.xInterval = double.Parse(txtXInterval.Text);
board.yInterval = double.Parse(txtYInterval.Text);
board.CreatePins();
board.SetPinPosition();
Triangle tri = new Triangle();
Random rand = new Random();
int[] row = new int[3];
int[] col = new int[3];
for (int i = 0; i <= 2; i++)
{
row[i] = rand.Next(board.rows);
col[i] = rand.Next(board.columns);
tri.pointArray[i] = board.pinArray[row[i], col[i]];
}
txtPoint1Row.Text = row[0].ToString();
txtPoint2Row.Text = row[1].ToString();
txtPoint3Row.Text = row[2].ToString();
txtPoint1Col.Text = col[0].ToString();
txtPoint2Col.Text = col[1].ToString();
txtPoint3Col.Text = col[2].ToString();
txtPoint1X.Text = tri.pointArray[0].x.ToString();
txtPoint2X.Text = tri.pointArray[1].x.ToString();
txtPoint3X.Text = tri.pointArray[2].x.ToString();
txtPoint1Y.Text = tri.pointArray[0].y.ToString();
txtPoint2Y.Text = tri.pointArray[1].y.ToString();
txtPoint3Y.Text = tri.pointArray[2].y.ToString();
}
private void btnCalculate_Click(object sender, EventArgs e)
{
Triangle tri = new Triangle();
int[] row = new int[3];
int[] col = new int[3];
row[0] = Convert.ToInt16(txtPoint1Row.Text);
row[1] = Convert.ToInt16(txtPoint2Row.Text);
row[2] = Convert.ToInt16(txtPoint3Row.Text);
col[0] = Convert.ToInt16(txtPoint1Col.Text);
col[1] = Convert.ToInt16(txtPoint2Col.Text);
col[2] = Convert.ToInt16(txtPoint3Col.Text);
for (int i = 0; i <= 2; i++)
{
tri.pointArray[i] = board.pinArray[row[i], col[i]];
}
txtPoint1X.Text = tri.pointArray[0].x.ToString();
txtPoint2X.Text = tri.pointArray[1].x.ToString();
txtPoint3X.Text = tri.pointArray[2].x.ToString();
txtPoint1Y.Text = tri.pointArray[0].y.ToString();
txtPoint2Y.Text = tri.pointArray[1].y.ToString();
txtPoint3Y.Text = tri.pointArray[2].y.ToString();
txtOutput.Clear();
if (tri.ConditionsOfTriangle()==false)
{
txtOutput.AppendText("This is not a triangle!");
}
else
{
txtOutput.AppendText("Perimeter = " + tri.Perimeter().ToString() + '\n');
txtOutput.AppendText("Area = " + tri.Area().ToString() + '\n');
txtOutput.AppendText("Radius of circumcircle = " + tri.RadiusOfCircumcircle().ToString() + '\n');
}
}
}
}
Point
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace quiz4
{
class Point
{
public double x;
public double y;
public double DistanceTo(Point p) //calculate distance between two points
{
return Math.Sqrt((this.x - p.x) * (this.x - p.x) + (this.y - p.y) * (this.y - p.y));
}
}
}
PinBoard
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace quiz4
{
class PinBoard
{
public int rows = 10;
public int columns = 10;
public double xInterval = 1;
public double yInterval = 1;
public Point[,] pinArray = null;
public void CreatePins() //create pins array
{
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 SetPinPosition() //set pins position (x,y)
{
double xPosition;
double yPosition;
yPosition = 0;
for (int i = 0; i < rows; i++)
{
xPosition = 0;
for (int j = 0; j < columns; j++)
{
pinArray[i, j].x = xPosition;
pinArray[i, j].y = yPosition;
xPosition += xInterval;
}
yPosition += yInterval;
}
}
}
}
Triangle
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace quiz4
{
class Triangle
{
public Point[] pointArray = new Point[3];
public bool ConditionsOfTriangle()
{
double length1 = pointArray[0].DistanceTo(pointArray[1]);
double length2 = pointArray[1].DistanceTo(pointArray[2]);
double length3 = pointArray[2].DistanceTo(pointArray[0]);
double temp = 0;
while (true)
{
if (length2 > length1)
{
temp = length2;
length2 = length1;
length1 = temp;
}
else if (length3 > length2)
{
temp = length3;
length3 = length2;
length2 = temp;
}
else
{
break;
}
}
if (length1 < (length2 + length3))
{
return true;
}
else
{
return false;
}
}
public double Perimeter() //calculate triangle perimeter
{
double length1 = pointArray[0].DistanceTo(pointArray[1]);
double length2 = pointArray[1].DistanceTo(pointArray[2]);
double length3 = pointArray[2].DistanceTo(pointArray[0]);
return (length1 + length2 + length3);
}
public double Area() //calculate triangle area
{
double length1 = pointArray[0].DistanceTo(pointArray[1]);
double length2 = pointArray[1].DistanceTo(pointArray[2]);
double length3 = pointArray[2].DistanceTo(pointArray[0]);
double length = (length1 + length2 + length3) / 2;
return Math.Sqrt(length * (length - length1) * (length - length2) * (length - length3));
}
public double RadiusOfCircumcircle() //calculate radius of circumcircle of triangle
{
double length1 = pointArray[0].DistanceTo(pointArray[1]);
double length2 = pointArray[1].DistanceTo(pointArray[2]);
double length3 = pointArray[2].DistanceTo(pointArray[0]);
double cosAngle = (length2 * length2 + length3 * length3 - length1 * length1) / (2 * length2 * length3);
double sinAngle = Math.Sqrt(1 - cosAngle * cosAngle);
return 0.5 * length1 / sinAngle;
}
}
}
沒有留言:
張貼留言