C# :: Aufgabe #261
5 Lösungen

Alle Winkel von einem Dreieck ausrechnen...
Fortgeschrittener - C#
von maxi72501
- 06.05.2019 um 15:01 Uhr
Gegeben sind alle Koordinaten. Die Punkte A und B gibt der Benutzer ein und die C Koordinate ist der Koordinaten Ursprung (0, 0).
Anhand der Seiten soll der Flächeninhalt und alle Winkel berechnet werden.
Anhand der Seiten soll der Flächeninhalt und alle Winkel berechnet werden.
Lösungen:

using System; namespace DreieckRechner { class Program { static void Main(string[] args) { Console.Write("A x: "); double Ax; double.TryParse(Console.ReadLine(), out Ax); Console.Write("A y: "); double Ay; double.TryParse(Console.ReadLine(), out Ay); Console.Write("B x: "); double Bx; double.TryParse(Console.ReadLine(), out Bx); Console.Write("B y: "); double By; double.TryParse(Console.ReadLine(), out By); Console.Write("C x: "); double Cx; double.TryParse(Console.ReadLine(), out Cx); Console.Write("C y: "); double Cy; double.TryParse(Console.ReadLine(), out Cy); double a = Math.Sqrt(Math.Pow(Bx - Cx, 2) + Math.Pow(By - Cy, 2)); double b = Math.Sqrt(Math.Pow(Ax - Cx, 2) + Math.Pow(Ay - Cy, 2)); double c = Math.Sqrt(Math.Pow(Ax - Bx, 2) + Math.Pow(Ay - By, 2)); a = Math.Round(a, 3); b = Math.Round(b, 3); c = Math.Round(c, 3); Console.WriteLine("\n-------------------\nLängen: \na= " + a + " cm \nb= " + b + " cm \nc= " + c + " cm\n-------------------\n"); double winkelA = Math.Round((180 / Math.PI) * Math.Acos((Math.Pow(b, 2) + Math.Pow(c, 2) - Math.Pow(a, 2)) / (2 * b * c)), 3); double winkelB = Math.Round((180 / Math.PI) * Math.Acos((Math.Pow(a, 2) + Math.Pow(c, 2) - Math.Pow(b, 2)) / (2 * a * c)), 3); double winkelC = 180 - winkelA - winkelB; Console.WriteLine("\n-------------------\nWinkel: \nAplpha= " + winkelA + " Grad \nBeta= " +winkelB +" Grad \nGamma= "+winkelC+ " Grad\n-------------------\n"); double u = a + b + c; double fläche = Math.Round((Math.Sqrt(u / 2 * (u / 2 - a) * (u / 2 - b) * (u / 2 - c))), 3); Console.WriteLine("\n-------------------\nUmfang= " + u + " cm\nFläche= " + fläche+ " cm*2\n-------------------"); Console.Read(); } } }

