Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

Cookbook

This page is an introduction to An by example. It covers the most important classes.

Random

An provides several random-related functions in Random.h: Two distributions (An::uniformRand and An::gaussianRand) working on double, and two functions returning a random bool (An::boolRand) and a random unsigned int on a certain range (An::intRand).

In addition, An also provides a random number generator which is faster, but less accurate than the C standard rand(): An::FastRandom. It is very easy to use, just instanciate the An::FastRandom class and call get() on it:

// Create a fast random object
FastRandom fastRandom;

// Set the seed to actual time
fastRandom.setSeed((unsigned long)time(NULL));

// Read a random value as unsigned long
std::cout << "Random unsigned long = " << fastRandom.get() << std::endl;

// Read a random value as double
std::cout << "Random double = " << fastRandom.getRange(1.0) << std::endl;

Geometry

Liban provides primitives for 2D geometry, including An::Vector, An::Matrix22 and An::Segment. Those classes have corresponding overloaded operators for ease of use:
// Vector creation
An::Vector v1;
An::Vector v2(2, 3);
An::Vector v3 = v1 + v2;

// Overloaded operators
v1 = (v3 * 2) + v2;

// Dot product
double dotproduct = v1 * v2;

// Norm
double dist = norm(v3);

// Unitary vector
An::Vector v4 = v3.unitary();

// Cross product
double crossproduct = v4.cross(v2);

// Matrix creation
An::Matrix22 rotationMatrix(M_PI/4.0);

// Rotate vector
v4 = rotationMatrix * v4;

// Segment creation
An::Segment s(v1, v2);
An::Segment t(v3, v4);

// Distance to segment
double distToSegment = s.dist(An::Vector(10, 10));

// Segment intersection
bool isIntersection = s.doesIntersect(t);

Color

The An::Color class contains 4 double values (RGBA). It has overloaded operators for ease of use:
// Color creation
An::Color r = An::red;
An::Color g = An::green;
An::Color b = An::blue;

// Overloaded operators
An::Color c = (r + g) * 2 - b;
c /= 2;

// Component access
c[0] += 0.1;
c[2] *= 0.95;

// Threshold
c.threshold(An::Color(0, 0, 0));

// Conversion to gray
An::Color bw = c.gray();

Formula

An::Formula is a class that parses a string representing a formula and returns its result on An::Formula::eval(). It can bind formula variable in formula to C++ doubles and can be extended by user functions:
// Create a Formula object
An::Formula formula("x*cos(y) + 0.5*y*(x-y)");

// C++ doubles
double x, y;

// Bind C++ doubles to formula names
formula.setVariable("x", x);
formula.setVariable("y", y);

// Parse formula
An::Formula::ParseResult result = formula.parse();
if (result.result != An::Formula::ParseResult::SUCCESS)
{
    std::cerr << "Error at pos " << result.errorPos << std::endl;
    // do something
}

// Eval formula for some values
for (x = -10; x < 10; x++)
    for (y = -10; y < 10; y++)
        std::cout << "f(" << x << ", " << y << ") =  " << formula.eval() << std::endl;
An is very fast because it creates highly efficient bytecode on An::Formula::parse.

Miscanellous

An provides another facility: An::ExpDecay, an exponential decay variable that is timestep independant:
// Create an exponential decay
An::ExpDecay decay();

// Initial value = 10
decay.setValue(10);

// Time constant = 1
decay.setTau(1);

// Timestep = 0.1
decay.setDt(0.1);

// Do the decay
for (double t = 0; t < 10; t += 0.1)
    std::cout << "decay(" << t << ") = " << (double)decay << std::endl;

Generated on Mon Oct 24 17:30:34 2005 for liban by  doxygen 1.4.2