根据用户输入绘制三角形

Drawing triangles given user input

所以我正在尝试制作一个程序,在给定一些用户输入的情况下绘制一个三角形。用户提供的变量是 angleA、angleB 和 leC,以及相应的边。我设置的寻找角度的三个点的代码如下。

double angle_A = double.Parse(angleA.Text);
double angle_B = double.Parse(angleB.Text);
double angle_C = double.Parse(angleC.Text);
double side_A = double.Parse(sideA.Text);
double side_B = double.Parse(sideB.Text);
double side_C = double.Parse(sideC.Text);

double triangleHeight = Area * 2 / (double.Parse(sideB.Text));
double height = canvas.Height;
double width = canvas.Width;

int aX, aY, bX, bY, cX, cY; 

aY = Convert.ToInt32(canvas.Height - triangleHeight / 2);

if (angle_A <= 90 && angle_C <= 90)
{
    aX = Convert.ToInt32((width - side_B) / 2);
}
else if (angle_A > 90)
{
    double extraLength = Math.Sqrt(Math.Pow(side_C, 2) - Math.Pow(triangleHeight, 2));
    aX = Convert.ToInt32(width - ((width - (side_B + extraLength)) / 2) + side_B);
}
else if (angle_C > 90)
{
    double extraLength = Math.Sqrt(Math.Pow(side_A, 2) - Math.Pow(triangleHeight, 2));
    aX = Convert.ToInt32((width - side_B + extraLength) / 2);
}
else
{
    aX = 0;
    MessageBox.Show("ERROR: No such triangle exists", "ERROR:");
}

cX = aX + Convert.ToInt32(side_B);
cY = aY;

bX = Convert.ToInt32(side_A * Math.Cos(Math.PI * angle_C / 180) + cX);
bY = Convert.ToInt32(side_A * Math.Sin(Math.PI * angle_C / 180) - cY);

Point pointA = new Point(aX, aY);
Point pointB = new Point(bX, bY);
Point pointC = new Point(cX, cY);
Point[] points = new Point[3] { pointA, pointB, pointC };

return points;

这returns paint 方法应该用来绘制三角形的三个点。但是,当我插入这些值时,它绘制的三角形看起来与我在用户输入中描述的三角形完全不同。关于为什么会这样的任何想法?提前致谢。

P.S。该错误不在我的代码中,因为它没有给我任何错误并且不会崩溃。这完全是一个数学错误,我一直没能找到。

我想像三角形 ABC 沿基线有两个角 A 和 C,A 在左边,C 在右边,B 在它们上方的某处。 A边就是A角的对面,以此类推

正如Damien_the_Unbeliever所说,您应该只允许输入B边、C边和角A的角度。验证A不超过180度。从原点 A 开始,所以我们立即知道 xA = 0、yA = 0、xC = B 边的长度、yC=0、xB = C 边 * cos A 和 yB = C 边 * sin A。我相信即使 A 超过 90 度也能工作,xB 确实会得到负值,但别担心,继续!

现在您所要做的就是将三角形置于 canvas 的中心。我不明白你从哪里得到 Area 。从三角形的面积计算三角形的高度是没有意义的。三角形的高度是 yB,只要 yB <= 高度,您就可以计算出垂直居中所需的偏移量。向所有点添加相同的 y 偏移量。

水平偏移有点复杂!如果 xB 为负,我会为所有 x 值添加一个偏移量以使 xB 变为 0,这会将您的三角形定位在 canvas 的左侧,其宽度由新的 xC 给出。如果 xB 为非负数,则宽度为 xC 或 xB 的最大值。然后你可以根据你知道的宽度计算x偏移量。

我有时间做一些代码,例如值;这将绘制一个三角形但尚未居中:

int sideB = 100;
int sideC = 143;
int angleA = 28;
double angleARadians = Math.PI * angleA / 180.0;

int[] xs = new int[3];
int[] ys = new int[3];

//1st corner is at the origin
xs[0] = 0; ys[0] = 0;

//Then the third corner is along the x axis from there to the length of side B

xs[2] = sideB; ys[2] = 0;

// The second corner is up a bit above the x axis.  x could be negative.

// 注意,你画的时候,y轴是向下延伸的,所以会在x轴下方画一个正的y值。

    xs[1] = (int)Math.Round(sideC * Math.Cos(angleARadians));
    ys[1] = (int)Math.Round(sideC * Math.Sin(angleARadians));

 //If Bx is negative, move all the points over until it's 0

    if (xs[1] < 0)
    {
        int zeroX = xs[1] * -1;
        for (int i = 0; i < 3; i++)
        {
            xs[i] += zeroX;
        }
    }

    // Now centre the triangle on the canvas.
    // Firstly find the width of the triangle.  Point B could be to the left of A, or between A and C, or to the right of C.
    // So the left most point of the triangle is the minimum of A or B, and the right most point is the maximum of B or C.
    int minX = Math.Min(xs[0],xs[1]);
    int maxX = Math.Max(xs[2], xs[1]);

    //The height of the triangle is yB.

    int offsetX = (panCanvas.Width - (maxX - minX)) / 2;
    int offsetY = (panCanvas.Height - ys[1]) / 2;

    //offset all the points by the same amount, to centre the triangle.
    for (int i = 0; i < 3; i++)
    {
        xs[i] += offsetX;
        ys[i] += offsetY;
    }

给定三角形的三边abc的坐标的顶点是

P=[0,0]
Q=[a,0]
R=[(a^2+c^2-b^2)/(2*a), sqrt(c^2*(2*(a^2+b^2)-c^2)-(a+b)^2*(a-b)^2)/(4*a^2))]

例如,a=6,b=4 和 c=8

P=[0,0]
Q=[6,0]
R=[7,√15]