Here's how I figured out my task, can someone come in handy.
To perform a reflection of a triangle with respect to an arbitrary line, you must perform specific actions:
- moving the line and triangle so that the line passes through the origin;
- rotation of the line and triangle around the point of origin of coordinates to coincide with the coordinate axis X;
- reflection relative to the coordinate axis;
- reverse rotation around the origin;
- move to the starting position.
In the matrix form, this transformation has the idea
[T] = [T'] [R] [R'] [R^(-1)] [T'^(-1)] , where T' is the displacement matrix, R is the rotation matrix around the origin, R ' - reflection matrix.
The equation ax + by + c = 0 can be reduced to the form:
by = -ax - c
y = -a / bx - c / b
From it we find two points of the line A , B :
Point A = new Point( -width, -(a / b) * -width - c / b ); Point B = new Point( width, -(a / b) * width - c / b );
Where a , b , c are coefficients read from fields on the form, width is the distance from the beginning of the canvas to the middle ( width = Canvas.Width / 2 ), where x = 0.
As a result, there is a line L passing through the entire coordinate system.
We write it in matrix form:
float[,] L = new float[2, 3] { { AX, AY, 1 }, { BX, BY, 1 } };
We also write the matrix X from the array of points of the triangle points :
float[,] X = new float[3, 3] { { points[0].X, points[0].Y, 1 }, { points[1].X, points[1].Y, 1 }, { points[2].X, points[2].Y, 1 } };
Now we make affine transformations by multiplying matrices.
For multiplication we will use the MultiplyMatrix method.
private static float[,] MultiplyMatrix(float[,] a, float[,] b) { float[,] product = new float[a.GetLength(0), b.GetLength(1)]; for (int row = 0; row product.GetLength(0); row++) for (int col = 0; col product.GetLength(1); col++) // Multiply the row of A by the column of B for (int inner = 0; inner a.GetLength(1); inner++) product[row, col] += a[row, inner] * b[inner, col]; return product; }
1) To move a line and a triangle so that the line passes through the origin, we find the position of y at x = 0 :
float ys = -(a / b) * 0 - c / b;
Move the line and triangle to the origin using the displacement matrix:
float[,] T = new float[3, 3] { { 1, 0, 0 }, { 0, 1, 0 }, { 0, -ys, 1 } }; L = MultiplyMatrix(L, T); X = MultiplyMatrix(X, T);
2) Rotate the line and triangle around the origin point until it coincides with the coordinate axis X To do this, we find at what angle is the line relative to the X axis:
double radians = Math.Atan(-(a / b));
And rotate using the rotation matrix around the origin:
float[,] R = new float[3, 3] { { Math.Cos(radians), -Math.Sin(radians), 0 }, { Math.Sin(radians), Math.Cos(radians), 0 }, { 0, 0, 1 } }; L = MultiplyMatrix(L, R); X = MultiplyMatrix(X, R);
3) Reflect the triangle relative to the coordinate axis using the reflection matrix:
float[,] Rr = new float[3, 3] { { 1, 0, 0 }, { 0, -1, 0 }, { 0, 0, 1 } }; X = MultiplyMatrix(X, Rr);
4) Make a reverse rotation around the origin:
float[,] Rh = new float[3, 3] { { Math.Cos(radians), Math.Sin(radians), 0 }, { -Math.Sin(radians), Math.Cos(radians), 0 }, { 0, 0, 1 } }; L = MultiplyMatrix(L, Rh); X = MultiplyMatrix(X, Rh);
5) Making the move to the starting position:
float[,] Th = new float[3, 3] { { 1, 0, 0 }, { 0, 1, 0 }, { 0, ys, 1 } }; L = MultiplyMatrix(L, Th); X = MultiplyMatrix(X, Th);
Assign new values ​​to the points array:
for (int i = 0; i p.Length; i++) { points[i].X = X[i, 0]; points[i].Y = X[i, 1]; }
We draw a triangle with new coordinates:
graph.FillPolygon(Brushes.RoyalBlue, points);

ax + by + c = 0. - VladD