构造函数,用于创建圆形对象javascript

时间:2013-12-01 20:17:00

标签: javascript object methods constructor geometry

我想创建一个创建圆形对象的构造函数。我需要添加两个方法。我想要第一个方法(translate())获取两个数字参数,并将第一个添加到Circle的x坐标,并将第二个添加到Circle的y坐标。

我希望第二种方法获取Circle参数,如果两个Circles相交则返回yes,否则返回false。

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

我如何实现第二个,intersects()方法?

1 个答案:

答案 0 :(得分:0)

你走了:

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

}