using System; namespace DreieckCalc { /* Gegeben sind alle Koordinaten.Die Punkte A und B gibt der Benutzer ein und die C Koordinate ist der Koordinaten Ursprung (0, 0). Anhand der Seiten soll der Flächeninhalt und alle Winkel berechnet werden. */ class Program { static Double[] a = new Double[2]; static Double[] b = new Double[2]; static Double[] c = { 0, 0 }; static Double sA, sB, sC, fi, alpha, beta, gamma; static void Main(string[] args) { Eingeben(); Rechnen(); Ausgeben(); } static void Eingeben() { int kompos; string koordhelp; //A Console.WriteLine("Gib die Koordianten von A ein!\nFormat: x,y"); koordhelp = Console.ReadLine(); kompos = koordhelp.IndexOf(','); if(kompos == -1) { Console.Clear(); Console.WriteLine("Komma konnte nicht gefunden werden!"); return; } a[0] = Convert.ToDouble(koordhelp.Substring(0, kompos)); a[1] = Convert.ToDouble(koordhelp.Substring(kompos + 1)); //B Console.WriteLine("Gib die Koordianten von B ein!\nFormat: x,y"); koordhelp = Console.ReadLine(); kompos = koordhelp.IndexOf(','); if (kompos == -1) { Console.Clear(); Console.WriteLine("Komma konnte nicht gefunden werden!"); return; } b[0] = Convert.ToDouble(koordhelp.Substring(0, kompos)); b[1] = Convert.ToDouble(koordhelp.Substring(kompos + 1)); } static void Rechnen() { //Seite b sB = Math.Sqrt((Math.Pow(a[0],2) + Math.Pow(a[1],2))); //Seite a sA = Math.Sqrt((Math.Pow(b[0], 2) + Math.Pow(b[1], 2))); //Seite c double helpc1; double helpc2; helpc1 = a[0] - b[0]; helpc2 = a[1] - b[1]; if(helpc1 < 0 ) { helpc1 = helpc1 * (-1); } if (helpc2 < 0) { helpc2 = helpc2 * (-1); } sC = Math.Sqrt(Math.Pow(helpc1, 2) + Math.Pow(helpc2, 2)); //Flächeninhalt fi = Math.Sqrt((sA + sB + sC)*(sA + sB - sC)*(sB + sC - sA)*(sC + sA - sB)) / 4; //Winkel alpha = Math.Acos((Math.Pow(sB, 2) + Math.Pow(sC, 2) - Math.Pow(sA, 2)) / (2 * sB * sC)); beta = Math.Acos((Math.Pow(sA, 2) + Math.Pow(sC, 2) - Math.Pow(sB, 2)) / (2 * sA * sC)); gamma = Math.Acos((Math.Pow(sA, 2) + Math.Pow(sB, 2) - Math.Pow(sC, 2)) / (2 * sA * sB)); alpha = RadUmrechner(alpha); beta = RadUmrechner(beta); gamma = RadUmrechner(gamma); } static double RadUmrechner(double rad) { double deg = 0.0; deg = 180 / Math.PI * rad; return deg; } static void Ausgeben() { Console.Clear(); Console.WriteLine("Das Ergebnis ist..."); Console.WriteLine("Flächeninhalt: {0}cm²\nAlpha: {1}°\nBeta: {2}°\nGamma:{3}°",fi,alpha,beta,gamma); Console.ReadLine(); } } }

using System; namespace Dreieck_Console { class Program { static void Main(string[] args) { Console.WriteLine("Koordinate Ax eingeben:"); Double Ax = Convert.ToDouble(Console.ReadLine()); Console.WriteLine("Koordinate Ay eingeben:"); Double Ay = Convert.ToDouble(Console.ReadLine()); Console.WriteLine("Koordinate Bx eingeben:"); Double Bx = Convert.ToDouble(Console.ReadLine()); Console.WriteLine("Koordinate By eingeben:"); Double By = Convert.ToDouble(Console.ReadLine()); Double a = Math.Sqrt(Math.Pow(Bx - Ax, 2) + Math.Pow(Ay - By, 2)); Console.WriteLine("Länge a = " + a); Double b = Math.Sqrt(Math.Pow(Ax, 2) + Math.Pow(Ay, 2)); Console.WriteLine("Länge b = " + b); Double c = Math.Sqrt(Math.Pow(Bx, 2) + Math.Pow(By, 2)); Console.WriteLine("Länge c = " + c); Double cosa = (Math.Pow(b, 2) + Math.Pow(c, 2) - Math.Pow(a, 2))/(2*b*c); Console.WriteLine("Winkel alpha = " + 180*Math.Acos(cosa)/Math.PI); Double cosb = (Math.Pow(a, 2) + Math.Pow(c, 2) - Math.Pow(b, 2)) / (2 * a * c); Console.WriteLine("Winkel Beta = " + 180 * Math.Acos(cosb) / Math.PI); Double cosc = (Math.Pow(a, 2) + Math.Pow(b, 2) - Math.Pow(c, 2)) / (2 * a * b); Console.WriteLine("Winkel Gamma = " + 180 * Math.Acos(cosc) / Math.PI); Double U = a + b + c; Double s = U / 2; Double F = Math.Sqrt(s*(s-a)*(s-b)*(s-c)); Console.WriteLine("Fläche = " + F); Console.ReadLine(); } } }

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Trainb { class Program { public static double Seitenlaenge(double x1, double y1, double x2, double y2) { double seite = Math.Sqrt(Math.Pow((x1 - x2), 2) + Math.Pow((y1 - y2), 2)); return seite; } public static double Winkel(double hypothenuse, double kathetea, double katheteb) { double winkel = Math.Acos((Math.Pow(hypothenuse, 2) - Math.Pow(kathetea, 2) - Math.Pow(katheteb, 2)) / (-2 * kathetea * katheteb)) * 180 / Math.PI; return winkel; } public static double Flaeche(double a, double b, double c) { double s = (a + b + c) / 2.0; double flaeche = Math.Sqrt(s * (s - a) * (s - b) * (s - c)); return flaeche; } static void Main(string[] args) { Console.WriteLine("Geben sie die erste x-Koordinate ein"); double x1 = double.Parse(Console.ReadLine()); Console.WriteLine("Geben sie die erste y-Koordinate ein"); double y1 = double.Parse(Console.ReadLine()); Console.WriteLine("Geben sie die zweite x-Koordinate ein"); double x2 = double.Parse(Console.ReadLine()); Console.WriteLine("Geben sie die zweite y-Koordinate ein"); double y2 = double.Parse(Console.ReadLine()); Console.WriteLine("Seite a = " + Seitenlaenge(x1, y1, 0, 0) + " cm"); Console.WriteLine("Seite b = " + Seitenlaenge(x2, y2, 0, 0) + " cm"); Console.WriteLine("Seite c = " + Seitenlaenge(x1, y1, x2, y2) + " cm"); Console.WriteLine("Winkel alpha = " + Winkel(Seitenlaenge(x1, y1, 0 ,0), Seitenlaenge(x2, y2, 0, 0),Seitenlaenge(x1, y1, x2, y2)) + " Grad"); Console.WriteLine("Winkel alpha = " + Winkel(Seitenlaenge(x2, y2, 0, 0), Seitenlaenge(x1, y1, 0, 0), Seitenlaenge(x1, y1, x2, y2)) + " Grad"); Console.WriteLine("Winkel alpha = " + Winkel(Seitenlaenge(x1, y1, x2, y2), Seitenlaenge(x2, y2, 0, 0), Seitenlaenge(x1, y1, 0, 0)) + " Grad"); Console.WriteLine("Die Fläche beträgt " + Flaeche(Seitenlaenge(x1, y1, 0, 0), Seitenlaenge(x2, y2, 0, 0), Seitenlaenge(x1, x2, y1, y2)) + " cm^2"); Console.ReadLine(); } } }

using System; namespace CS_Aufgabe_261_Dreiecksberechnung { class Program { static void Main(string[] args) { Console.WriteLine("xA eingeben: "); double xA = Convert.ToDouble(Console.ReadLine()); Console.WriteLine("yA eingeben: "); double yA = Convert.ToDouble(Console.ReadLine()); Console.WriteLine("xB eingeben: "); double xB = Convert.ToDouble(Console.ReadLine()); Console.WriteLine("yB eingeben: "); double yB = Convert.ToDouble(Console.ReadLine()); Triangle tri = new Triangle(xA, yA, xB, yB, 0.0, 0.0); var sides = tri.CalculateSidesFromCoordinates(); Console.WriteLine($"Seitenlaengen a={sides.Item1:0.0000}, b={sides.Item2:0.0000}, c={sides.Item3:0.0000}"); var angles = tri.CalculateAnglesFromCosineRule(); Console.WriteLine($"Winkel alp={angles.Item1 * 180.0 / Math.PI:0.0000}, bet={angles.Item2 * 180.0 / Math.PI:0.0000}, gam={angles.Item3 * 180.0 / Math.PI:0.0000}"); Console.WriteLine($"Winkelsumme: {(angles.Item1+ angles.Item2 + angles.Item3) *180.0 / Math.PI}"); Console.WriteLine($"Flaeche A={tri.CalculateArea():0.0000}"); } } internal class Triangle { public double XA { get; private set; } public double YA { get; private set; } public double XB { get; private set; } public double YB { get; private set; } public double XC { get; private set; } public double YC { get; private set; } public int Sides { get; private set; } public Triangle(double xA, double yA, double xB, double yB, double xC, double yC) { XA = xA; YA = yA; XB = xB; YB = yB; XC = xC; YC = yC; } public (double,double,double) CalculateSidesFromCoordinates() { double sideA = Math.Sqrt(Math.Pow(YC - YB, 2.0) + Math.Pow(XC - XB, 2.0)); double sideB = Math.Sqrt(Math.Pow(YC - YA, 2.0) + Math.Pow(XC - XA, 2.0)); double sideC = Math.Sqrt(Math.Pow(YB - YA, 2.0) + Math.Pow(XB - XA, 2.0)); return (sideA, sideB, sideC); } public (double, double, double) CalculateAnglesFromCosineRule() { var sides = CalculateSidesFromCoordinates(); double alpha = Math.Acos(( Math.Pow(sides.Item2, 2.0) + Math.Pow(sides.Item3, 2.0) - Math.Pow(sides.Item1, 2.0)) / (2.0 * sides.Item2 * sides.Item3)); double beta = Math.Acos(( Math.Pow(sides.Item1, 2.0) + Math.Pow(sides.Item3, 2.0) - Math.Pow(sides.Item2, 2.0)) / (2.0 * sides.Item1 * sides.Item3)); double gamma = Math.Acos(( Math.Pow(sides.Item1, 2.0) + Math.Pow(sides.Item2, 2.0) - Math.Pow(sides.Item3, 2.0)) / (2.0 * sides.Item1 * sides.Item2)); return (alpha, beta, gamma); } public double CalculateArea() { // ~Wikipedia/Trapezformel return 0.5 * (XA * (YB - YC) + XB * (YC - YA) + XC * (YA - YB)); } } }