Last active
August 21, 2016 17:07
-
-
Save MrToph/8f0b86c722c4c72fdb4ea860b109d028 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Curve from './Curve'; | |
export default class PeanoCurve extends Curve { | |
createData (recLevel) { | |
this.points = []; | |
this.recLevel = recLevel; | |
if (recLevel <= 0) return; | |
// maxSize is used for rescaling point coordinates to [0,1] | |
this.maxSize = Math.pow(3, recLevel) - 1; // 3^recLevel - 1 will be the max coordinate of the data. | |
this.createDataRecursive(0, 0, Math.pow(3, recLevel), 0, 0); | |
return this.points; | |
} | |
createDataRecursive (x, y, width, i1, i2) { | |
if (width === 1) { | |
this.points.push([x / this.maxSize, 1 - (y / this.maxSize)]); // 1-y, because svg's y coordinate grows from top to bottom | |
} else { | |
width /= 3; | |
this.createDataRecursive(x + (2 * i1 * width), y + (2 * i1 * width), width, i1, i2); | |
this.createDataRecursive(x + ((i1 - i2 + 1) * width), y + ((i1 + i2) * width), width, i1, 1 - i2); | |
this.createDataRecursive(x + width, y + width, width, i1, 1 - i2); | |
this.createDataRecursive(x + ((i1 + i2) * width), y + ((i1 - i2 + 1) * width), width, 1 - i1, 1 - i2); | |
this.createDataRecursive(x + (2 * i2 * width), y + (2 * (1 - i2) * width), width, i1, i2); | |
this.createDataRecursive(x + ((1 + i2 - i1) * width), y + ((2 - i1 - i2) * width), width, i1, i2); | |
this.createDataRecursive(x + (2 * (1 - i1) * width), y + (2 * (1 - i1) * width), width, i1, i2); | |
this.createDataRecursive(x + ((2 - i1 - i2) * width), y + ((1 + i2 - i1) * width), width, 1 - i1, i2); | |
this.createDataRecursive(x + (2 * (1 - i2) * width), y + (2 * i2 * width), width, 1 - i1, i2); | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment