0

I want to make a constructor function that creates circle objects. I need to add two methods to it. I want first method (translate()) to takes two number parameters and adds the first one to the Circle's x-coordinate and adds the second one to the Circle's y-coordinate.

and I want the second method to take a Circle parameter and return yes if the two Circles intersect, but return false otherwise.

function Circle (xPoint, yPoint, radius) {
this.x = xPoint; 
this.y = yPoint;  
this.r = radius;  
}

function translate(horiz, vert) {
return ((horiz + this.x) && (vert + this.y));
}

How would I implement the second, intersects(), method?

  • First off, that's not really an OO method.`this.y` has no reference to the `Circle` object... Secondly, what have you already tried for the `intersect()` method, and why didn't it work? – BenM Dec 01 '13 at 20:20

1 Answers1

0

Here you go:

function Circle(x, y, r) {

    this.x = x;
    this.y = y;
    this.r = r;

    this.translate = function(h, v) {
        this.x += h;
        this.y += v;
    };

    this.intersect = function(circle) {
        var centerDistance = Math.pow(this.x - circle.x, 2) + Math.pow(this.y - circle.y, 2);
        return Math.pow(this.r - circle.r, 2) <= centerDistance && centerDistance <= Math.pow(this.r + cirle.r, 2);
    };

}
Samuel
  • 2,106
  • 1
  • 17
  • 27
  • [http://stackoverflow.com/questions/8367512/algorithm-to-detect-if-a-circles-intersect-with-any-other-circle-in-the-same-pla] for a discussion about the intersection algorithm – Samuel Dec 01 '13 at 20:28
  • so how would you make this return true if they intersect? – user2985900 Dec 01 '13 at 22:32