Java – convert a triangle to another triangle

Hi, I'm trying to create an affine transformation, let me turn one triangle into another I have the coordinates of two triangles Can you help me?

According to Adam Rosenfeld's answer, I came up with this code to solve the equation by myself in case someone is bored:

public static AffineTransform createTransform(ThreePointSystem source,ThreePointSystem dest) {        
    double x11 = source.point1.getX();
    double x12 = source.point1.getY();
    double x21 = source.point2.getX();
    double x22 = source.point2.getY();
    double x31 = source.point3.getX();
    double x32 = source.point3.getY();
    double y11 = dest.point1.getX();
    double y12 = dest.point1.getY();
    double y21 = dest.point2.getX();
    double y22 = dest.point2.getY();
    double y31 = dest.point3.getX();
    double y32 = dest.point3.getY();

    double a1 = ((y11-y21)*(x12-x32)-(y11-y31)*(x12-x22))/
                ((x11-x21)*(x12-x32)-(x11-x31)*(x12-x22));
    double a2 = ((y11-y21)*(x11-x31)-(y11-y31)*(x11-x21))/
                ((x12-x22)*(x11-x31)-(x12-x32)*(x11-x21));
    double a3 = y11-a1*x11-a2*x12;
    double a4 = ((y12-y22)*(x12-x32)-(y12-y32)*(x12-x22))/
                ((x11-x21)*(x12-x32)-(x11-x31)*(x12-x22));
    double a5 = ((y12-y22)*(x11-x31)-(y12-y32)*(x11-x21))/
                ((x12-x22)*(x11-x31)-(x12-x32)*(x11-x21));
    double a6 = y12-a4*x11-a5*x12;
    return new AffineTransform(a1,a4,a2,a5,a3,a6);
}

Solution

I'll assume you're talking about 2D here There are 9 values in the affine transformation matrix:

    | a1 a2 a3 |
A = | a4 a5 a6 |
    | a7 a8 a9 |

There are three input vertices x1, X2 and X3, which should become Y1, Y2 and Y3 after transformation However, since we are using homogeneous coordinates, applying a to X1 does not necessarily give Y1 – it gives a multiple of Y1 Therefore, we also have unknown multipliers K1, K2 and K3, using the equation:

A*x1 = k1*y1
A*x2 = k2*y2
A*x3 = k3*y3

Each is a vector, so we really have 9 equations out of 12 unknowns, so the solution will be unconstrained If we require A7 = 0, a8 = 0 and A9 = 1, the solution will be unique (this choice is natural, because it means that if the input point is (x, y, 1), the output point will always have uniform coordinate 1, so the resulting transformation is only a 2 × 2 transformation plus one transformation)

Therefore, this reduces the equation to:

a1*x11 + a2*x12 + a3 = k1*y11
a4*x11 + a5*x12 + a6 = k1*y12
                   1 = k1
a1*x21 + a2*x22 + a3 = k2*y21
a4*x21 + a5*x22 + a6 = k2*y22
                   1 = k2
a1*x31 + a2*x32 + a3 = k3*y31
a4*x31 + a5*x32 + a6 = k3*y32
                   1 = k3

Therefore, K1 = K2 = K3 = 1 insert these and convert them into matrix form to obtain:

| x11 x12   1   0   0   0 |   | a1 |   | y11 |
| x21 x22   1   0   0   0 |   | a2 |   | y21 |
| x31 x32   1   0   0   0 | * | a3 | = | y31 |
|   0   0   0 x11 x12   1 |   | a4 |   | y12 |
|   0   0   0 x21 x22   1 |   | a5 |   | y22 |
|   0   0   0 x31 x32   1 |   | a6 |   | y32 |

Solve this 6 × 6 the equations will produce your affine transformation matrix A. it will have a unique solution if and only if the 3 points of the source triangle are not collinear

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>