-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtranslation_matrix.c
90 lines (72 loc) · 2.09 KB
/
translation_matrix.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <stdio.h>
#include <stdlib.h>
#include "utils.h"
#define SIZE 3
double **createTransMatrix(double tx, double ty)
{
// Allocate memory for matrix
double **transMatrix = allocateMatrix(SIZE, SIZE);
// Fill matrix
for (int i = 0; i < SIZE; i++)
{
for (int j = 0; j < SIZE; j++)
{
if (i == j)
{
transMatrix[i][j] = 1;
}
else
{
transMatrix[i][j] = 0;
}
}
}
transMatrix[0][2] = tx;
transMatrix[1][2] = ty;
return transMatrix;
}
// Function to translate points using the translation matrix
double **translateMatrix(double **points, size_t rows, size_t cols, double tx, double ty)
{
double **transMatrix = createTransMatrix(tx, ty);
double **resultPoints = allocateMatrix(rows, cols); // Resulting points
for (int i = 0; i < rows; i++)
{
double tempPoints[SIZE];
tempPoints[0] = points[i][0];
tempPoints[1] = points[i][1];
tempPoints[2] = 1;
double tempDot[SIZE];
matrixVectorDotProduct(transMatrix, SIZE, SIZE, tempPoints, SIZE, tempDot);
resultPoints[i][0] = tempDot[0]; // x'
resultPoints[i][1] = tempDot[1]; // y'
}
// Free translation matrix memory
freeMatrix(transMatrix, SIZE);
return resultPoints;
}
int main()
{
double tx = 2, ty = 3;
// Init points
size_t rows = 3, cols = 2;
double init_points[] = {0, 0, 1, 0, 0.5, 1};
double **points = allocateMatrix(rows, cols);
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
points[i][j] = init_points[i * cols + j];
}
}
printf("Matrix points with %zu rows and %zu cols.\n", rows, cols);
printMatrix(points, rows, cols, 1);
// Translate matrix
double **result = translateMatrix(points, rows, cols, tx, ty);
printf("Matrix result with %zu rows and %zu cols.\n", rows, cols);
printMatrix(result, rows, cols, 2);
// Free memory
freeMatrix(points, rows);
freeMatrix(result, rows);
return 0;
}