Difference between revisions of "U"

From Robowiki
Jump to navigation Jump to search
m
m (fix)
 
(9 intermediate revisions by the same user not shown)
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 immutable universal 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:
+
The purpose of U is to avoid code like this, which coupled business logic and math logic
  
 
<syntaxhighlight lang="java">
 
<syntaxhighlight lang="java">
Vec dis = displacement(bot.pos(), enemy.pos());
+
double absoluteBearing = this.getHeadingRadians() + event.getBearingRadians();
double latVel = cross(dis, enemy.vel());
+
double latVel = Math.sin(event.getHeadingRadians() - absoluteBearing) * event.getVelocity();
double advVel = -dot(dis, enemy.vel());
+
double advVel = -Math.cos(event.getHeadingRadians() - absoluteBearing) * event.getVelocity();
</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 project(source, direction, speed * (now - fireTime));
 
  }
 
}
 
</syntaxhighlight>
 
 
 
With a immutable universal data-structure library, you can easily build data abstraction on top.
 
 
 
<strong>No longer messy code like this:</strong>
 
 
 
<syntaxhighlight lang="java">
 
@Override
 
public final void onScannedRobot(@NotNull ScannedRobotEvent event) {
 
  double absoluteBearing = this.getHeadingRadians() + event.getBearingRadians();
 
  double latVel = Math.sin(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 direction = direction(robot.pos(), enemy.pos());
protected void onUpdated() {
+
double latVel = cross(direction, enemy.vel());
  Vec dis = displacement(bot.pos(), enemy.pos());
+
double advVel = -dot(direction, enemy.vel());
  double latVel = cross(dis, enemy.vel());
 
  double advVel = -dot(dis, enemy.vel());
 
}
 
 
</syntaxhighlight>
 
</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.
+
the second one is also much faster because it involves no trig.
 
 
You can write PrecisePrediction without using trigs!
 
 
 
<syntaxhighlight lang="java">
 
Point pos = bot.pos();
 
Vec vel = bot.vel();
 
Angle turn = bot.turnRate();
 
 
 
for (int i = 0; i < 100; ++i) {
 
  vel = rotate(vel, turn);
 
  pos = project(pos, vel);
 
}
 
</syntaxhighlight>
 

Latest revision as of 07:17, 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 purpose 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 direction = direction(robot.pos(), enemy.pos());
double latVel = cross(direction, enemy.vel());
double advVel = -dot(direction, enemy.vel());

the second one is also much faster because it involves no trig.