#include "../../../headers/input/touch/touch_controller_L2.h" #include #include /* ...but the compiler throws an error, so math.h does not really define it */ /* aarch64-linux-gnu/include/math.h already defines it */ #ifndef M_PI #define M_PI 3.14159265358979323846264338327950288 #endif touch_coordinates* TC_L2_getAABB( touch_event **events, uint8_t count){ touch_coordinates* AABB = malloc( 2 * sizeof(touch_coordinates)); uint8_t minX = UINT8_MAX; uint8_t maxX = 0; uint8_t minY = UINT8_MAX; uint8_t maxY = 0; touch_event *current; for( uint8_t i = 0; i < count; i++){ current = events[i]; if( minX > current->position.x) minX = current->position.x; if( maxX < current->position.x) maxX = current->position.x; if( minY > current->position.y) minY = current->position.y; if( maxY < current->position.y) maxY = current->position.y; } AABB[0].x = minX; AABB[0].y = minY; AABB[1].x = maxX; AABB[1].y = maxY; return AABB; } float TC_L2_get_width( touch_event **events, uint8_t count){ touch_coordinates* AABB = TC_L2_getAABB( events, count); uint16_t dx = AABB[0].x - AABB[1].x; uint16_t dy = AABB[0].y - AABB[1].y; float width = sqrtf( powf( dx, 2) + powf( dy, 2)); free( AABB); return width; } float TC_L2_get_angle( touch_event **events, uint8_t count){ touch_coordinates* AABB = TC_L2_getAABB( events, count); uint16_t dx = AABB[0].x - AABB[1].x; uint16_t dy = AABB[0].y - AABB[1].y; free( AABB); return atan2f(dx, dy) * 180 / M_PI; } touch_coordinates* TC_L2_get_midpoint( touch_event **events, uint8_t count){ uint32_t sum_x; uint32_t sum_y; if( !count){ return NULL; } for( uint8_t i = 0; i < count; i++){ sum_x += events[i]->position.x; sum_y += events[i]->position.y; } touch_coordinates *mid = malloc( sizeof( touch_coordinates)); mid->x = sum_x / count; mid->y = sum_y / count; return mid; }