## C# FORM APPLICATION - CALCULATING RESULTANT FORCE

In this post, I will create a form application to calculate the resultant force of given forces. Based on that application, magnitude and the angle of the resultant force can be calculated. Form design will look like in figure below.

Magnitude | Angle | |

F1 | txtMagnitude1 | txtAngle1 |

F2 | txtMagnitude2 | txtAngle2 |

F3 | txtMagnitude3 | txtAngle3 |

F4 | txtMagnitude4 | txtAngle4 |

btnCalculate | ||

Result | txtResultantMagnitude | txtResultantAngle |

__Note:____ __Textboxes and button are named as above. Labels' name does not matter.

There are 4 forces. Their magnitudes(newton) and angles (degree) are given F1, F2, F3, F4. Magnitudes and angles (with respect to x‐axis) of the four forces (F1, F2, F3, and F4) should be already entered to the textbox fields in order to calculate. By clicking the Calculate button, magnitude and angle of the resultant force will be written to the two textboxes at the bottom of the form. Program will calculate the magnitude and angle.

When any of the textboxes of those four forces is clicked, the textbox must be cleared automatically. To do it, we have to use the MouseClick event of a textbox.

private double m1, m2, m3, m4, a1, a2, a3, a4; private double m1x, m1y, m2x, m2y, m3x, m3y, m4x, m4y; private double rm=0, rmx=0, rmy=0, ra=0;

We use these variables as global in the class. "m1, m2, m3, m4" variables represent magnitudes (newton) which are comes from textboxes. "a1, a2, a3, a4" variables represent angles (degree) which are comes from textboxes. "m1x, m2x, m3x, m4x" and "m1y, m2y, m3y, m4y" variables are used for calculating cosine and sine respectively. "rmx, rmy" variables are used for calculating resultant magnitude. "rm" is resultant magnitude. Finally, "ra" is resultant angle.

In this application, three custom and functions are created. Functions as follows:

private double GetExactAngle(double Angle) { double a = 0; if (Angle > 360) a = Angle % 360; else if (Angle < 0) { if (Angle < -360) { a = Angle % 360; a += 360; } else a = 360 + Angle; } else { if (a <= 90 && a >= 0) a = Angle; else if (a <= 180 && a > 90) a = Angle - 90; else if (a <= 270 && a > 180) a = Angle - 180; else if (a <= 360 && a > 270) a = 360 - Angle; } return a; }

"GetExactAngle" function converts angle to exact degree. For example, if the degree is 540, function returns 180. Another example, if the function is -120, function returns 240.

private double ConvertToDegree(double Degree) { return Degree * Math.PI / 180; } private double ConvertToRadian(double Radian) { return Radian * 180 / Math.PI; }

ConvertToDegree and ConvertToRadian functions are used for angle conversion. It is just a standard nathematical calculation.

private void btnCalculate_Click(object sender, EventArgs e) { if (txtMagnitude1.Text != "" || txtMagnitude2.Text != "" || txtMagnitude3.Text != "" || txtMagnitude4.Text != "" || txtAngle1.Text != "" || txtAngle2.Text != "" || txtAngle3.Text != "" || txtAngle4.Text != "") { MessageBox.Show("Fields cannot be left blank."); return; } m1 = Convert.ToDouble(txtMagnitude1.Text); a1 = GetExactAngle(Convert.ToDouble(txtAngle1.Text)); m1x = Math.Cos(ConvertToDegree(a1)) * m1; m1y = Math.Sin(ConvertToDegree(a1)) * m1; m2 = Convert.ToDouble(txtMagnitude2.Text); a2 = GetExactAngle(Convert.ToDouble(txtAngle2.Text)); m2x = Math.Cos(ConvertToDegree(a2)) * m2; m2y = Math.Sin(ConvertToDegree(a2)) * m2; m3 = Convert.ToDouble(txtMagnitude3.Text); a3 = GetExactAngle(Convert.ToDouble(txtAngle3.Text)); m3x = Math.Cos(ConvertToDegree(a3)) * m3; m3y = Math.Sin(ConvertToDegree(a3)) * m3; m4 = Convert.ToDouble(txtMagnitude4.Text); a4 = GetExactAngle(Convert.ToDouble(txtAngle4.Text)); m4x = Math.Cos(ConvertToDegree(a4)) * m4; m4y = Math.Sin(ConvertToDegree(a4)) * m4; rmx = m1x + m2x + m3x + m4x; rmy = m1y + m2y + m3y + m4y; rm = Math.Sqrt(Math.Pow(rmx, 2) + Math.Pow(rmy, 2)); txtResultantMagnitude.Text = rm.ToString(); //Resultant Angle Calculation double tangent = Math.Tan(ConvertToDegree(rmy) / ConvertToDegree(rmx)); ra = Math.Atan(tangent); ra = ConvertToRadian(ra); txtResultantAngle.Text = ra.ToString(); }

Finally, btnCalculate_Click event handler is created to perform main process. As I told before, "m1, m2, m3, m4" variables represents magnitudes. "a1, a2, a3, a4" variables represents angles. According to the code, x‐axis and y‐axis of the magnitudes are calculates first. Then "rmx, rmy" variables store the overall of x and y values respectively. Lastly, rm is calculated using rmx and rmy. Calculation exactly like this "rm^{2} = rmx^{2} + rmy^{2}". In the last section, resultant angle variable "ra" is calculated.

You can download the source code from here. If you have any question, you can leave a comment or you can send me a message from here.

## Comments (0)