Wednesday 15 August 2012

java - Detecting closest element in array -


I am having problems finding the closest element in the player's block (using the circle) array.

Whatever I have done so far:

  Public static int near-abusive behavior (game object object, game object [obstacles]) {int closeest = -1; Float minDistSq = Float.MAX_VALUE; // To begin with ridiculous big value (int i = 0; i & lt; barriers.length; i ++) {Float Barrier Raides = Obstacles [i] .getWidth () / 2; Float object radius = object.getWidth () / 2; Game Object curls = Obstacles [I]; // current float dx = (object .getX) - ((curr.getX ()); Float DI = (object .getY) - ((curr.getY ()); Float distSq = (((dx * dx + dy * dy) - objectRadius) - barrierRadius; // Use square distance if (distSq & lt; minDistSq) // // Smallest and ID minDistSq = remember distSq; Closest = i; }} Closest return; }   

It passes through most tests, but the return of the nearest one to the last one is 2 instead of expecting 2. These are the tests (this is the fourth closest '': Public Open Test Classicbreaker () {// First of the Game Object Player = New Game Object (0,1); Player.Sidwidth (2); Game Object [] Barriers = New Game Object [4]; Obstacles [0] = New Game Object (8 9); Block [0]. Setwidth (3) Barriers [1] = New Game Object (10,15); Obstacles [1] .setWidth (2) ; Barrier [2] = new game object (15,20); barriers [2] .setWidth (5); barrier [3] = new game object (100,210); barriers [3] .setWidth (10); assertEquals (" W Layer first hurdle first ", 0, submission. Clogest bier (player, obstacles); // next to the other player. STX (12); Player Setting (12); emphasis value (" Player for the second hurdle Closest Bayer player, Obstacles); // Closest to third player; STX (12) Player; Setwave (20); Assume to emphasize ("Near player third barrier", 2, submission.Clust Breair (player, obstacles)) // fourth b Nearby. STX to Ladhi (90); player.setY (100); emphasis ( "player Fourth closest player barrier", 3, Sbmisnkclojest Bayriyr (player, obstacles)); }

Your code is correct and your written test is incorrect - hinders 2 is constrained Close to about 90,100

Obstacle 2:

(90-15) ^ 2 + (100-20) ^ 2

12025

Obstacle 3:

(100-90) ^ 2 + (210-100) ^ 2

12200

12025 & lt; 12200 - & gt; Handicap 2 is close

EDIT: In your edited reply, you forgot square object Rugius and Barrier Radius. E.g.

float distSq = (((dx * dx + dy * dy) - objectRadius) - constraint redesign

In this line, you have DX ^ 2, D ^ 2, but only non-square object rades and barrier radius

No comments:

Post a Comment