js面向对象的复数运算

var Complex = function (x,y)
{
    this.x=x;
    this.y=y;
    this.angle= function(){ return Math.atan(y/x);}
    this.modes= function(){ return Math.sqrt(x*x+y*y); }
    this.toString= function(){ return x+","+y;}
    this.max= function() {   return (x>y)?x:y;}
    this.min= function() {   return (x<y)?x:y;}
    this.quad= function()
    {
      if(x>=0&&y>=0){ return 1;}
      if(x>=0&&y<0){ return 4;}
      if(x<0&&y>=0){ return 2;}
      if(x<=0&&y<=0){ return 3;}
    }
    this.add= function(z) { return new Complex( this.x+z.x, this.y+z.y); } //加
    this.sub= function(z) { return new Complex( this.x-z.x, this.y-z.y); } //减
    this.flipy= function(){ return new Complex( this.x, -this.y); }
    this.flipx= function(){ return new Complex( - this.x, this.y); }
    this.inv= function() { return new Complex( - this.x, - this.y); }
    this.multi= function(z) //乘
    {
       return new Complex( this.x*z.x- this.y*z.y, this.x*z.y+ this.y*z.x);
    }
    this.recip= function() //倒数
    {
        return new Complex( this.x/( this.x* this.x+ this.y* this.y), - this.y/( this.x* this.x+ this.y* this.y));
    }
    this.div = function(t) //除以实数
    {
       return new Complex( this.x/t, this.y/t);
    }
    this.divide= function(z) //除
    {
       return this.multi(z.recip());
    }
    this.rot = function(z0,t) //旋转指定角度
    {
       var Zangle= new Complex(Math.cos(t),Math.sin(t));
       return this.sub(z0).multi(Zangle).add(z0);
    }   
}

var z1= new Complex(1,2);
var z2= new Complex(3,4);


var z=z1.rot(z2,3.14);
var x=z.x;
var y=z.y;
alert(x+","+y)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值