.net/vb.net根据三边绘制三角形(余弦定理应用)

在高中阶段,我们在解三角形里面学过余弦定理,他的基本公式是:

 

得到的∠C是弧度制的角度

如果你只是想单纯的得到角度(非弧度制,即角度制)那么请看以下代码:

Dim la = CInt(T1.Text)
Dim lb = CInt(T2.Text)
Dim lc = CInt(T3.Text)
Dim a = Math.Acos((lb * lb + lc * lc - la * la) / (2 * lb * lc)) * (180 / Math.PI)
Dim b = Math.Acos((la * la + lc * lc - lb * lb) / (2 * la * lc)) * (180 / Math.PI)
Dim c = Math.Acos((la * la + lb * lb - lc * lc) / (2 * la * lb)) * (180 / Math.PI)

 如果不是,请往下看

于是就有以下代码: (使用反余弦求出余弦值所对应的角度弧度制))

Dim la = CInt(T1.Text)
Dim lb = CInt(T2.Text)
Dim lc = CInt(T3.Text)
Dim a = Math.Acos((lb * lb + lc * lc - la * la) / (2 * lb * lc))
Dim b = Math.Acos((la * la + lc * lc - lb * lb) / (2 * la * lc))
Dim c = Math.Acos((la * la + lb * lb - lc * lc) / (2 * la * lb))

然后将边长和角度做成数组

Dim l As Integer() = {la, lb, lc}
Dim ang As Double() = {a, b, c}

最后,给出2种画法:

Dim picw = l(2)
Dim pich = Math.Cos(Math.PI / 2 - ang(1)) * l(0)
Dim point2 = New Point(Math.Sin(Math.PI / 2 - ang(1)) * l(0), pich)
Dim bmp1 = New Bitmap(picw, CInt(pich))
Dim g = Graphics.FromImage(bmp1)
g.DrawPolygon(New Pen(Color.Red), {New Point(0, 0), New Point(picw, 0), point2})
Pic1.Image = bmp1

pich = Math.Cos(Math.PI / 2 - ang(0)) * l(1)
point2 = New Point(Math.Sin(Math.PI / 2 - ang(0)) * l(1), pich)
Dim bmp2 = New Bitmap(picw, CInt(pich))
Dim g2 = Graphics.FromImage(bmp2)
g2.DrawPolygon(New Pen(Color.Red), {New Point(0, 0), New Point(picw, 0), point2})
Pic2.Image = bmp2

效果:

 

猜你喜欢

转载自blog.csdn.net/weixin_56050945/article/details/129468286