数学原理:
1、三角形的两边的和一定大于第三边 ,由此亦可证明得三角形的两边的差一定小于第三边.
2、c为最大值边,直角三角形:a^2+b^2=c^2 、钝角三角形:a^2+b^2<c^2 、锐角三角形:a^2+b^2>c^2
程序实现:
Triangle类:用于绘制三角形
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Drawing; namespace WindowsFormsApplication1 { class Triangle { PointF A, B, C; public Triangle(PointF a, PointF b, PointF c) { A = a; B = b; C = c; } /// <summary> /// 绘制三角形 /// </summary> /// <param name="g"></param> public void Draw(Graphics g) { Pen pen = new Pen(Color.Red, 1); g.DrawLine(pen, A, B); g.DrawLine(pen, B, C); g.DrawLine(pen, C, A); } /// <summary> /// 三角形旋转 /// </summary> /// <param name="degrees"></param> public void Rotate(int degrees) { float angle = (float)(degrees / 360f * Math.PI); float newX = (float)(A.X * Math.Cos(angle) - A.Y * Math.Sin(angle)); float newY = (float)(A.X * Math.Sin(angle) + A.Y * Math.Cos(angle)); A.X = newX; A.Y = newY; newX = (float)(B.X * Math.Cos(angle) - B.Y * Math.Sin(angle)); newY = (float)(B.X * Math.Sin(angle) + B.Y * Math.Cos(angle)); B.X = newX; B.Y = newY; newX = (float)(C.X * Math.Cos(angle) - C.Y * Math.Sin(angle)); newY = (float)(C.X * Math.Sin(angle) + C.Y * Math.Cos(angle)); C.X = newX; C.Y = newY; } } }
point类:定义坐标点的X,Y
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace WindowsFormsApplication1 { public class point { public double x, y; public point() { ; } public point(double x, double y) { this.x = x; this.y = y; } } }
judgeTri类:用于判断三角形形状
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace WindowsFormsApplication1 { public class judgeTri { public double a, b, c; public judgeTri() { ; } public judgeTri(double a, double b, double c) { this.a = a; this.b = b; this.c = c; } //三角形判断 public bool f1() { if (a + b > c || a + c > b || b + c > a) { return true; } else return false; } //判断值是否等边三角形 public bool f2() { if (f1() && a == b && b == c && a == c) return true; else return false; } //等腰三角形 public bool f3() { if (f1()) if (a == b || b == c || a == c) return true; else return false; else return false; } //直角三角形 public bool f4() { double max; max = Math.Max(a, b); max = Math.Max(max, c); if (max == a) { if (Math.Pow(max, 2) == Math.Pow(b, 2) + Math.Pow(c, 2)) return true; else return false; } else if (max == b) { if (Math.Pow(max, 2) == Math.Pow(a, 2) + Math.Pow(c, 2)) return true; else return false; } else if (max == c) { if (Math.Pow(max, 2) == Math.Pow(b, 2) + Math.Pow(a, 2)) return true; else return false; } else return false; } //钝角三角形 public bool f5() { double max; max = Math.Max(a, b); max = Math.Max(max, c); if (max == a) { if (Math.Pow(max, 2) > Math.Pow(b, 2) + Math.Pow(c, 2)) return true; else return false; } else if (max == b) { if (Math.Pow(max, 2) > Math.Pow(a, 2) + Math.Pow(c, 2)) return true; else return false; } else if (max == c) { if (Math.Pow(max, 2) > Math.Pow(b, 2) + Math.Pow(a, 2)) return true; else return false; } else return false; } //锐角三角形 public bool f6() { double max; max = Math.Max(a, b); max = Math.Max(max, c); if (max == a) { if (Math.Pow(max, 2) < Math.Pow(b, 2) + Math.Pow(c, 2)) return true; else return false; } else if (max == b) { if (Math.Pow(max, 2) < Math.Pow(a, 2) + Math.Pow(c, 2)) return true; else return false; } else if (max == c) { if (Math.Pow(max, 2) < Math.Pow(b, 2) + Math.Pow(a, 2)) return true; else return false; } else return false; } } }
Form:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { point A, B, C; judgeTri tri; Triangle t; string sMessage=""; public double angle_A, angle_B, angle_C; public Form1() { InitializeComponent(); } //求两点距离 public double pointLen(point m, point n) { return Math.Abs(Math.Sqrt(Math.Pow(m.x - n.x, 2) + Math.Pow(m.y - n.y, 2))); } //已知三点求角度 public double Angle(point cen, point first, point second) { const double M_PI = 3.1415926535897; double ma_x = first.x - cen.x; double ma_y = first.y - cen.y; double mb_x = second.x - cen.x; double mb_y = second.y - cen.y; double v1 = (ma_x * mb_x) + (ma_y * mb_y); double ma_val = Math.Sqrt(ma_x * ma_x + ma_y * ma_y); double mb_val = Math.Sqrt(mb_x * mb_x + mb_y * mb_y); double cosM = v1 / (ma_val * mb_val); double angleAMB = Math.Acos(cosM) * 180 / M_PI; return angleAMB; } private void button1_Click(object sender, EventArgs e) { sMessage = ""; A = new point(Convert.ToDouble(AX.Text.Trim()),Convert.ToDouble(AY.Text.Trim())); B = new point( Convert.ToDouble(BX.Text.Trim()),Convert.ToDouble(BY.Text.Trim())); C = new point(Convert.ToDouble(CX.Text.Trim()),Convert.ToDouble(CY.Text.Trim())); tri = new judgeTri(); tri.a = pointLen(B, C); tri.b = pointLen(A, C); tri.c = pointLen(A, B); //计算边长 sMessage += "边长:\na=" + tri.a + "\nb=" + tri.b + "\nc=" + tri.c+"\n"; Message.Text = sMessage; //判断形状 if (tri.f1()) { sMessage += "三角形\n"; Message.Text = sMessage; } if (tri.f2()) { sMessage += "等边三角形\n"; Message.Text = sMessage; } if (tri.f3()) { sMessage += "等腰三角形\n"; Message.Text = sMessage; } if (tri.f4()) { sMessage += "直角三角形\n"; Message.Text = sMessage; } if (tri.f5()) { sMessage += "钝角三角形\n"; Message.Text = sMessage; } if (tri.f6()) { sMessage += "锐角三角形\n"; Message.Text = sMessage; } //计算角度 sMessage += "角度:\n角A="+Angle(A,B,C)+"度\n角B="+Angle(B,A,C)+"度\n角C="+Angle(C,A,B)+"度\n"; Message.Text = sMessage; //绘制图形 PointF x = new PointF((float)A.x,(float)A.y); PointF y = new PointF((float)B.x, (float)B.y); PointF z = new PointF((float)C.x, (float)C.y); t = new Triangle(x, y, z); Graphics g = Draw.CreateGraphics(); g.Clear(Draw.BackColor); g.TranslateTransform(150, 150); //绘制标注 Graphics f = Draw.CreateGraphics(); Font font = new Font("微软雅黑", 7); //Point一样,只是值是浮点类型 PointF point = new PointF((float)A.x, (float)A.y); g.DrawString("A", font, Brushes.Green, point); point = new PointF((float)B.x, (float)B.y); g.DrawString("B", font, Brushes.Green, point); point = new PointF((float)C.x, (float)C.y); g.DrawString("C", font, Brushes.Green, point); t.Draw(g); } private void button2_Click(object sender, EventArgs e) { AX.Text = AY.Text=BX.Text=BY.Text=CX.Text=CY.Text=""; } private void Form1_Load(object sender, EventArgs e) { } } }
设计器代码:
namespace WindowsFormsApplication1 { partial class Form1 { /// <summary> /// 必需的设计器变量。 /// </summary> private System.ComponentModel.IContainer components = null; /// <summary> /// 清理所有正在使用的资源。 /// </summary> /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows 窗体设计器生成的代码 /// <summary> /// 设计器支持所需的方法 - 不要 /// 使用代码编辑器修改此方法的内容。 /// </summary> private void InitializeComponent() { this.label1 = new System.Windows.Forms.Label(); this.label2 = new System.Windows.Forms.Label(); this.label3 = new System.Windows.Forms.Label(); this.label4 = new System.Windows.Forms.Label(); this.label5 = new System.Windows.Forms.Label(); this.AX = new System.Windows.Forms.TextBox(); this.AY = new System.Windows.Forms.TextBox(); this.BX = new System.Windows.Forms.TextBox(); this.BY = new System.Windows.Forms.TextBox(); this.CX = new System.Windows.Forms.TextBox(); this.CY = new System.Windows.Forms.TextBox(); this.button1 = new System.Windows.Forms.Button(); this.button2 = new System.Windows.Forms.Button(); this.groupBox1 = new System.Windows.Forms.GroupBox(); this.groupBox2 = new System.Windows.Forms.GroupBox(); this.groupBox3 = new System.Windows.Forms.GroupBox(); this.Draw = new System.Windows.Forms.Panel(); this.Message = new System.Windows.Forms.RichTextBox(); this.groupBox1.SuspendLayout(); this.groupBox2.SuspendLayout(); this.groupBox3.SuspendLayout(); this.SuspendLayout(); // // label1 // this.label1.AutoSize = true; this.label1.Location = new System.Drawing.Point(62, 33); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(11, 12); this.label1.TabIndex = 1; this.label1.Text = "X"; // // label2 // this.label2.AutoSize = true; this.label2.Location = new System.Drawing.Point(145, 33); this.label2.Name = "label2"; this.label2.Size = new System.Drawing.Size(11, 12); this.label2.TabIndex = 2; this.label2.Text = "Y"; // // label3 // this.label3.AutoSize = true; this.label3.Location = new System.Drawing.Point(26, 58); this.label3.Name = "label3"; this.label3.Size = new System.Drawing.Size(23, 12); this.label3.TabIndex = 3; this.label3.Text = "点A"; // // label4 // this.label4.AutoSize = true; this.label4.Location = new System.Drawing.Point(26, 86); this.label4.Name = "label4"; this.label4.Size = new System.Drawing.Size(23, 12); this.label4.TabIndex = 4; this.label4.Text = "点B"; // // label5 // this.label5.AutoSize = true; this.label5.Location = new System.Drawing.Point(26, 113); this.label5.Name = "label5"; this.label5.Size = new System.Drawing.Size(23, 12); this.label5.TabIndex = 5; this.label5.Text = "点C"; // // AX // this.AX.Location = new System.Drawing.Point(64, 55); this.AX.Name = "AX"; this.AX.Size = new System.Drawing.Size(39, 21); this.AX.TabIndex = 6; // // AY // this.AY.Location = new System.Drawing.Point(128, 55); this.AY.Name = "AY"; this.AY.Size = new System.Drawing.Size(39, 21); this.AY.TabIndex = 7; // // BX // this.BX.Location = new System.Drawing.Point(64, 83); this.BX.Name = "BX"; this.BX.Size = new System.Drawing.Size(39, 21); this.BX.TabIndex = 8; // // BY // this.BY.Location = new System.Drawing.Point(128, 83); this.BY.Name = "BY"; this.BY.Size = new System.Drawing.Size(39, 21); this.BY.TabIndex = 9; // // CX // this.CX.Location = new System.Drawing.Point(64, 110); this.CX.Name = "CX"; this.CX.Size = new System.Drawing.Size(39, 21); this.CX.TabIndex = 10; // // CY // this.CY.Location = new System.Drawing.Point(128, 110); this.CY.Name = "CY"; this.CY.Size = new System.Drawing.Size(39, 21); this.CY.TabIndex = 11; // // button1 // this.button1.Location = new System.Drawing.Point(28, 150); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(53, 23); this.button1.TabIndex = 12; this.button1.Text = "确定(&o)"; this.button1.UseVisualStyleBackColor = true; this.button1.Click += new System.EventHandler(this.button1_Click); // // button2 // this.button2.Location = new System.Drawing.Point(115, 150); this.button2.Name = "button2"; this.button2.Size = new System.Drawing.Size(52, 23); this.button2.TabIndex = 13; this.button2.Text = "重置"; this.button2.UseVisualStyleBackColor = true; this.button2.Click += new System.EventHandler(this.button2_Click); // // groupBox1 // this.groupBox1.Controls.Add(this.AX); this.groupBox1.Controls.Add(this.button2); this.groupBox1.Controls.Add(this.label1); this.groupBox1.Controls.Add(this.button1); this.groupBox1.Controls.Add(this.label2); this.groupBox1.Controls.Add(this.CY); this.groupBox1.Controls.Add(this.label3); this.groupBox1.Controls.Add(this.CX); this.groupBox1.Controls.Add(this.label4); this.groupBox1.Controls.Add(this.BY); this.groupBox1.Controls.Add(this.label5); this.groupBox1.Controls.Add(this.BX); this.groupBox1.Controls.Add(this.AY); this.groupBox1.Location = new System.Drawing.Point(499, 13); this.groupBox1.Name = "groupBox1"; this.groupBox1.Size = new System.Drawing.Size(200, 212); this.groupBox1.TabIndex = 15; this.groupBox1.TabStop = false; this.groupBox1.Text = "编辑坐标点"; // // groupBox2 // this.groupBox2.Controls.Add(this.Message); this.groupBox2.Location = new System.Drawing.Point(499, 232); this.groupBox2.Name = "groupBox2"; this.groupBox2.Size = new System.Drawing.Size(200, 229); this.groupBox2.TabIndex = 16; this.groupBox2.TabStop = false; this.groupBox2.Text = "信息展示"; // // groupBox3 // this.groupBox3.Controls.Add(this.Draw); this.groupBox3.Location = new System.Drawing.Point(13, 13); this.groupBox3.Name = "groupBox3"; this.groupBox3.Size = new System.Drawing.Size(480, 448); this.groupBox3.TabIndex = 17; this.groupBox3.TabStop = false; this.groupBox3.Text = "绘制图形"; // // Draw // this.Draw.Location = new System.Drawing.Point(7, 21); this.Draw.Name = "Draw"; this.Draw.Size = new System.Drawing.Size(467, 412); this.Draw.TabIndex = 0; // // Message // this.Message.Location = new System.Drawing.Point(7, 21); this.Message.Name = "Message"; this.Message.Size = new System.Drawing.Size(187, 193); this.Message.TabIndex = 0; this.Message.Text = ""; // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(788, 517); this.Controls.Add(this.groupBox3); this.Controls.Add(this.groupBox2); this.Controls.Add(this.groupBox1); this.Name = "Form1"; this.Text = "Form1"; this.Load += new System.EventHandler(this.Form1_Load); this.groupBox1.ResumeLayout(false); this.groupBox1.PerformLayout(); this.groupBox2.ResumeLayout(false); this.groupBox3.ResumeLayout(false); this.ResumeLayout(false); } #endregion private System.Windows.Forms.Label label1; private System.Windows.Forms.Label label2; private System.Windows.Forms.Label label3; private System.Windows.Forms.Label label4; private System.Windows.Forms.Label label5; private System.Windows.Forms.TextBox AX; private System.Windows.Forms.TextBox AY; private System.Windows.Forms.TextBox BX; private System.Windows.Forms.TextBox BY; private System.Windows.Forms.TextBox CX; private System.Windows.Forms.TextBox CY; private System.Windows.Forms.Button button1; private System.Windows.Forms.Button button2; private System.Windows.Forms.GroupBox groupBox1; private System.Windows.Forms.GroupBox groupBox2; private System.Windows.Forms.GroupBox groupBox3; private System.Windows.Forms.Panel Draw; private System.Windows.Forms.RichTextBox Message; } }
运行结果: