// coded by SiR // // compile: cl rect.cpp /link user32.lib winmm.lib #include #include #define NUM_OF_RECT 10000 #define TRIAL_LOOP 1000 // 関数動作の検証用 //#define FUNCTION_TEST void swap(LONG &a, LONG &b) { LONG c = a; a = b; b = c; } BOOL collisionRect(LPRECT r1, LPRECT r2) { LONG a = (r1->left - r2->right) & (r2->left - r1->right) & (r1->top - r2->bottom) & (r2->top - r1->bottom); return ((DWORD)a) >> 31; } void main(void) { int i, j; DWORD startTime, endTime; BOOL tmp; RECT r[NUM_OF_RECT]; RECT dmy; BOOL res[NUM_OF_RECT]; // データの準備 for(i = 0; i < NUM_OF_RECT; i ++) { r[i].left = rand( ); r[i].top = rand( ); r[i].right = rand( ); r[i].bottom = rand( ); if(r[i].left > r[i].right) swap(r[i].left, r[i].right); if(r[i].top > r[i].bottom) swap(r[i].top, r[i].bottom); } startTime = timeGetTime( ); for(i = 0; i < TRIAL_LOOP; i ++) { for(j = 0; j < NUM_OF_RECT -1; j ++) { tmp = collisionRect(&r[j], &r[j+1]); #ifdef FUNCTION_TEST if(tmp) tmp = 1; if(res[j] != tmp) { printf("(%d,%d)-(%d,%d) : (%d,%d)-(%d,%d) = %d,%d\n", r[j].left, r[j].top, r[j].right, r[j].bottom, r[j+1].left, r[j+1].top, r[j+1].right, r[j+1].bottom, res[j], tmp); puts("error"); } #endif res[j] = tmp; } } endTime = timeGetTime( ); printf("%ld\n", endTime - startTime); startTime = timeGetTime( ); for(i = 0; i < TRIAL_LOOP; i ++) { for(j = 0; j < NUM_OF_RECT -1; j ++) { tmp = IntersectRect(&dmy, &r[j], &r[j+1]); #ifdef FUNCTION_TEST if(tmp) tmp = 1; #endif res[j] = tmp; } } endTime = timeGetTime( ); printf("%ld\n", endTime - startTime); startTime = timeGetTime( ); for(i = 0; i < TRIAL_LOOP; i ++) { for(j = 0; j < NUM_OF_RECT -1; j ++) { tmp = collisionRect(&r[j], &r[j+1]); #ifdef FUNCTION_TEST if(tmp) tmp = 1; if(res[j] != tmp) { printf("(%d,%d)-(%d,%d) : (%d,%d)-(%d,%d) = %d,%d\n", r[j].left, r[j].top, r[j].right, r[j].bottom, r[j+1].left, r[j+1].top, r[j+1].right, r[j+1].bottom, res[j], tmp); puts("error"); } #endif res[j] = tmp; } } endTime = timeGetTime( ); printf("%ld\n", endTime - startTime); startTime = timeGetTime( ); for(i = 0; i < TRIAL_LOOP; i ++) { for(j = 0; j < NUM_OF_RECT -1; j ++) { tmp = IntersectRect(&dmy, &r[j], &r[j+1]); #ifdef FUNCTION_TEST if(tmp) tmp = 1; #endif res[j] = tmp; } } endTime = timeGetTime( ); printf("%ld\n", endTime - startTime); }