intersection.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. /*
  2. intersection.c
  3. coverage: gcc -fprofile-arcs -ftest-coverage intersection.c
  4. new intersection with asymetric boundaries
  5. warning: the case v0==v1 is not checked.
  6. */
  7. #include <stdint.h>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <assert.h>
  11. uint8_t array[256];
  12. void clear(void)
  13. {
  14. int i;
  15. for( i = 0; i < 256; i++ )
  16. array[i] = 0;
  17. }
  18. void line(uint8_t from, uint8_t to, uint8_t value)
  19. {
  20. uint8_t i = from;
  21. for(;;)
  22. {
  23. if ( i == to )
  24. break;
  25. array[i] |= value;
  26. i++;
  27. }
  28. }
  29. void show(void)
  30. {
  31. int i = 0;
  32. for(;;)
  33. {
  34. if ( i == 256 )
  35. break;
  36. if ( array[i] == 0 ) printf(".");
  37. if ( array[i] == 1 ) printf("=");
  38. if ( array[i] == 2 ) printf("|");
  39. if ( array[i] == 3 ) printf("#");
  40. i++;
  41. if ( (i % 64) == 0 ) printf("\n");
  42. }
  43. printf("\n");
  44. }
  45. uint8_t a1 = 20;
  46. uint8_t a2 = 40;
  47. int is_array_intersection(void)
  48. {
  49. int i;
  50. for( i = 0; i < 256; i++ )
  51. if ( array[i] >= 3 )
  52. return 1;
  53. return 0;
  54. }
  55. #define PLA_SIZE 512
  56. int pla[PLA_SIZE];
  57. void conditioninit(void)
  58. {
  59. int i;
  60. for( i = 0; i < PLA_SIZE; i++ )
  61. pla[i] = 0;
  62. }
  63. void conditionshow(void)
  64. {
  65. int i, j;
  66. for( i = 0; i < PLA_SIZE; i++ )
  67. {
  68. for( j = 8; j >= 0; j-- )
  69. {
  70. printf("%d", (i>>j)&1);
  71. }
  72. /*
  73. if ( pla[i] == 0 )
  74. printf(" -\n");
  75. else
  76. printf(" %d\n", pla[i]);
  77. */
  78. if ( pla[i] == 0 )
  79. printf(" 2\n");
  80. else
  81. printf(" %d\n", pla[i]-1);
  82. }
  83. }
  84. int conditionpattern(uint8_t b1, uint8_t b2)
  85. {
  86. int c, p;
  87. p = 0;
  88. c = b1 < a1 ? 1 : 0;
  89. p <<= 1;
  90. p |= c;
  91. c = b1 <= a1 ? 1 : 0;
  92. p <<= 1;
  93. p |= c;
  94. c = b1 < a2 ? 1 : 0;
  95. p <<= 1;
  96. p |= c;
  97. c = b1 <= a2 ? 1 : 0;
  98. p <<= 1;
  99. p |= c;
  100. c = b2 > a1 ? 1 : 0;
  101. p <<= 1;
  102. p |= c;
  103. c = b2 >= a1 ? 1 : 0;
  104. p <<= 1;
  105. p |= c;
  106. c = b2 > a2 ? 1 : 0;
  107. p <<= 1;
  108. p |= c;
  109. c = b2 >= a2 ? 1 : 0;
  110. p <<= 1;
  111. p |= c;
  112. c = b1 <= b2 ? 1 : 0;
  113. p <<= 1;
  114. p |= c;
  115. return p;
  116. }
  117. int is_math_intersection_original(uint8_t b1, uint8_t b2)
  118. {
  119. uint8_t c1, c2, c3;
  120. c1 = b1 <= a2;
  121. c2 = b2 >= a1;
  122. c3 = b1 > b2;
  123. if ( c1 && c2 )
  124. return 1;
  125. if ( c1 && c3 )
  126. return 1;
  127. if ( c2 && c3 )
  128. return 1;
  129. return 0;
  130. }
  131. int is_math_intersection(uint8_t b1, uint8_t b2)
  132. {
  133. uint8_t c1, c2, c3, tmp;
  134. c1 = b1 < a2; // c1 = b1 <= a2;
  135. c2 = b2 > a1; // c2 = b2 >= a1;
  136. c3 = b1 > b2; // c3 = b1 > b2;
  137. tmp = c1;
  138. c1 &= c2;
  139. c2 &= c3;
  140. c3 &= tmp;
  141. c1 |= c2;
  142. c1 |= c3;
  143. return c1;
  144. /*
  145. if ( c1 && c2 )
  146. return 1;
  147. if ( c1 && c3 )
  148. return 1;
  149. if ( c2 && c3 )
  150. return 1;
  151. return 0;
  152. */
  153. }
  154. uint8_t u8g_is_intersection_math(uint8_t a0, uint8_t a1, uint8_t v0, uint8_t v1)
  155. {
  156. uint8_t c1, c2, c3, tmp;
  157. c1 = v0 < a1; // c1 = v0 <= a1;
  158. c2 = v1 > a0; // c2 = v1 >= a0;
  159. c3 = v0 > v1; // c3 = v0 > v1;
  160. tmp = c1;
  161. c1 &= c2;
  162. c2 &= c3;
  163. c3 &= tmp;
  164. c1 |= c2;
  165. c1 |= c3;
  166. return c1 & 1;
  167. }
  168. /*
  169. version with asymetric boundaries.
  170. a1 and v1 are excluded
  171. v0 == v1 is not support end return 1
  172. */
  173. uint8_t u8g_is_intersection_decision_tree(uint8_t a0, uint8_t a1, uint8_t v0, uint8_t v1)
  174. {
  175. if ( v0 < a1 ) // v0 <= a1
  176. {
  177. if ( v1 > a0 ) // v1 >= a0
  178. {
  179. return 1;
  180. }
  181. else
  182. {
  183. if ( v0 > v1 ) // v0 > v1
  184. {
  185. return 1;
  186. }
  187. else
  188. {
  189. return 0;
  190. }
  191. }
  192. }
  193. else
  194. {
  195. if ( v1 > a0 ) // v1 >= a0
  196. {
  197. if ( v0 > v1 ) // v0 > v1
  198. {
  199. return 1;
  200. }
  201. else
  202. {
  203. return 0;
  204. }
  205. }
  206. else
  207. {
  208. return 0;
  209. }
  210. }
  211. }
  212. void check(uint8_t b1, uint8_t b2)
  213. {
  214. int intersection, p;
  215. clear();
  216. line(a1, a2, 1);
  217. line(b1, b2, 2);
  218. //show();
  219. intersection = is_array_intersection();
  220. p = conditionpattern(b1, b2);
  221. pla[p] |= intersection+1;
  222. /*
  223. if ( p == 0 || p == 7)
  224. printf("%02x: [%d %d] [%d %d] array_intersection:%d\n", p, a1, a2, b1, b2, intersection);
  225. */
  226. /*
  227. if ( is_math_intersection(b1, b2) != intersection)
  228. {
  229. printf("%02x: [%d %d] [%d %d] is_math_intersection:%d != %d failed\n", p, a1, a2, b1, b2, intersection, is_math_intersection(b1, b2));
  230. exit(0);
  231. }
  232. */
  233. if ( u8g_is_intersection_decision_tree(a1,a2,b1,b2) != intersection )
  234. {
  235. printf("%02x: [%d %d] [%d %d] u8g_is_intersection_decision_tree:%d != %d failed\n", p, a1, a2, b1, b2, intersection, u8g_is_intersection_decision_tree(a1,a2,b1,b2));
  236. exit(0);
  237. }
  238. if ( u8g_is_intersection_math(a1,a2,b1,b2) != intersection )
  239. {
  240. printf("%02x: [%d %d] [%d %d] u8g_is_intersection_math:%d != %d failed\n", p, a1, a2, b1, b2, intersection, u8g_is_intersection_math(a1,a2,b1,b2));
  241. exit(0);
  242. }
  243. }
  244. #define STEP 1
  245. void check_size(uint8_t size)
  246. {
  247. int i;
  248. uint8_t b1;
  249. uint8_t b2;
  250. b1 = 0;
  251. for( i = 0; i <= 256; i+=STEP )
  252. {
  253. b2 = b1;
  254. b2 += size;
  255. check(b1, b2);
  256. b1 += STEP;
  257. }
  258. }
  259. void check_all(void)
  260. {
  261. uint8_t size;
  262. for( size =1; size < 255; size++ )
  263. {
  264. printf("size=%d\n", size);
  265. check_size(size);
  266. }
  267. }
  268. void main(void)
  269. {
  270. /*
  271. assert( u8g_is_intersection_decision_tree(4, 6, 7, 9) == 0 );
  272. assert( u8g_is_intersection_decision_tree(4, 6, 6, 9) != 0 );
  273. assert( u8g_is_intersection_decision_tree(6, 9, 4, 6) != 0 );
  274. assert( u8g_is_intersection_decision_tree(7, 9, 4, 6) == 0 );
  275. */
  276. conditioninit();
  277. check_all();
  278. /*
  279. check_size(7);
  280. check_size(17);
  281. check_size(20);
  282. check_size(21);
  283. check_size(37);
  284. check_size(78);
  285. */
  286. //conditionshow();
  287. }