/* Attractor.cpp
Copyright (c) 2008 Michael Zahniser
Please note the license terms (MIT license) at the end of this document.

Function definitions for the Attractor class.
*/

#include "Attractor.h"

#include <cstdlib>
#include <cmath>

using std::rand;
using std::srand;
using std::sin;
using std::cos;



Attractor::Attractor()
{
	Set(rand());
}



Attractor::Attractor(unsigned long seed)
{
	Set(seed);
}



Attractor::Attractor(const Attractor &attractor)
{
	Set(attractor.seed);
}



Attractor &Attractor::operator=(const Attractor &attractor)
{
	Set(attractor.seed);
	return *this;
}



void Attractor::operator()(double &x, double &y) const
{
	// The low bits of rand() may not be as random as desired.
	// Switch based on a higher bit.
	if(rand() & 256)
	{
		double bx = b * x;
		x = sin(a * y) + c * cos(a * x);
		y = sin(bx) + d * cos(b * y);
	}
	else
	{
		double fx = f * x;
		x = sin(e * y) + g * cos(e * x);
		y = sin(fx) + h * cos(f * y);
	}
}


	
unsigned long Attractor::Seed() const
{
	return seed;
}



void Attractor::Set(unsigned long seed)
{
	srand(seed);
	this->seed = seed;
	
	a = 2.0 * drand();
	b = 2.0 * drand();
	c = 2.0 * drand();
	d = 2.0 * drand();
	
	e = 2.0 * drand();
	f = 2.0 * drand();
	g = 2.0 * drand();
	h = 2.0 * drand();
}



double Attractor::drand()
{
	return static_cast<double>(rand() - RAND_MAX / 2) / static_cast<double>(RAND_MAX / 2);	
}



/* Copyright (c) 2008 Michael Zahniser

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE. */

