Difference between revisions of "U"
Jump to navigation
Jump to search
m (direct) |
m |
||
Line 5: | Line 5: | ||
− | U is a immutable | + | U is a universal immutable data-structure library, where U stands for both Util and Universal. |
With U, you can write code just like this: | With U, you can write code just like this: | ||
Line 45: | Line 45: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | With a immutable | + | With a universal immutable data-structure library, you can easily build data abstraction on top. |
<strong>No longer messy code like this:</strong> | <strong>No longer messy code like this:</strong> |
Revision as of 06:21, 15 August 2017
This page is dedicated for describing the xor.util.ds.U
U is a universal immutable data-structure library, where U stands for both Util and Universal.
With U, you can write code just like this:
Vec dis = displacement(bot.pos(), enemy.pos());
dis = rotate(dis, radians(PI / 2));
Point projected = project(bot.pos(), dis);
Or even this:
Direction direct = direction(displacement(bot.pos(), enemy.pos()));
double latVel = cross(direct, enemy.vel());
double advVel = -dot(direct, enemy.vel());
And if you like:
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));
}
}
With a universal immutable data-structure library, you can easily build data abstraction on top.
No longer messy code like this:
@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();
}
write like this:
@Override
protected void onUpdated() {
Direction direct = direction(displacement(bot.pos(), enemy.pos()));
double latVel = cross(direct, enemy.vel());
double advVel = -dot(direct, enemy.vel());
}
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!
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 "+"
}