# 物件導向QUIZ3+4+5 ## Quiz3 ``` using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace _106_Quiz3 { class Program { static void Main(string[] args) { string[] tokens; Triangle tri=new Triangle(); while (true) { Console.WriteLine("請輸入三角形的三個頂點座標,並計算其特性"); Console.Write("(x1,y1)= "); tokens = Console.ReadLine().Split(); tri.Point1X = Double.Parse(tokens[0]); tri.Point1Y = Double.Parse(tokens[1]); Console.Write("(x2,y2)= "); tokens = Console.ReadLine().Split(); tri.Point2X = Double.Parse(tokens[0]); tri.Point2Y = Double.Parse(tokens[1]); Console.Write("(x3,y3)= "); tokens = Console.ReadLine().Split(); tri.Point3X = Double.Parse(tokens[0]); tri.Point3Y = Double.Parse(tokens[1]); if (!tri.isValid()) { Console.WriteLine("這三點不能組成三角形!"); } else { Console.WriteLine("Perimeter=" + tri.Perimeter().ToString()); Console.WriteLine("Area=" + tri.Area().ToString()); Console.WriteLine("Radius of Circumcircle=" + tri.RadiusOfCircumcircle().ToString()); if (tri.isRight()) Console.WriteLine("直角三角形!"); else Console.WriteLine("非直角三角形!"); } Console.WriteLine("Again? "); String check = Console.ReadLine(); if ((check != "y")&&(check!="Y")) break; } Console.WriteLine("End of Program!"); Console.ReadKey(); } } } ``` ``` using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace _106_Quiz3 { class Triangle { public double Point1X; public double Point1Y; public double Point2X; public double Point2Y; public double Point3X; public double Point3Y; private double SideLength(double X1, double Y1, double X2, double Y2) { return Math.Sqrt((X1 - X2) * (X1 - X2) + (Y1 - Y2) * (Y1 - Y2)); } public double Perimeter() { double s1 = SideLength(Point1X, Point1Y, Point2X, Point2Y); double s2 = SideLength(Point2X, Point2Y, Point3X, Point3Y); double s3 = SideLength(Point3X, Point3Y, Point1X, Point1Y); return (s1 + s2 + s3); } public double Area() { double s1 = SideLength(Point1X, Point1Y, Point2X, Point2Y); double s2 = SideLength(Point2X, Point2Y, Point3X, Point3Y); double s3 = SideLength(Point3X, Point3Y, Point1X, Point1Y); double s = (s1 + s2 + s3) / 2; return Math.Sqrt(s * (s - s1) * (s - s2) * (s - s3)); } public bool isValid() { double s1 = SideLength(Point1X, Point1Y, Point2X, Point2Y); double s2 = SideLength(Point2X, Point2Y, Point3X, Point3Y); double s3 = SideLength(Point3X, Point3Y, Point1X, Point1Y); if ((s1 + s2 <= s3) || (s2 + s3 <= s1) || (s1 + s3 <= s2)) return false; return true; } public bool isRight() { double s1 = SideLength(Point1X, Point1Y, Point2X, Point2Y); double s2 = SideLength(Point2X, Point2Y, Point3X, Point3Y); double s3 = SideLength(Point3X, Point3Y, Point1X, Point1Y); if ((s1 * s1 + s2 * s2 - s3 * s3 < 1e-10) || (s2 * s2 + s3 * s3 - s1 * s1 < 1e-10) || (s1 * s1 + s3 * s3 - s2 * s2 < 1e-10)) return true; return false; } public double RadiusOfCircumcircle() { double s1 = SideLength(Point1X, Point1Y, Point2X, Point2Y); double s2 = SideLength(Point2X, Point2Y, Point3X, Point3Y); double s3 = SideLength(Point3X, Point3Y, Point1X, Point1Y); double CosAlpha = (s2 * s2 + s3 * s3 - s1 * s1) / (2 * s2 * s3); double SinAlpha = Math.Sqrt(1 - CosAlpha * CosAlpha); return 0.5 * s1 / SinAlpha; } } } ``` ## Quiz 4 ``` using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace _106_Quiz4 { class Program { static void Main(string[] args) { Triangle tri = new Triangle(); string[] tokens; while (true) { Console.WriteLine("請依序輸入三角形的三個頂點"); for (int i = 0; i <= 2; i++) { Console.Write("(x" + (i + 1).ToString() + ", y" + (i + 1).ToString()+ ")= "); tokens = Console.ReadLine().Split(); tri.XCoord[i] = Double.Parse(tokens[0]); tri.YCoord[i] = Double.Parse(tokens[1]); } if (!tri.isValid()) Console.WriteLine("Not a Triangle, Please Input the new coordinates!"); else { Console.WriteLine("Perimeter=" + tri.Perimeter().ToString()); Console.WriteLine("Area=" + tri.Area().ToString()); Console.WriteLine("Radius of Circumcircle=" + tri.RadiusOfCircumcircle().ToString()); if (tri.isRight()) Console.WriteLine("This is a right triangle!"); } Console.Write("Again? "); String check = Console.ReadLine(); if ((check != "y") && (check != "Y")) break; } Console.WriteLine("End of Program!"); Console.ReadKey(); } } } ``` ``` using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace _106_Quiz4 { class Triangle { public double[] XCoord = new double[3]; public double[] YCoord = new double[3]; public const double Tol= 1e-10; private void SideLengths(double[] side) { //side[0] = Math.Sqrt((XCoord[0] - XCoord[1]) * (XCoord[0] - XCoord[1]) + (YCoord[0] - YCoord[1]) * (YCoord[0] - YCoord[1])); //side[1] = Math.Sqrt((XCoord[1] - XCoord[2]) * (XCoord[1] - XCoord[2]) + (YCoord[1] - YCoord[2]) * (YCoord[1] - YCoord[2])); //side[2] = Math.Sqrt((XCoord[2] - XCoord[0]) * (XCoord[2] - XCoord[0]) + (YCoord[2] - YCoord[0]) * (YCoord[2] - YCoord[0])); for (int i=0;i<3;i++) { side[i] = Math.Sqrt((XCoord[i] - XCoord[(i+1)%3]) * (XCoord[i] - XCoord[(i + 1) % 3]) + (YCoord[i] - YCoord[(i + 1) % 3]) * (YCoord[i] - YCoord[(i + 1) % 3])); } } public double Perimeter() { double[] s = new double[3]; SideLengths(s); double sum = 0; foreach (var element in s) sum += element; return (sum); } public double Area() { double[] s = new double[3]; SideLengths(s); double p = Perimeter() / 2; return Math.Sqrt(p * (p - s[0]) * (p - s[1]) * (p - s[2])); } public bool isValid() { double[] s = new double[3]; SideLengths(s); if ((Math.Abs(s[0] + s[1] - s[2]) < Tol) || (Math.Abs(s[1] + s[2] - s[0]) < Tol) || (Math.Abs(s[2] + s[0] - s[1]) < Tol)) return false; return true; } public bool isRight() { double[] s = new double[3]; SideLengths(s); if ((Math.Abs(s[0] * s[0] + s[1] * s[1] - s[2] * s[2]) < Tol) || (Math.Abs(s[1] * s[1] + s[2] * s[2] - s[0] * s[0]) < Tol) || (Math.Abs(s[0] * s[0] + s[2] * s[2] - s[1] * s[1]) < Tol)) return true; return false; } public double RadiusOfCircumcircle() { double[] s = new double[3]; SideLengths(s); double CosAlpha = (s[1] * s[1] + s[2] * s[2] - s[0] * s[0]) / (2 * s[1] * s[2]); double SinAlpha = Math.Sqrt(1 - CosAlpha * CosAlpha); return 0.5 * s[0] / SinAlpha; } } } ``` ## Quiz5 private void btn_Calculate_Click(object sender, EventArgs e) { string message=""; Triangle tri = new Triangle(); tri.CreatePoints(); tri.ptArr[0].xCoord = Double.Parse(txtPoint1X.Text); tri.ptArr[0].yCoord = Double.Parse(txtPoint1Y.Text); tri.ptArr[1].xCoord = Double.Parse(txtPoint2X.Text); tri.ptArr[1].yCoord = Double.Parse(txtPoint2Y.Text); tri.ptArr[2].xCoord = Double.Parse(txtPoint3X.Text); tri.ptArr[2].yCoord = Double.Parse(txtPoint3Y.Text); if (tri.isValid()) { if (cbx_Area.Checked) message += ("Area=" + tri.Area().ToString() + Environment.NewLine); if (cbx_Perimeter.Checked) message += ("Perimeter=" + tri.Perimeter().ToString() + Environment.NewLine); if(cbx_ShapeType.Checked) { switch (tri.ShapeType()) { case 1: message += "本三角形為直角三角形"; break; case 2: message += "本三角形為銳角三角形"; break; case 3: message += "本三角形為鈍角三角形"; break; default: message += "程式有錯"; break; } } } else { message += "此三點無法形成三角形"; } txt_Display.Text = message; }