Correct int/frac coordinates for negative position

This commit is contained in:
Danny Robson 2012-05-24 17:08:11 +10:00
parent 4793f435c9
commit 59d2862508

View File

@ -101,6 +101,12 @@ value<L>::eval (double x, double y) const {
double x_fac = x - x_int; double x_fac = x - x_int;
double y_fac = y - y_int; double y_fac = y - y_int;
// Shift the coordinate system down a little to ensure we get unit weights
// for the lerp. It's better to do this than abs the fractional portion so
// we don't get reflections along the origin.
if (x < 0) { x_fac = 1.0 + x_fac; x_int -= 1; }
if (y < 0) { y_fac = 1.0 + y_fac; y_int -= 1; }
// Generate the four corner values // Generate the four corner values
double p0 = generate<double> (x_int, y_int, this->seed); double p0 = generate<double> (x_int, y_int, this->seed);
double p1 = generate<double> (x_int + 1, y_int, this->seed); double p1 = generate<double> (x_int + 1, y_int, this->seed);
@ -144,6 +150,12 @@ gradient<L>::eval (double x, double y) const {
double x_fac = x - x_int; double x_fac = x - x_int;
double y_fac = y - y_int; double y_fac = y - y_int;
// Shift the coordinate system down a little to ensure we get unit weights
// for the lerp. It's better to do this than abs the fractional portion so
// we don't get reflections along the origin.
if (x < 0) { x_fac = 1.0 + x_fac; x_int -= 1; }
if (y < 0) { y_fac = 1.0 + y_fac; y_int -= 1; }
// Generate the four corner values. It's not strictly necessary to // Generate the four corner values. It's not strictly necessary to
// normalise the values, but we get a more consistent and visually // normalise the values, but we get a more consistent and visually
// appealing range of outputs with normalised values. // appealing range of outputs with normalised values.
@ -191,6 +203,12 @@ cellular::eval (double x, double y) const {
double x_fac = x - x_int; double x_fac = x - x_int;
double y_fac = y - y_int; double y_fac = y - y_int;
// Generate the four corner values. It's not strictly necessary to
// normalise the values, but we get a more consistent and visually
// appealing range of outputs with normalised values.
if (x < 0) { x_fac = 1.0 + x_fac; x_int -= 1; }
if (y < 0) { y_fac = 1.0 + y_fac; y_int -= 1; }
// +---+---+---+ // +---+---+---+
// | 0 | 1 | 2 | // | 0 | 1 | 2 |
// +---+---+---+ // +---+---+---+