Questions

Jump to navigation Jump to search
Revision as of 23 April 2012 at 15:46.
The highlighted comment was created in this revision.

Hi. Has somebody an idear how to make this smaller. I need the max bearing difference between all enemys. My gut feeling says it should be easier, but i can't see it.

	private void calcBearingDiff()
	{
		if (myScans.isEmpty()) return;
		
		Comparator<ATarget> compi = new Comparator<ATarget>() 
		{
			@Override
			public int compare(ATarget a, ATarget b) 
			{
				double eAbsBearA = a.getBearing() + myRobot.getHeadingRadians();
				double eAbsBearB = b.getBearing() + myRobot.getHeadingRadians();
				return Double.compare(eAbsBearA, eAbsBearB);
			}
		};
		ArrayList<ATarget> enemys = new ArrayList<ATarget>(myScans.values());
		Collections.sort(enemys, compi);
		
		enemys.add(enemys.get(0));
		
		ATarget last = null;
		for (ATarget target : enemys) 
		{
			if (last != null) 
			{
				double eAbsBearingA = last.getBearing() + myRobot.getHeadingRadians();
				double eAbsBearingB = target.getBearing() + myRobot.getHeadingRadians();
				double diff = Utils.normalRelativeAngle(eAbsBearingA - eAbsBearingB);	

	                        // .... sort out the max
			}
			last = target;
		}
	}
    Wompi11:06, 21 April 2012

    Ah .. i'm looking for a different approach to the problem and not to make the code smaler.

      Wompi11:19, 21 April 2012
       

      Could make ATarget comparable

        Chase-san18:30, 21 April 2012
         

        Thanks Chase. ATarget is a comparable but for other stuff. What i was looking for was something around another data structur. The above code snippet is way to much code for my taste. I thougth about to register the next ATarget on every ATarget and then simple ask the difference but it didn't work very well because i had to reorder the references to much. I'm looking for something that can handle ... update ATarget ... sort out the closest (bearing) ATarget ... and give the difference back ... I think i have to find a appropriate List/Tree structure for this.

          Wompi13:48, 23 April 2012
           

          Yeah, I had a similar idea to Chase. Sort the targets in radial order (possibly using a custom comparator if you already have one?), then when looking for the closest for a specific target just check the ones on the left and the right to see which is closest. Use (index + list.size())%list.size() on the index for the left and right value so that it automatically wraps back to the beginning/end when you go over the edge. This approach would be the fastest, but I'm not sure it would be the smallest in terms of codesize.

            Skilgannon16:46, 23 April 2012