10
10
11
11
12
12
#define VECTORIO_POLYGON_DEBUG (...) (void)0
13
- // #define VECTORIO_POLYGON_DEBUG(...) mp_printf(&mp_plat_print __VA_OPT__(,) __VA_ARGS__)
13
+ // #define VECTORIO_POLYGON_DEBUG(...) mp_printf(&mp_plat_print, __VA_ARGS__)
14
14
15
15
16
16
// Converts a list of points tuples to a flat list of ints for speedier internal use.
@@ -30,27 +30,32 @@ static void _clobber_points_list(vectorio_polygon_t *self, mp_obj_t points_tuple
30
30
VECTORIO_POLYGON_DEBUG ("free(%d), " , sizeof (self -> points_list ));
31
31
gc_free (self -> points_list );
32
32
}
33
- self -> points_list = gc_alloc (2 * len * sizeof (int ), false, false);
34
- VECTORIO_POLYGON_DEBUG ("alloc(%p, %d)" , self -> points_list , 2 * len * sizeof (int ));
33
+ self -> points_list = gc_alloc (2 * len * sizeof (uint16_t ), false, false);
34
+ VECTORIO_POLYGON_DEBUG ("alloc(%p, %d)" , self -> points_list , 2 * len * sizeof (uint16_t ));
35
35
}
36
36
self -> len = 2 * len ;
37
37
38
- for (size_t i = 0 ; i < len ; ++ i ) {
38
+ for (uint16_t i = 0 ; i < len ; ++ i ) {
39
39
size_t tuple_len = 0 ;
40
40
mp_obj_t * tuple_items ;
41
41
mp_obj_tuple_get (items [i ], & tuple_len , & tuple_items );
42
42
43
43
if (tuple_len != 2 ) {
44
44
mp_raise_ValueError_varg (translate ("%q must be a tuple of length 2" ), MP_QSTR_point );
45
45
}
46
- if (!mp_obj_get_int_maybe (tuple_items [ 0 ], & self -> points_list [2 * i ])
47
- || !mp_obj_get_int_maybe (tuple_items [ 1 ], & self -> points_list [2 * i + 1 ])
46
+ mp_int_t x ;
47
+ mp_int_t y ;
48
+ if (!mp_obj_get_int_maybe (tuple_items [ 0 ], & x )
49
+ || !mp_obj_get_int_maybe (tuple_items [ 1 ], & y )
50
+ || x < SHRT_MIN || x > SHRT_MAX || y < SHRT_MIN || y > SHRT_MAX
48
51
) {
49
- self -> len = 0 ;
50
52
gc_free (self -> points_list );
51
53
self -> points_list = NULL ;
52
54
mp_raise_ValueError_varg (translate ("unsupported %q type" ), MP_QSTR_point );
55
+ self -> len = 0 ;
53
56
}
57
+ self -> points_list [2 * i ] = (int16_t )x ;
58
+ self -> points_list [2 * i + 1 ] = (int16_t )y ;
54
59
}
55
60
}
56
61
@@ -68,16 +73,23 @@ void common_hal_vectorio_polygon_construct(vectorio_polygon_t *self, mp_obj_t po
68
73
69
74
mp_obj_t common_hal_vectorio_polygon_get_points (vectorio_polygon_t * self ) {
70
75
VECTORIO_POLYGON_DEBUG ("%p common_hal_vectorio_polygon_get_points {len: %d, points_list: %p}\n" , self , self -> len , self -> points_list );
71
- mp_obj_t list = mp_obj_new_list (self -> len / 2 , NULL );
76
+ mp_obj_list_t * list = MP_OBJ_TO_PTR (mp_obj_new_list (0 , NULL ));
77
+
78
+ VECTORIO_POLYGON_DEBUG (" >points\n" );
79
+ for (uint16_t i = 0 ; i < self -> len ; i += 2 ) {
80
+ VECTORIO_POLYGON_DEBUG (" (%4d, %4d)\n" , self -> points_list [i ], self -> points_list [i + 1 ]);
81
+
82
+ mp_obj_tuple_t * pair = MP_OBJ_TO_PTR (mp_obj_new_tuple (2 , NULL ));
83
+ pair -> items [0 ] = mp_obj_new_int ((mp_int_t ) self -> points_list [i ]);
84
+ pair -> items [1 ] = mp_obj_new_int ((mp_int_t ) self -> points_list [i + 1 ]);
72
85
73
- for (size_t i = 0 ; i < self -> len ; i += 2 ) {
74
- mp_obj_t tuple [] = { mp_obj_new_int (self -> points_list [i ]), mp_obj_new_int (self -> points_list [i + 1 ]) };
75
86
mp_obj_list_append (
76
87
list ,
77
- mp_obj_new_tuple ( 2 , tuple )
88
+ pair
78
89
);
79
90
}
80
- return list ;
91
+ VECTORIO_POLYGON_DEBUG (" <points\n" );
92
+ return MP_OBJ_FROM_PTR (list );
81
93
}
82
94
void common_hal_vectorio_polygon_set_points (vectorio_polygon_t * self , mp_obj_t points_list ) {
83
95
VECTORIO_POLYGON_DEBUG ("%p common_hal_vectorio_polygon_set_points: " , self );
@@ -98,25 +110,30 @@ void common_hal_vectorio_polygon_set_on_dirty(vectorio_polygon_t *self, vectorio
98
110
99
111
void common_hal_vectorio_polygon_get_area (void * polygon , displayio_area_t * area ) {
100
112
vectorio_polygon_t * self = polygon ;
113
+ VECTORIO_POLYGON_DEBUG ("%p common_hal_vectorio_polygon_get_area\n" );
101
114
102
115
area -> x1 = SHRT_MAX ;
103
116
area -> y1 = SHRT_MAX ;
104
117
area -> x2 = SHRT_MIN ;
105
118
area -> y2 = SHRT_MIN ;
106
- for (size_t i = 0 ; i < self -> len ; ++ i ) {
107
- int x = self -> points_list [i ];
119
+ for (uint16_t i = 0 ; i < self -> len ; ++ i ) {
120
+ int16_t x = self -> points_list [i ];
108
121
++ i ;
109
- int y = self -> points_list [i ];
122
+ int16_t y = self -> points_list [i ];
110
123
if (x < area -> x1 ) {
124
+ VECTORIO_POLYGON_DEBUG (" x1: %d\n" , x );
111
125
area -> x1 = x ;
112
126
}
113
127
if (y < area -> y1 ) {
128
+ VECTORIO_POLYGON_DEBUG (" y1: %d\n" , y );
114
129
area -> y1 = y ;
115
130
}
116
131
if (x > area -> x2 ) {
132
+ VECTORIO_POLYGON_DEBUG (" x2: %d\n" , x );
117
133
area -> x2 = x ;
118
134
}
119
135
if (y > area -> y2 ) {
136
+ VECTORIO_POLYGON_DEBUG (" y2: %d\n" , y );
120
137
area -> y2 = y ;
121
138
}
122
139
}
@@ -126,7 +143,7 @@ void common_hal_vectorio_polygon_get_area(void *polygon, displayio_area_t *area)
126
143
// <0 if the point is to the left of the line vector
127
144
// 0 if the point is on the line
128
145
// >0 if the point is to the right of the line vector
129
- __attribute__((always_inline )) static inline int line_side (mp_int_t x1 , mp_int_t y1 , mp_int_t x2 , mp_int_t y2 , int16_t px , int16_t py ) {
146
+ __attribute__((always_inline )) static inline int line_side (int16_t x1 , int16_t y1 , int16_t x2 , int16_t y2 , int16_t px , int16_t py ) {
130
147
return (px - x1 ) * (y2 - y1 )
131
148
- (py - y1 ) * (x2 - x1 );
132
149
}
@@ -140,14 +157,14 @@ uint32_t common_hal_vectorio_polygon_get_pixel(void *obj, int16_t x, int16_t y)
140
157
return 0 ;
141
158
}
142
159
143
- int winding_number = 0 ;
144
- int x1 = self -> points_list [0 ];
145
- int y1 = self -> points_list [1 ];
146
- for (size_t i = 2 ; i <= self -> len + 1 ; ++ i ) {
160
+ int16_t winding_number = 0 ;
161
+ int16_t x1 = self -> points_list [0 ];
162
+ int16_t y1 = self -> points_list [1 ];
163
+ for (uint16_t i = 2 ; i <= self -> len + 1 ; ++ i ) {
147
164
VECTORIO_POLYGON_DEBUG (" {(%3d, %3d)," , x1 , y1 );
148
- int x2 = self -> points_list [i % self -> len ];
165
+ int16_t x2 = self -> points_list [i % self -> len ];
149
166
++ i ;
150
- int y2 = self -> points_list [i % self -> len ];
167
+ int16_t y2 = self -> points_list [i % self -> len ];
151
168
VECTORIO_POLYGON_DEBUG (" (%3d, %3d)}\n" , x2 , y2 );
152
169
if (y1 <= y ) {
153
170
if (y2 > y && line_side (x1 , y1 , x2 , y2 , x , y ) < 0 ) {
0 commit comments