Skip to content

Commit

Permalink
improve matrix and gauge
Browse files Browse the repository at this point in the history
  • Loading branch information
xianjimli committed Jan 30, 2025
1 parent 0378b44 commit 41e6b56
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 21 deletions.
3 changes: 3 additions & 0 deletions docs/changes.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# 最新动态

2025/01/30
* 修复matrix变换顺序后gauge指针旋转不正常(感谢兆坤提供补丁)

2025/01/28
* 补充注释(感谢兆坤提供补丁)
* 修复仿射变换顺序错误(感谢兆坤提供补丁)
Expand Down
6 changes: 3 additions & 3 deletions src/ext_widgets/gauge/gauge_pointer.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ static rect_t gauge_pointer_calc_dirty_rect(widget_t* widget, int32_t img_w, int
float_t anchor_y = tk_eval_ratio_or_px(gauge_pointer->anchor_y, widget->h);

matrix_init(&m);
matrix_translate(&m, ox, oy);
matrix_translate(&m, anchor_x, anchor_y);
matrix_rotate(&m, rotation);
matrix_translate(&m, -anchor_x, -anchor_y);
matrix_rotate(&m, rotation);
matrix_translate(&m, anchor_x, anchor_y);
matrix_translate(&m, ox, oy);

matrix_transform_point(&m, 0, 0, &(x), &(y));
min_x = x;
Expand Down
45 changes: 27 additions & 18 deletions src/tkc/matrix.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* Copyright (c) 2018 - 2025 Guangzhou ZHIYUAN Electronics Co.,Ltd.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; withm even the implied warranty of
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* License file for more details.
*
Expand All @@ -30,20 +30,12 @@ matrix_t* matrix_init(matrix_t* m) {
}

matrix_t* matrix_identity(matrix_t* m) {
return_value_if_fail(m != NULL, NULL);
/**
* ⌈1 0 0⌉
* |0 1 0|
* ⌊0 0 1⌋
*/
m->a0 = 1;
m->a1 = 0;
m->a2 = 0;
m->a3 = 1;
m->a4 = 0;
m->a5 = 0;

return m;
return matrix_set(m, 1, 0, 0, 1, 0, 0);
}

static inline double matrix_compute_determinant(matrix_t* m) {
Expand All @@ -63,6 +55,8 @@ static inline double matrix_compute_determinant(matrix_t* m) {
}

static inline bool_t matrix_is_invertible_ex(matrix_t* m, float* det) {
return_value_if_fail(m != NULL, FALSE);

double det_d = matrix_compute_determinant(m);

if (det != NULL) {
Expand All @@ -77,6 +71,8 @@ bool_t matrix_is_invertible(matrix_t* m) {
}

matrix_t* matrix_invert(matrix_t* m) {
return_value_if_fail(m != NULL, NULL);

float aa = m->a0, ab = m->a1, ac = m->a2, ad = m->a3, atx = m->a4, aty = m->a5;
float det = 0;
if (!matrix_is_invertible_ex(m, &det)) {
Expand Down Expand Up @@ -128,6 +124,8 @@ matrix_t* matrix_invert(matrix_t* m) {
}

matrix_t* matrix_set(matrix_t* m, float a0, float a1, float a2, float a3, float a4, float a5) {
return_value_if_fail(m != NULL, NULL);

m->a0 = a0;
m->a1 = a1;
m->a2 = a2;
Expand All @@ -139,6 +137,8 @@ matrix_t* matrix_set(matrix_t* m, float a0, float a1, float a2, float a3, float
}

matrix_t* matrix_multiply(matrix_t* m, matrix_t* b) {
return_value_if_fail(m != NULL && b != NULL, NULL);

/**
* ⌈a0 a2 a4⌉ ⌈b0 b2 b4⌉ ⌈a0*b0+a2*b1+a4*0 a0*b2+a2*b3+a4*0 a0*b4+a2*b5+a4*1⌉ ⌈a0*b0+a2*b1 a0*b2+a2*b3 a0*b4+a2*b5+a4⌉
* |a1 a3 a5| |b1 b3 b5| = |a1*b0+a3*b1+a5*0 a1*b2+a3*b3+a5*0 a1*b4+a3*b5+a5*1| = |a1*b0+a3*b1 a1*b2+a3*b3 a1*b4+a3*b5+a5|
Expand All @@ -158,6 +158,8 @@ matrix_t* matrix_multiply(matrix_t* m, matrix_t* b) {
}

matrix_t* matrix_translate(matrix_t* m, xy_t x, xy_t y) {
return_value_if_fail(m != NULL, NULL);

/**
* ⌈1 0 x⌉
* |0 1 y|
Expand All @@ -172,6 +174,8 @@ matrix_t* matrix_translate(matrix_t* m, xy_t x, xy_t y) {
}

matrix_t* matrix_scale(matrix_t* m, float sx, float sy) {
return_value_if_fail(m != NULL, NULL);

/**
* ⌈Sx 0 0⌉
* | 0 Sy 0|
Expand All @@ -186,6 +190,8 @@ matrix_t* matrix_scale(matrix_t* m, float sx, float sy) {
}

matrix_t* matrix_rotate(matrix_t* m, float rad) {
return_value_if_fail(m != NULL, NULL);

/**
* ⌈cos -sin 0⌉
* |sin cos 0|
Expand All @@ -201,27 +207,30 @@ matrix_t* matrix_rotate(matrix_t* m, float rad) {
}

matrix_t* matrix_transform_point(matrix_t* m, xy_t x, xy_t y, xy_t* ox, xy_t* oy) {
float x1 = m->a0 * x + m->a2 * y + m->a4;
float y1 = m->a1 * x + m->a3 * y + m->a5;
return_value_if_fail(m != NULL, NULL);

float x1 = 0, y1 = 0;

matrix_transform_pointf(m, x, y, &x1, &y1);

*ox = x1 > 0 ? x1 + 0.5f : x1 - 0.5f;
*oy = y1 > 0 ? y1 + 0.5f : y1 - 0.5f;
*ox = tk_roundi(x1);
*oy = tk_roundi(y1);

return m;
}

matrix_t* matrix_transform_pointf(matrix_t* m, float x, float y, float* ox, float* oy) {
return_value_if_fail(m != NULL, NULL);

/**
* 齐次坐标点(x, y, w=1)
*
* ⌈a0 a2 a4⌉ ⌈x⌉ ⌈a0*x+a2*y+a4*1⌉ ⌈a0*x+a2*y+a4⌉
* |a1 a3 a5| |y| = |a1*x+a3*y+a5*1| = |a1*x+a3*y+a5|
* ⌊ 0 0 1⌋ ⌊1⌋ ⌊ 0*x+ 0*y+ 1*1⌋ ⌊ 1⌋
*/
float x1 = m->a0 * x + m->a2 * y + m->a4;
float y1 = m->a1 * x + m->a3 * y + m->a5;
*ox = x1;
*oy = y1;
*ox = m->a0 * x + m->a2 * y + m->a4;
*oy = m->a1 * x + m->a3 * y + m->a5;

return m;
}

0 comments on commit 41e6b56

Please sign in to comment.