Alex Diamond
Alex Diamond

Reputation: 586

MouseDrag Triangle Logic

I'm trying to create a triangle with a simple mousedown, drag, and mouseup. I have a sketch of the logic, that I drew with lines, for further clarification and some snippet that I've been trying but, it never turns out the way I've tried to plot it out in my sketch.

Is there something I'm doing incorrect, is it not possible, and is there another solution besides clicking each vertex?

Sketch

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 is the first left corner (mousedown)

Vertex2 is the right corner

Vertex3 is the top corner

Upvotes: 1

Views: 76

Answers (2)

Steven Cvetko
Steven Cvetko

Reputation: 11

You describe an "isosceles triangle" instead of equilateral. For Vertex3.X try adding and dividing by 2 to get the "average".

Vertex3.X = (LeftCorner.X + RightCorner.X) / 2; //The x is half way from the two corners

Upvotes: 1

Blorgbeard
Blorgbeard

Reputation: 103437

This line is wrong:

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

It should be

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

Upvotes: 2

Related Questions