Reputation: 99
I have to programming a triangle-calculator, that can calculate from three given values all the other missing information's. I do know how I can calculate the values - that isn't the problem. But I don't know, how I can programming that in a good way.
The interface looks like this:
With this 12 input-fields there are many possible combinations. My first idea was to use if/else statements for every combination. But that is not very efficient. I'am sure, that there is a better solution, but I don't know how. Can anybody help me?
EDIT My code looks at the moment so:
protected void FlaecheBerechnen_Click(object sender, EventArgs e)
{
WerteAuslesen();
Berechnen();
}
protected void WerteAuslesen()
{
//Sides
string strSeite_a = this.txt_a.Text; if (strSeite_a == string.Empty) { i++; } else { if ((double.TryParse(strSeite_a, out seite_a) == false)) { GenerateErrorReport("Seite a"); } }
string strSeite_b = this.txt_b.Text; if (strSeite_b == string.Empty) { i++; } else { if ((double.TryParse(strSeite_b, out seite_b) == false)) { GenerateErrorReport("Seite b"); } }
string strSeite_c = this.txt_c.Text; if (strSeite_c == string.Empty) { i++; } else { if ((double.TryParse(strSeite_c, out seite_c) == false)) { GenerateErrorReport("Seite c"); } }
//Angles
string strWinkel_a = this.txtAlpha.Text; if (strWinkel_a == string.Empty) { i++; } else { if ((double.TryParse(strWinkel_a, out winkel_a) == false)) { GenerateErrorReport("Winkel Alpha"); } }
string strWinkel_b = this.txtBeta.Text; if (strWinkel_b == string.Empty) { i++; } else { if ((double.TryParse(strWinkel_b, out winkel_b) == false)) { GenerateErrorReport("Winkel Beta"); } }
string strWinkel_y = this.txtGamma.Text; if (strWinkel_y == string.Empty) { i++; } else { if ((double.TryParse(strWinkel_y, out winkel_y) == false)) { GenerateErrorReport("Winkel Gamma"); } }
//Height
string strHoehe_a = this.txt_ha.Text; if (strHoehe_a == string.Empty) { i++; } else { if ((double.TryParse(strHoehe_a, out hoehe_a) == false)) { GenerateErrorReport("Höhe a"); } }
string strHoehe_b = this.txt_hb.Text; if (strHoehe_b == string.Empty) { i++; } else { if ((double.TryParse(strHoehe_b, out hoehe_b) == false)) { GenerateErrorReport("Höhe b"); } }
string strHoehe_c = this.txt_hc.Text; if (strHoehe_c == string.Empty) { i++; } else { if ((double.TryParse(strHoehe_c, out hoehe_c) == false)) { GenerateErrorReport("Höhe c"); } }
//ErrorReport:
if (ErrorMessage != null)
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "ErrorAlert", "alert('Folgende Angaben sind fehlerhaft: " + ErrorMessage + "');", true);
return;
}
//Logic
//If more than 3 values....
string AlertText;
if (i < 6)
{
AlertText = "Es dürfen nur 3 Angaben gemacht werden!";
Page.ClientScript.RegisterStartupScript(this.GetType(), "ErrorAlert", "alert('" + AlertText + "');", true);
return;
}
if (i > 6)
{
AlertText = "Es müssen mindestens 3 Angaben gemacht werden!";
Page.ClientScript.RegisterStartupScript(this.GetType(), "ErrorAlert", "alert('" + AlertText + "');", true);
return;
}
}
protected void Berechnen()
{
//Höhensatz
if (seite_a != 0 && seite_b != 0 && seite_c != 0)
{
//Calculate missing angles
//Winkel Gamma
cos_y = (seite_a * seite_a) + (seite_b * seite_b) - (seite_c * seite_c); //Zähler berechnen
cos_y = cos_y / (2 * seite_a * seite_b); //Durch Nenner teilen
winkel_y = Math.Acos(cos_y); //Bogenradius berechnen
winkel_y = winkel_y * 180 / Math.PI; //In Winkel umrechnen
//Winkel Beta
cos_b = (seite_c * seite_c) + (seite_a * seite_a) - (seite_b * seite_b); //Zähler berechnen
cos_b = cos_b / (2 * seite_c * seite_a); //Durch Nenn teilen
winkel_b = Math.Acos(cos_b); //Bogenradius berechnen
winkel_b = winkel_b * 180 / Math.PI; //In Winkel umrechnen
//Winkel Alpha
double winkel_a = 180 - winkel_b - winkel_y;
//Werte eintragen
txtAlpha.Text = Convert.ToString(winkel_a);
txtBeta.Text = Convert.ToString(winkel_b);
txtGamma.Text = Convert.ToString(winkel_y);
//Flächen berechnen
//Mit Satz des Heron
Heron heron = new Heron(seite_a, seite_b, seite_c);
double FlaecheHeron = heron.Area;
Page.ClientScript.RegisterStartupScript(this.GetType(), "ErrorAlert", "alert('" + FlaecheHeron + "');", true);
}
}
The problem is, that there are so much possible combinations. if (seite_a != 0 && seite_b != 0 && seite_c != 0), etc, etc....
Upvotes: 2
Views: 1864
Reputation: 60858
You could write down all the rules you could possibly use to perform your computations. I guess you'd use angle sum, law of sines, law of cosines, and so on. Write each of these rules as a class, and create an instance for each possible labeling for each of these (i.e. whether you're using the law of cosines to compute γ or β). You should end up with a list of objects, all sharing a common interface, which you can use to compute some values from some others. You could then iterate over this list to see which rules apply, and use them to compute additional values. Once the count of missing values reaches zero, you are done.
If you iterate over all your rules without finding a rule to apply, then your set of possible rules isn't complete enough. I believe that it would be a very good idea to write a unit test which tries out all 220 possible ways to fill in three out of twelve inputs, to make sure that you have a sufficient set of rules to handle all of them. Simply start with a complete set of 12 values, compute all three-element subsets, feed them into your application code and compare the results against the expected values, taking numerical errors into account.
The above approach is neither optimized for performance nor for numeric stability. If either of these is an issue for you, a different approach might be better suited.
Upvotes: 2