// // 「ビルゲイツの入社試験」に出てきた問題をProcessingで作ってみたもの。 // int MARGIN = 50; int RADIUS = 200; float centerx,centery; float oposx, oposy; // 鬼の位置 float posx, posy; // 人の位置 float ospeed = 4.0; // 鬼の速度 float speed = 1.0; // 人の速度 int targetidx = 0; int step = 0; int end = 0; float [] pathx = new float[7]; float [] pathy = new float[7]; void setup(){ float sradius = RADIUS / 4.0; float sx = sradius / 2.0 * sqrt(3.0); float sy = sradius / 2.0; pathx[0] = 0.0; pathy[0] = -sradius; pathx[1] = -sx; pathy[1] = -sy; pathx[2] = -sx; pathy[2] = sy; pathx[3] = 0.0; pathy[3] = sradius; pathx[4] = sx; pathy[4] = sy; pathx[5] = sx; pathy[5] = -sy; size((RADIUS+MARGIN)*2,(RADIUS+MARGIN)*2); centerx = RADIUS + MARGIN; centery = RADIUS + MARGIN; oposx = 0.0; oposy = RADIUS; posx = 0.0; posy = 0.0; } void loop(){ if (end == 1) { return; } float oangle,odelta; float angle,delta; float mangle; float mx, my; background(200,140,255); fill(200,200,200); ellipse(centerx-RADIUS,centery-RADIUS,RADIUS*2,RADIUS*2); // 鬼の角度 oangle = atan2(oposy,oposx); // ヒトの角度 angle = atan2(posy,posx); delta = angle - oangle; if(delta < 0) delta += PI * 2; odelta = ospeed / RADIUS; if(delta > PI) odelta = -odelta; oangle += odelta; oposx = cos(oangle) * RADIUS; oposy = sin(oangle) * RADIUS; fill(255,255,0); rect(centerx+oposx-5,centerx+oposy-5,10,10); mx = pathx[targetidx]; my = pathy[targetidx]; mangle = atan2(my-posy,mx-posx); posx += cos(mangle) * speed; posy += sin(mangle) * speed; fill(0,0,255); rect(centerx+posx-5,centery+posy-5,10,10); point(centerx, centery); line(centerx+posx, centery+posy, centerx+oposx, centery+oposy); float dx = abs(posx - pathx[targetidx]); float dy = abs(posy - pathy[targetidx]); if (dx < 0.1 && dy < 0.1) { if (targetidx != 6) { targetidx = (targetidx + 1) % 6; step += 1; if (abs(posy / (oposy - posy) - posx / (oposx - posx)) < 0.02 && 0 < step && targetidx != 6) { pathx[6] = -oposx; pathy[6] = -oposy; targetidx = 6; } } else { end = 1; } } }