给用户输入绘制三角形

时间:2015-02-27 05:41:45

标签: c# trigonometry system.drawing

所以我试图制作一个程序,根据一些用户输入绘制一个三角形。用户提供的变量是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;

这将返回paint方法用于绘制三角形的三个点。但是,当我插入值时,它绘制的三角形看起来与我用用户输入描述的三角形不同。对此为何的任何想法?提前谢谢。

P.S。错误不在我的代码中,因为它没有给我任何错误,也没有崩溃。这是我无法找到的严格的数学错误。

2 个答案:

答案 0 :(得分:0)

我想象三角形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的负值,但不要担心,无论如何都要继续!

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

水平偏移有点复杂!如果xB为负数,我会在所有x值上添加一个偏移量,使xB为0,这会将三角形定位在画布的左侧,其宽度由新的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;
    }

答案 1 :(得分:0)

给定三角形的三个边 a b c 顶点的坐标为

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]

pic