Point為類別,計算兩點距離
PinBoard為類別,產生pinboard格點座標
Triangle為類別,產生triangle並計算周長、面積、外接圓半徑
Program
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace homework5
{
class Program
{
static void Main(string[] args)
{
PinBoard board = new PinBoard();
Triangle tri = new Triangle();
Random rand = new Random();
while (true)
{
Console.WriteLine("Please input rows and columns");
Rows:
Console.WriteLine("How many rows?"); //input rows
board.rows = int.Parse(Console.ReadLine());
if (board.rows <= 0)
{
Console.WriteLine("Must be a positive integer!");
goto Rows;
}
Columns:
Console.WriteLine("How many columns?"); //input columns
board.columns = int.Parse(Console.ReadLine());
if (board.columns <= 0)
{
Console.WriteLine("Must be a positive integer!");
goto Columns;
}
Console.WriteLine("Please input interval between points");
XInterval:
Console.WriteLine("Interval along x-axis?"); //input x-interval
board.xInterval = double.Parse(Console.ReadLine());
if (board.xInterval <= 0)
{
Console.WriteLine("Must be positive!");
goto XInterval;
}
YInterval:
Console.WriteLine("Interval along y-axis?"); //input y-interval
board.yInterval = double.Parse(Console.ReadLine());
if (board.yInterval <= 0)
{
Console.WriteLine("Must be positive!");
goto YInterval;
}
board.CreatePins();
board.SetPinPosition();
for (int i = 0; i <= 2; i++) //create three points (x,y) randomly
{
tri.pointArray[i] = board.pinArray[rand.Next(board.rows), rand.Next(board.columns)];
}
Console.WriteLine("Position of three points that form a triangle are:"); //output answers
for (int i = 0; i <= 2; i++)
{
Console.WriteLine("({0},{1})", tri.pointArray[i].x, tri.pointArray[i].y);
}
Console.WriteLine("Perimeter = {0}", tri.Perimeter());
Console.WriteLine("Area = {0}", tri.Area());
Console.WriteLine("Radius of circumcircle = {0}", tri.RadiusOfCircumcircle());
Console.WriteLine("Again? (Y or N)");
string select = Console.ReadLine();
if (("y" != select) && ("Y" != select))
{
break;
}
}
Console.Read();
}
}
}
Point
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace homework5
{
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;
namespace homework5
{
class PinBoard
{
public int rows;
public int columns;
public double xInterval;
public double yInterval;
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;
namespace homework5
{
class Triangle
{
public Point[] pointArray = new Point[3];
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;
}
}
}
沒有留言:
張貼留言