Line data Source code
1 : /* File: geometry_offset.inl; Copyright and License: see below */ 2 : 3 : #include "u8/u8_trace.h" 4 : #include "u8/u8_log.h" 5 : #include <assert.h> 6 : #include <math.h> 7 : 8 66 : static inline void geometry_offset_init ( geometry_offset_t *this_, double dx, double dy ) 9 : { 10 66 : (*this_).dx = dx; 11 66 : (*this_).dy = dy; 12 66 : } 13 : 14 65 : static inline geometry_offset_t geometry_offset_new ( double dx, double dy ) 15 : { 16 : geometry_offset_t result; 17 65 : geometry_offset_init( &result, dx, dy ); 18 65 : return result; 19 : } 20 : 21 9 : static inline void geometry_offset_reinit ( geometry_offset_t *this_, double dx, double dy ) 22 : { 23 9 : (*this_).dx = dx; 24 9 : (*this_).dy = dy; 25 9 : } 26 : 27 1 : static inline void geometry_offset_copy ( geometry_offset_t *this_, const geometry_offset_t *original ) 28 : { 29 1 : assert( NULL != original ); 30 1 : (*this_) = (*original); 31 1 : } 32 : 33 1 : static inline void geometry_offset_replace ( geometry_offset_t *this_, const geometry_offset_t *original ) 34 : { 35 1 : assert( NULL != original ); 36 1 : (*this_) = (*original); 37 1 : } 38 : 39 2 : static inline void geometry_offset_destroy ( geometry_offset_t *this_ ) 40 : { 41 2 : } 42 : 43 65 : static inline double geometry_offset_get_dx ( const geometry_offset_t *this_ ) 44 : { 45 65 : return (*this_).dx; 46 : } 47 : 48 65 : static inline double geometry_offset_get_dy ( const geometry_offset_t *this_ ) 49 : { 50 65 : return (*this_).dy; 51 : } 52 : 53 1 : static inline bool geometry_offset_equals ( const geometry_offset_t *this_, const geometry_offset_t *that ) 54 : { 55 1 : assert( NULL != that ); 56 1 : return ( ( fabs( (*this_).dx - (*that).dx ) + fabs( (*this_).dy - (*that).dy ) ) < 0.000000001 ); 57 : } 58 : 59 9 : static inline geometry_direction_t geometry_offset_get_direction ( const geometry_offset_t *this_ ) 60 : { 61 : geometry_direction_t result; 62 9 : if ( (*this_).dx > 0.000000001 ) 63 : { 64 3 : if ( (*this_).dy > 0.000000001 ) 65 : { 66 1 : result = GEOMETRY_DIRECTION_DOWN_RIGHT; 67 : } 68 2 : else if ( (*this_).dy < -0.000000001 ) 69 : { 70 1 : result = GEOMETRY_DIRECTION_UP_RIGHT; 71 : } 72 : else 73 : { 74 1 : result = GEOMETRY_DIRECTION_RIGHT; 75 : } 76 : } 77 6 : else if ( (*this_).dx < -0.000000001 ) 78 : { 79 3 : if ( (*this_).dy > 0.000000001 ) 80 : { 81 1 : result = GEOMETRY_DIRECTION_DOWN_LEFT; 82 : } 83 2 : else if ( (*this_).dy < -0.000000001 ) 84 : { 85 1 : result = GEOMETRY_DIRECTION_UP_LEFT; 86 : } 87 : else 88 : { 89 1 : result = GEOMETRY_DIRECTION_LEFT; 90 : } 91 : } 92 : else 93 : { 94 3 : if ( (*this_).dy > 0.000000001 ) 95 : { 96 1 : result = GEOMETRY_DIRECTION_DOWN; 97 : } 98 2 : else if ( (*this_).dy < -0.000000001 ) 99 : { 100 1 : result = GEOMETRY_DIRECTION_UP; 101 : } 102 : else 103 : { 104 1 : result = GEOMETRY_DIRECTION_CENTER; 105 : } 106 : } 107 9 : return result; 108 : } 109 : 110 1 : static inline void geometry_offset_trace ( const geometry_offset_t *this_ ) 111 : { 112 1 : U8_TRACE_INFO( "geometry_offset_t" ); 113 1 : U8_TRACE_INFO_INT( "- dx:", (*this_).dx ); 114 1 : U8_TRACE_INFO_INT( "- dy:", (*this_).dy ); 115 1 : } 116 : 117 : 118 : /* 119 : Copyright 2025-2025 Andreas Warnke 120 : 121 : Licensed under the Apache License, Version 2.0 (the "License"); 122 : you may not use this file except in compliance with the License. 123 : You may obtain a copy of the License at 124 : 125 : http://www.apache.org/licenses/LICENSE-2.0 126 : 127 : Unless required by applicable law or agreed to in writing, software 128 : distributed under the License is distributed on an "AS IS" BASIS, 129 : WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 130 : See the License for the specific language governing permissions and 131 : limitations under the License. 132 : */