Difference between revisions of "U"

From Robowiki
Jump to navigation Jump to search
(more content.)
m (fix)
 
(11 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:
+
The purpose of U is to avoid code like this, which coupled business logic and math logic
<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">
 
<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>
 
</syntaxhighlight>
  
And if you like:
+
write like this:
  
 
<syntaxhighlight lang="java">
 
<syntaxhighlight lang="java">
import xor.util.ds.*;
+
V direction = direction(robot.pos(), enemy.pos());
import static xor.util.ds.U.*;
+
double latVel = cross(direction, enemy.vel());
 
+
double advVel = -dot(direction, enemy.vel());
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 traveled(double now, double speed) {
 
    return project(source, direction, speed * (now - fireTime));
 
  }
 
}
 
 
</syntaxhighlight>
 
</syntaxhighlight>
  
With a immutable universal data-structure library, you can easily build data abstraction on top. No longer messy code like this:
+
the second one is also much faster because it involves no trig.
 
 
<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>
 

Latest revision as of 08: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.