Search code examples
algorithmcoordinatescoordinate-systems

How can I solve for the points of an arbitrary trapezoid inscribed inside of a triangle?


Given the following coordinate points:

trapezoid

Write an algorithm (in pseudocode) to solve for x1, x2, x3, and x4.

This is what I have so far:

 var y1, y2, y3, y4 = 50, 50, 75, 75;

 var offset1 = tan(60) * y1;

 var offset2 = tan(60) * (y2 - y1);

 var x1 = 200 + offset1;

 var x3 = 200 + offset1 + offset2;

Solution

  • Consider the right triangle { (200,0) (350,300) (350,0) } (see the illustration below).

    Since the big and the small triangles sharing the (Xb, Yb) corner are similar, the ratio of the length of the red side (X,Y)-(Xb,Y) to the length of (Xb,Y)-(Xb,Yb) is the same as the ratio of (Xa,Ya)-(Xb,Ya) to (Xb,Ya)-(Xb,Yb).

    Illustration

    Since (X,Y)-(Xb,Y) is your only unknown, you could figure out the answer by solving the equation

    Xb-X   Xb-Xa
    ---- = -----
    Yb-Y   Yb-Ya
    

    Therefore,

             (Xb-Xa)*(Yb-Y)
    X = Xb - --------------
                  Yb-Ya