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;
// 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 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();
// 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;
// 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;