【问题标题】:MouseDrag Triangle Logic鼠标拖动三角形逻辑
【发布时间】:2016-03-15 22:25:39
【问题描述】:

我正在尝试使用简单的 mousedown、drag 和 mouseup 创建一个三角形。我有一个逻辑草图,我用线条绘制,以进一步澄清和一些我一直在尝试的 sn-p,但它从来没有变成我试图在我的草图中绘制出来的方式。

我做错了什么吗,这不可能吗,除了点击每个顶点之外,还有其他解决方案吗?

private void Form1_MouseDown(object sender, MouseEventArgs e)
    {
     Vertex1 = e.Location; //First (left) Corner
    }

private void Form1_MouseUp(object sender, MouseEventArgs e)
    {
     Vertex2.X = e.X;         //This creates a straight base from Vertex 1 to Vertex 2
     Vertex2.Y = Vertex1.Y    //'''' ''''''' ' '''''''' ''''
     Vertex3.Y = e.Y; //The y is the same height the mouse was let go
     Vertex3.X = (Vertex1.X + Vertex2.X) / 2; //The x is half way from the two corners

     Point[] pts = new Point[3] { Vertex1, Vertex3, Vertex2};
     g.DrawPolygon(Pen1, pts);
     }

Vertex1 是第一个左角(鼠标按下)

Vertex2 是右角

Vertex3 是顶角

【问题讨论】:

    标签: c# winforms graphics


    【解决方案1】:

    这一行是错误的:

    Vertex3.X = (Vertex1.X - Vertex2.X) / 2; //The x is half way from the two corners
    

    应该是

    Vertex3.X = Vertex1.X + (Vertex2.X - Vertex1.X) / 2;
    

    【讨论】:

    • 啊,数学错误。这在制作直立或倒立的三角形时效果惊人,但对于指向左或右的三角形,我还能做些什么吗?
    • 当然,只是数学略有不同。 Vertex2 将是 (Vertex1.X, e.Y) - 顶部/左侧,而 Vertex3 可能是 (e.X, [Vertex1.Y 和 e.Y 之间的中间])。
    • 想想由两个鼠标事件点描述的矩形,以及该矩形上的点在哪里描述了你想要的三角形。
    【解决方案2】:

    您描述的是“等腰三角形”而不是等边三角形。对于 Vertex3.X,尝试添加并除以 2 以获得“平均值”。

    Vertex3.X = (LeftCorner.X + RightCorner.X) / 2; //x是两个角的一半

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-25
      • 1970-01-01
      相关资源
      最近更新 更多