MattO
MattO

Reputation: 1753

Where should I place this condition?

For the last step of this project, I want the growing circle to stop when it collides with another circle. The isOnCircle function already checks for this successfully when creating a new circle. However, when adding the condition !isOnCircle to my grow() function (line 61) it prevents any new circles from being added.

function grow() {
    var a = circles[circles.length - 1];
    if (!isOnCircle(a)){
        a.radius += 1;
    }}

Perhaps the circle is being created first, then in the check for collision, it's colliding with itself. Where else could I put the !isOnCircle check so that it gets checked at every radius increase and stops the grow function then? check this

//set up canvas
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");

var circles = [];

//create circle
function create(location) {
    circles.push({
        x: location.x,
        y: location.y,
        radius: 10,
        color: '#' + Math.floor(Math.random() * 16777215).toString(16)
    });
}

//figure out mouse position
var rect = document.getElementById("canvas").getBoundingClientRect();
// Get canvas offset on page
var offset = {
    x: rect.left,
    y: rect.top
};

function isOnCanvas(a) {
    if ((a.x >= 0 && a.x <= rect.width) && (a.y >= 0 && a.y <= rect.height)) {
        return true;
    }
    return false;
}

function isOnCircle(a) {
    var i = 0,
        l = circles.length,
        x, y, d, c;
    for (; i < l; ++i) {
        c = circles[i];
        x = a.x - c.x;
        y = a.y - c.y;
        d = (a.radius || 10) + c.radius;
        if (x * x + y * y <= d * d) {
            return true;
        }
    }
    return false;
}

// draw all circles
function draw() {
    ctx.clearRect(0, 0, canvas.width, canvas.height);
    for (var i = 0; i < circles.length; i++) {
        var p = circles[i];
        ctx.beginPath();
        ctx.arc(p.x, p.y, p.radius, 0, 2 * Math.PI);
        ctx.fillStyle = p.color;
        ctx.fill();
    }
}

//make last drawn circle 1px bigger
function grow() {
    var a = circles[circles.length - 1];
        a.radius += 1;
}

//find percentage of canvas filled in
var totalSpace = canvas.width * canvas.height;
var totalFilled = function () {
    total = 0;
    for (var i = 0; i < circles.length; i++) {
        var p = circles[i];
        total += Math.PI * Math.pow(p.radius, 2);
    }
    return total;
    console.log(total);
}

    function findPercentage() {
        return (totalFilled() / totalSpace) * 100;
    }

    function updateInfo() {
        percentage = findPercentage();
        document.getElementById("percentage").innerHTML = "You've filled in " + percentage.toFixed(1) + "%";
    }

    //do all the stuff
var animate = function () {
    grow();
    draw();
    updateInfo();
}

//put this outside function so we can stop it later
var growLoop;

window.onmousedown = function (e) {
    // get event location on page offset by canvas location
    var location = {
        x: e.pageX - offset.x,
        y: e.pageY - offset.y
    };

    if (isOnCanvas(location) && !isOnCircle(location)) {
        create(location);
        draw();
        updateInfo();
        growLoop = setInterval(animate, 100);
    }
};

window.onmouseup = function () {
    clearInterval(growLoop);
}
window.onmouseout = function () {
    clearInterval(growLoop);
}

Upvotes: 0

Views: 64

Answers (2)

scf1001
scf1001

Reputation: 251

It is colliding with itself. Since you know the current circle will always be the last one in circles you can modify isOnCircle like this:

l = circles.length - 1,

so that it won't check against the current circle.

http://jsfiddle.net/SeAGU/91/

Upvotes: 1

Bergi
Bergi

Reputation: 664464

it's colliding with itself.

Probably. You definitely should avoid that in the collision detection:

function isOnCircle(a) {
    var l = circles.length,
        x, y, d, c;
    for (var i = 0; i < l; ++i) {
        c = circles[i];
        if (a == c)   // add these
            continue; // two lines!
        x = a.x - c.x;
        y = a.y - c.y;
        d = (a.radius || 10) + c.radius;
        if (x * x + y * y <= d * d) {
            return true;
        }
    }
    return false;
}

Upvotes: 1

Related Questions