Skip to content

Commit 465635b

Browse files
committed
Add twin_sincos function to refactor sin/cos usage
As several applications need the sine and cosine of the same angle, I replace all occurrences of `twin_sin` and `twin_cos` with the shortcut `twin_sincos`, including the code in the files `src/path.c` and `src/matrix.c`.
1 parent 30d4869 commit 465635b

File tree

4 files changed

+23
-6
lines changed

4 files changed

+23
-6
lines changed

include/twin.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -736,6 +736,8 @@ void twin_path_rmove(twin_path_t *path, twin_fixed_t x, twin_fixed_t y);
736736

737737
void twin_path_draw(twin_path_t *path, twin_fixed_t x, twin_fixed_t y);
738738

739+
void twin_path_draw_polar(twin_path_t *path, twin_angle_t deg);
740+
739741
void twin_path_rdraw(twin_path_t *path, twin_fixed_t x, twin_fixed_t y);
740742

741743
void twin_path_circle(twin_path_t *path,
@@ -1042,6 +1044,8 @@ twin_fixed_t twin_cos(twin_angle_t a);
10421044

10431045
twin_fixed_t twin_tan(twin_angle_t a);
10441046

1047+
void twin_sincos(twin_angle_t a, twin_fixed_t *sin, twin_fixed_t *cos);
1048+
10451049
/*
10461050
* widget.c
10471051
*/

src/matrix.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,8 @@ twin_point_t _twin_matrix_expand(twin_matrix_t *matrix)
100100
void twin_matrix_rotate(twin_matrix_t *m, twin_angle_t a)
101101
{
102102
twin_matrix_t t;
103-
twin_fixed_t c = twin_cos(a);
104-
twin_fixed_t s = twin_sin(a);
103+
twin_fixed_t c, s;
104+
twin_sincos(a, &c, &s);
105105

106106
t.m[0][0] = c;
107107
t.m[0][1] = s;

src/path.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,13 @@ void twin_path_draw(twin_path_t *path, twin_fixed_t x, twin_fixed_t y)
134134
_twin_matrix_y(&path->state.matrix, x, y));
135135
}
136136

137+
void twin_path_draw_polar(twin_path_t *path, twin_angle_t deg)
138+
{
139+
twin_fixed_t s, c;
140+
twin_sincos(deg, &s, &c);
141+
twin_path_draw(path, c, s);
142+
}
143+
137144
void twin_path_rdraw(twin_path_t *path, twin_fixed_t dx, twin_fixed_t dy)
138145
{
139146
twin_spoint_t here = _twin_path_current_spoint(path);
@@ -218,14 +225,13 @@ void twin_path_arc(twin_path_t *path,
218225
twin_angle_t last = (start + extent - inc + epsilon) & ~(step - 1);
219226

220227
if (first != start)
221-
twin_path_draw(path, twin_cos(start), twin_sin(start));
228+
twin_path_draw_polar(path, start);
222229

223230
for (twin_angle_t a = first; a != last; a += inc)
224-
twin_path_draw(path, twin_cos(a), twin_sin(a));
231+
twin_path_draw_polar(path, a);
225232

226233
if (last != start + extent)
227-
twin_path_draw(path, twin_cos(start + extent),
228-
twin_sin(start + extent));
234+
twin_path_draw_polar(path, start + extent);
229235

230236
twin_path_set_matrix(path, save);
231237
}

src/trig.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,10 @@ twin_fixed_t twin_tan(twin_angle_t a)
6262
return 0;
6363
return ((s << 15) / c) << 1;
6464
}
65+
66+
void twin_sincos(twin_angle_t a, twin_fixed_t *sin, twin_fixed_t *cos)
67+
{
68+
*sin = twin_sin(a);
69+
*cos = twin_cos(a);
70+
}
71+

0 commit comments

Comments
 (0)