Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Drawing equilateral triangles on the sides of another equilateral triangle

Triangel

I have been trying to draw equilateral triangles on the sides of a larger triangle. The first triangle is drawn by a separate method setting points A, B and C. So far I have just started with two sides, I am able to find the first two points of the smaller triangles, but I am unable to determine the correct formula for the third. I have tried refreshing my memory of trigonometry but I am at an impasse.

        float a =0;

        Point p = new Point(pnlDisplay.Width / 2 - (pnlDisplay.Width / 2) /3, 200);
        Triangle t = new Triangle(p, pnlDisplay.Width / 3, 0);
        drawEqTriangle(e, t);


        Point p1 = new Point();
        Point p2 = new Point();
        Point p3 = new Point();


        p1.X = Convert.ToInt32(A.X + t.size / 3);
        p1.Y = Convert.ToInt32(A.Y);

        p2.X = Convert.ToInt32(A.X + (t.size - t.size / 3));
        p2.Y = Convert.ToInt32(A.Y);
        //////////////////////////////
        p3.X = Convert.ToInt32((A.X - t.size / 3) * Math.Sin(a));
        p3.Y = Convert.ToInt32((A.Y - t.size / 3) * Math.Cos(a));
        drawTriangle(e, p1, p2, p3);


        p1.X = Convert.ToInt32((B.X - t.size / 3 * Math.Cos(t.angle + Math.PI / 3)));
        p1.Y = Convert.ToInt32((B.Y + t.size / 3 * Math.Sin(t.angle+ Math.PI / 3)));

        p2.X = Convert.ToInt32((B.X - (t.size - t.size / 3) * Math.Cos(t.angle + Math.PI / 3)));
        p2.Y = Convert.ToInt32((B.Y + (t.size - t.size / 3) * Math.Sin(t.angle + Math.PI / 3)));
        //////////////////////////////
        p3.X = Convert.ToInt32((B.X - t.size / 3) * Math.Cos(a));
        p3.Y = Convert.ToInt32((B.Y - t.size / 3) * Math.Tan(a));
        drawTriangle(e, p1, p2, p3);

This may be a question for the math subsection, but I thought I would try here first. What I need is the formula for p3.X and p3.Y

Any help would be greatly appreciated.

EDIT: changing "a" to float a = Convert.ToSingle( 60 * Math.PI / 180);

results in this: Edited

FINAL EDIT: Using MBo's answer: enter image description here

like image 304
Protium Avatar asked Oct 29 '22 14:10

Protium


1 Answers

Let's build universal formulas for any triangle orientation (note that it is worth to use A[] array for big triangle instead of explicit A,B,C vertices)

    p1.X = A.X * 2 / 3 + B.X / 3;
    p1.Y = A.Y * 2 / 3 + B.Y / 3;

    p2.X = A.X / 3 + B.X * 2 / 3;
    p2.Y = A.Y / 3 + B.Y * 2 / 3;

    D.X = (A.X - p1.X);
    D.Y = (A.Y - p1.Y);

    //note - angle sign depends on ABC orientation CW/CCW
    p3.X = p1.X + D.X * Cos(2*Pi/3) - D.Y * Sin(2*Pi/3)
    p3.Y = p1.Y + D.X * Sin(2*Pi/3) + D.Y * Cos(2*Pi/3)
like image 57
MBo Avatar answered Nov 12 '22 22:11

MBo