<commit_before>﻿// 2D Vector
function Vector(x, y) {
    this.x = x;
    this.y = y;
}

Vector.prototype = {
    x: this.x,
    y: this.y,

    //Adds vectors
    add: function (vector) {
        this.x += vector.x;
        this.y += vector.y;
        return this;
    },

    //Subtracts vectors (ordering does matter)
    sub: function (vector) {
        this.x -= vector.x;
        this.y -= vector.y;
        return this;
    },

    //Multiplication
    mul: function (vector) {
        this.x *= vector.x;
        this.y *= vector.y;
        return this;
    },

    //Division
    div: function (vector) {
        this.x /= vector.x;
        this.y /= vector.y;
        return this;
    },

    rotate: function (vector) {
        var nx = this.x * Math.cos(angle) - this.y * Math.sin(angle);
        var ny = this.x * Math.sin(angle) + this.y * Math.cos(angle);

        this.x = nx;
        this.y = ny;

        return this;
    }
};

var Tree = function (vec1, vec2) {
    this.vec1 = vec1;
    this.vec2 = vec2;

    this.draw = function (ctx) {
        ctx.fillStyle = 'black';
        ctx.beginPath();
        ctx.moveTo(vec1.x, vec1.y);
        ctx.lineTo(vec2.x, vec2.y);
        ctx.stroke();
    }

    this.getNewBranches = function () {
        var temp = [];
        rightBranch = new Vector(vec2.x + Math.sqrt(3) + Math.sqrt(Math.pow(Math.abs(vec2.x - vec1.x), 2) + Math.pow(Math.abs(vec2.y - vec1.y), 2)) / 1.5, vec2.y - Math.sqrt(Math.pow(Math.abs(vec2.x - vec1.x), 2) + Math.pow(Math.abs(vec2.y - vec1.y), 2)) / 1.5); //30 deg rotation
        leftBranch = new Vector(vec2.x - Math.sqrt(3) - Math.sqrt(Math.pow(Math.abs(vec2.x - vec1.x), 2) + Math.pow(Math.abs(vec2.y - vec1.y), 2)) / 1.5, vec2.y - Math.sqrt(Math.pow(Math.abs(vec2.x - vec1.x), 2) + Math.pow(Math.abs(vec2.y - vec1.y), 2)) / 1.5); //-30 deg rotation

        temp.push(leftBranch, rightBranch);
        return temp;
    }
};

var canvas = document.getElementById("container"),
    ctx = canvas.getContext("2d");

var myTree,
    branches = [];

function init() {
    myTree = new Tree(new Vector(canvas.width / 2, canvas.height), new Vector(canvas.width / 2, canvas.height - 100));
    branches.push(myTree);
}

function update() {
    ctx.clearRect(0, 0, canvas.width, canvas.height);

    if (branches.length > 0) branches.forEach(function (i) { i.draw(ctx); });
}

function onKeyPress(e) {}

function onMouseClick() {
    //var tempArr = myTree.getNewBranches();
    //for (var i = 0; i < 2; i++) {
    //    var tempTree = new Tree(myTree.vec2, tempArr[i]);
    //    branches.push(tempTree);
    //}
    for (var i = branches.length - 1; i >= 0; i--)
        branches[i].getNewBranches().forEach(function (item) {
            console.log(branches);
            branches.push(new Tree(branches[i].vec2, item));
        });

    console.log(branches);
}

function main() {<commit_msg>Split getBranches into getLeftBranch and getRightBranch for debugging, fixed issues with rotate<commit_after>﻿// 2D Vector
function Vector(x, y) {
    this.x = x;
    this.y = y;
}

Vector.prototype = {
    x: this.x,
    y: this.y,

    //Adds vectors
    add: function (vector) {
        this.x += vector.x;
        this.y += vector.y;
        return this;
    },

    //Subtracts vectors (ordering does matter)
    sub: function (vector) {
        this.x -= vector.x;
        this.y -= vector.y;
        return this;
    },

    //Multiplication
    mul: function (vector) {
        this.x *= vector.x;
        this.y *= vector.y;
        return this;
    },

    //Division
    div: function (vector) {
        this.x /= vector.x;
        this.y /= vector.y;
        return this;
    },

    rotate: function (angle) {
        var nx = this.x * Math.cos(angle * (Math.PI / 180)) - this.y * Math.sin(angle * (Math.PI / 180));
        var ny = this.x * Math.sin(angle * (Math.PI / 180)) + this.y * Math.cos(angle * (Math.PI / 180));

        this.x = nx;
        this.y = ny;

        return this;
    },

    //Returns a copy of the current vector
    copy: function () {
        return new Vector(this.x, this.y);
    }
};

var Tree = function (vec1, vec2) {
    this.vec1 = vec1;
    this.vec2 = vec2;

    this.draw = function (ctx) {
        ctx.fillStyle = 'black';
        ctx.beginPath();
        ctx.moveTo(vec1.x, vec1.y);
        ctx.lineTo(vec2.x, vec2.y);
        ctx.stroke();
    }

    this.getLeftBranch = function () {
        var modifiedVec = this.vec1.copy().sub(this.vec2),
        rightBranch = modifiedVec.copy().rotate(180 + 30);

        return new Tree(vec2, vec2.copy().add(rightBranch));
    }

    this.getRightBranch = function () {
        var modifiedVec = this.vec1.copy().sub(this.vec2),
            rightBranch = modifiedVec.copy().rotate(180 - 30);

        return new Tree(vec2, vec2.copy().add(rightBranch));
    }
};

var canvas = document.getElementById("container"),
    ctx = canvas.getContext("2d");

var myTree,
    branches = [];

function init() {
    myTree = new Tree(new Vector(canvas.width / 2, canvas.height), new Vector(canvas.width / 2, canvas.height - 100));
    branches.push(myTree);
}

function update() {
    ctx.clearRect(0, 0, canvas.width, canvas.height);

    if (branches.length > 0) branches.forEach(function (i) { i.draw(ctx); });
}

function onKeyPress(e) {}

function onMouseClick() {
    //var tempArr = myTree.getNewBranches();
    //for (var i = 0; i < 2; i++) {
    //    var tempTree = new Tree(myTree.vec2, tempArr[i]);
    //    branches.push(tempTree);
    //}
    for (var i = branches.length - 1; i >= 0; i--)
    {
        branches.push(myTree.getLeftBranch());
        branches.push(myTree.getRightBranch());
    }

    console.log(branches);
}

function main() {
{'fraction_non_alphanumeric': 0.166491963661775, 'fraction_numerical': 0.02148846960167715, 'mean_word_length': 2.1181917211328978, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1261389', 'n_tokens_mistral': 2088, 'n_tokens_neox': 2024, 'n_words': 448}