简体   繁体   中英

Constructor function that creates circle objects javascript

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?

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);
    };

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM