C # implementation Triangle Area Calculator

Source: Internet
Author: User
Tags pow trim

Objective

Last week, human-Computer Interaction class teacher assigned a homework, let us realize a triangle area calculator, have the kind of interface, programming language is not limited, and disclosed that use C # to achieve more convenient. Since has not studied C # before, holding a try attitude began the "Hello World" tour of C #.

WinForm or WPF?

VS2015 is ready to write C #, when the construction of the project did not know what to build C # works well, online check, there are basically two choices, WinForm and WPF, a little understanding of the difference between the two, WPF later than the WinForm, more mature than the processing of the WinForm thread, At the same time, the ability to draw is greatly improved. The answer seems obvious, I originally used WPF, but when the last step to draw a triangle, because there is no corresponding control, forcing me to turn to the WinForm, there may be another solution, for the time being not to study.

Interface design & Add events

Looking at Microsoft's C # tutorial, I started writing code. Really simple, the control is directly from the toolbox dragged into the page, and then in the property window can be set up a variety of properties, compared to the previous semester to learn the Java GUI design, it is too convenient, greatly improved the development efficiency.

To drag a few simple controls and design a layout, the next thing to do is to add events to the control. Three textbox for input I have added the TextChanged event to determine whether the user's current input is legitimate, and so on, the button control adds the Click event, and after clicking, it calculates the area of the triangle and draws the approximate triangle in the control PictureBox.

About drawing triangles this piece, but wasted me a lot of time. The first is System.Drawing this namespace can not find, engaged in a half-day found to add their own references, there is a shift from WPF to WinForm reasons, WPF did not find a suitable place to store the drawn triangle control, but found WinForm under the PictureBox control to solve this problem, so Turned to the WinForm. In addition, the function that draws the triangle usually needs to know the coordinates of the point, and we give the edge length, here I use the fixed longest edge at a certain horizontal line method, calculate three points of coordinates, and then draw.

The implementation of the results are as follows:
input side long illegal situation
Where the input edge length does not constitute a triangle
Calculate the area of the case

Partial event code:

The code is as follows Copy Code

Using System;
Using System.Drawing;
Using System.Windows.Forms;

Namespace Triangle_area
{
public partial class Title:form
{
Public title ()
{
InitializeComponent ();
}

        private void Button_Click (object sender, EventArgs e)
  & nbsp;     {
            if (!string . IsNullOrEmpty (Len_a.Text.Trim ()) &&!string. IsNullOrEmpty (Len_b.Text.Trim ()) &&!string. IsNullOrEmpty (Len_c.Text.Trim ())
            {
                Double A, B, C;
                a = double. Parse (Len_a.text);
                B = Double. Parse (Len_b.text);
                C = Double. Parse (len_c.text);

                if (A + B > C & & A + C > B && B + C > a)
            &N bsp;   {
                     Picturebox1.refresh ();
                     Double p = (A + B + c)/2;
                     double s = math.sqrt (P * (p-a) * (p-b) * (p-c));
                     result. Text = "Triangle area is:" + s.tostring ("F3");

Double Max_len = Math.max (Math.max (A, B), c);
Double Min_len = Math.min (Math.min (A, B), c);
Double Mid_len = a + B + c-max_len-min_len;
Double Cos_theda = (Math.pow (Max_len, 2) + Math.pow (Min_len, 2)-Math.pow (Mid_len, 2))/(2*max_len*min_len);
Double Sin_theda = math.sqrt (1-math.pow (Cos_theda, 2));
Double h = 2 * s/max_len;
Double pos = Min_len * COS_THEDA;

Double factor = 300/max_len;
Double Left_margin = 200-MAX_LEN/2 * factor;
Double Right_margin = + MAX_LEN/2 * FACTOR;

Graphics g = picturebox1.creategraphics ();
SolidBrush Bluebrush = new SolidBrush (Color.Blue);

Point point1 = new Point ((int) left_margin, 270);
Point point2 = new Point ((int) (right_margin), 270);
Point point3 = new Point ((int) (Left_margin + pos*factor), (int) (270-H * factor));
Point[] curvepoints = {point1, point2, point3};

G.fillpolygon (Bluebrush, curvepoints);


}
Else
{
Result. Text = "The side length of the input is not legal!" ";
}
}
Else
{
Result. Text = "Please enter the triangle three sides long!" ";
}
}

private void Len_a_textchanged (object sender, EventArgs e)
{
Result. Text = "";
System.Text.RegularExpressions.Regex rex = new System.Text.RegularExpressions.Regex (@ "^[+]?\d*[.]? \d+$ ");
if (!rex. IsMatch (Len_a.text))
{
Result. Text = "Please enter a number!" ";
}
else if (!string. IsNullOrEmpty (Len_a.Text.Trim ()) &&!string. IsNullOrEmpty (Len_b.Text.Trim ()) &&!string. IsNullOrEmpty (Len_c.Text.Trim ()))
{
Double A, B, C;
A = double. Parse (Len_a.text);
b = Double. Parse (Len_b.text);
c = Double. Parse (Len_c.text);

if (A + b <= C | | A + c <= B | | b + C <= a)
{
Result. Text = "The side length of the input is not legal!" ";
}
}
}

private void Len_b_textchanged (object sender, EventArgs e)
{
Result. Text = "";
System.Text.RegularExpressions.Regex rex = new System.Text.RegularExpressions.Regex (@ "^[+]?\d*[.]? \d+$ ");
if (!rex. IsMatch (Len_b.text))
{
Result. Text = "Please enter a number!" ";
}
else if (!string. IsNullOrEmpty (Len_a.Text.Trim ()) &&!string. IsNullOrEmpty (Len_b.Text.Trim ()) &&!string. IsNullOrEmpty (Len_c.Text.Trim ()))
{
Double A, B, C;
A = double. Parse (Len_a.text);
b = Double. Parse (Len_b.text);
c = Double. Parse (Len_c.text);

if (A + b <= C | | A + c <= B | | b + C <= a)
{
Result. Text = "The side length of the input is not legal!" ";
}
}
}

private void Len_c_textchanged (object sender, EventArgs e)
{
Result. Text = "";
System.Text.RegularExpressions.Regex rex = new System.Text.RegularExpressions.Regex (@ "^[+]?\d*[.]? \d+$ ");
if (!rex. IsMatch (Len_c.text))
{
Result. Text = "Please enter a number!" ";
}
else if (!string. IsNullOrEmpty (Len_a.Text.Trim ()) &&!string. IsNullOrEmpty (Len_b.Text.Trim ()) &&!string. IsNullOrEmpty (Len_c.Text.Trim ()))
{
Double A, B, C;
A = double. Parse (Len_a.text);
b = Double. Parse (Len_b.text);
c = Double. Parse (Len_c.text);

if (A + b <= C | | A + c <= B | | b + C <= a)
{
Result. Text = "The side length of the input is not legal!" ";
}
}
}
}
}


Finally, I'm going to have to spit it out. The C # code specification in VS, curly braces for lines or something.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.