Difference between revisions of "V"

From Robowiki
Jump to navigation Jump to search
(Initial commit)
 
m (boost!)
 
(2 intermediate revisions by the same user not shown)
Line 28: Line 28:
  
 
<syntaxhighlight lang="java">
 
<syntaxhighlight lang="java">
V pos = new V(...);
+
final V pos = new V(...);
  
 
for (...) {
 
for (...) {
   pos.project_(pos, heading, velocity);
+
   pos.project_(heading, velocity); // trailing underscore means inplace
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>
  
which is fast because no objects are allocated in loop at all (and boosted further internaly with FastMath)
+
which is much faster beacuse it can be replaced with scalar internally (in JVM), which means your code actually have the performance of this:
 +
 
 +
<syntaxhighlight lang="java">
 +
double x = ...;
 +
double y = ...; //
 +
 
 +
for (...) {
 +
  double dx = ...; // some fast math
 +
  double dy = ...; // some fast math
 +
 
 +
  x += dx;
 +
  y += dy;
 +
}
 +
</syntaxhighlight>
 +
 
 +
further, ''V'' eliminates unnecessary trig in cases like this:
 +
 
 +
<syntaxhighlight lang="java">
 +
Point2D.Double force = ...;
 +
 
 +
double angle = absoluteBearing(force); // uses Math.atan2, slow
 +
Point2D.Double dest = project(pos, angle, 100); // uses Math.sin and Math.cos, very slow
 +
</syntaxhighlight>
 +
 
 +
With ''V'', you can write:
 +
 
 +
<syntaxhighlight lang="java">
 +
final V force = ...;
 +
 
 +
final V dest = pos.project(force, 100); // uses no trig, much faster
 +
</syntaxhighlight>

Latest revision as of 07:42, 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.V




V is a vector 2d library that simplifies/boosts geom in robocoding.

It's common for robocoders to write code like this:

Point2D.Double project(Point2D.Double point, double angle, double distance) {
  return new Point2D.Double(point.getX() + distance * Math.sin(angle), point.getY() + distance * Math.cos(angle));
}

Point2D.Double pos = ...;

for (...) {
  pos = project(pos, heading, velocity);
}

However, this is slow.

With V, you have code like this

final V pos = new V(...);

for (...) {
  pos.project_(heading, velocity); // trailing underscore means inplace
}

which is much faster beacuse it can be replaced with scalar internally (in JVM), which means your code actually have the performance of this:

double x = ...;
double y = ...; // 

for (...) {
  double dx = ...; // some fast math
  double dy = ...; // some fast math

  x += dx;
  y += dy;
}

further, V eliminates unnecessary trig in cases like this:

Point2D.Double force = ...;

double angle = absoluteBearing(force); // uses Math.atan2, slow
Point2D.Double dest = project(pos, angle, 100); // uses Math.sin and Math.cos, very slow

With V, you can write:

final V force = ...;

final V dest = pos.project(force, 100); // uses no trig, much faster