Difference between revisions of "U"

From Robowiki
Jump to navigation Jump to search
m
(update to date)
Line 1: Line 1:
This page is dedicated for describing the <code>xor.util.ds.U</code>
+
{{Stub}}
 +
 
 +
This page is dedicated for describing the <code>aaa.util.math.U</code>
  
  
Line 5: Line 7:
  
  
U is a universal immutable data-structure library, where U stands for both Util and Universal.
+
''U'' is a universal math library, where ''U'' stands for ''U''niversal.  
 
 
With U, you can write code just like this:
 
<syntaxhighlight lang="java">
 
Vec dis = displacement(bot.pos(), enemy.pos());
 
dis = rotate(dis, radians(PI / 2));
 
Point projected = project(bot.pos(), dis);
 
</syntaxhighlight>
 
 
 
Or even this:
 
 
 
<syntaxhighlight lang="java">
 
Direction direct = direction(displacement(bot.pos(), enemy.pos()));
 
double latVel = cross(direct, enemy.vel());
 
double advVel = -dot(direct, enemy.vel());
 
</syntaxhighlight>
 
 
 
And if you like:
 
 
 
<syntaxhighlight lang="java">
 
import xor.util.ds.*;
 
import static xor.util.ds.U.*;
 
 
 
class Wave {
 
  private final double fireTime;
 
  private final Point source;
 
  private final Direction direction;
 
 
 
  Wave(double fireTime, @NotNull Point source, @NotNull Direction direction) {
 
    this.fireTime = fireTime;
 
    this.source = source;
 
    this.direction = direction;
 
  }
 
 
 
  public @NotNull Vec displacement(double now, double speed) {
 
    return mul(direction, speed * (now - fireTime));
 
  }
 
}
 
</syntaxhighlight>
 
 
 
With a universal immutable data-structure library, you can easily build data abstraction on top.  
 
  
<strong>No longer messy code like this:</strong>
+
The propose of U is to avoid code like this, which coupled business logic and math logic
  
 
<syntaxhighlight lang="java">
 
<syntaxhighlight lang="java">
@Override
 
public final void onScannedRobot(@NotNull ScannedRobotEvent event) {
 
 
   double absoluteBearing = this.getHeadingRadians() + event.getBearingRadians();
 
   double absoluteBearing = this.getHeadingRadians() + event.getBearingRadians();
 
   double latVel = Math.sin(event.getHeadingRadians() - absoluteBearing) * event.getVelocity();
 
   double latVel = Math.sin(event.getHeadingRadians() - absoluteBearing) * event.getVelocity();
 
   double advVel = -Math.cos(event.getHeadingRadians() - absoluteBearing) * event.getVelocity();
 
   double advVel = -Math.cos(event.getHeadingRadians() - absoluteBearing) * event.getVelocity();
}
 
 
</syntaxhighlight>
 
</syntaxhighlight>
  
Line 61: Line 20:
  
 
<syntaxhighlight lang="java">
 
<syntaxhighlight lang="java">
@Override
+
   V direct = displacement(robot.pos(), enemy.pos());
protected void onUpdated() {
 
   Direction direct = direction(displacement(bot.pos(), enemy.pos()));
 
 
   double latVel = cross(direct, enemy.vel());
 
   double latVel = cross(direct, enemy.vel());
 
   double advVel = -dot(direct, enemy.vel());
 
   double advVel = -dot(direct, enemy.vel());
}
 
</syntaxhighlight>
 
 
A side benefit of using U is that the need of massive trigs is automatically eliminated, as trigs are calculated only when retrieving data from robocode API.
 
 
You can write PrecisePrediction even without using trigs!
 
 
<syntaxhighlight lang="java">
 
Point pos = bot.pos();
 
Vec vel = bot.vel();
 
Angle turn = bot.turnRate(); // trigs are pre-calculated.
 
 
for (int i = 0; i < 100; ++i) {
 
  vel = rotate(vel, turn); // rotate is done with only "*" and "+"
 
  pos = project(pos, vel); // project is done with only "+"
 
}
 
 
</syntaxhighlight>
 
</syntaxhighlight>

Revision as of 08:05, 8 August 2018

This article is a stub. You can help RoboWiki by expanding it.

This page is dedicated for describing the aaa.util.math.U




U is a universal math library, where U stands for Universal.

The propose of U is to avoid code like this, which coupled business logic and math logic

  double absoluteBearing = this.getHeadingRadians() + event.getBearingRadians();
  double latVel = Math.sin(event.getHeadingRadians() - absoluteBearing) * event.getVelocity();
  double advVel = -Math.cos(event.getHeadingRadians() - absoluteBearing) * event.getVelocity();

write like this:

  V direct = displacement(robot.pos(), enemy.pos());
  double latVel = cross(direct, enemy.vel());
  double advVel = -dot(direct, enemy.vel());