js 数据结构中邻接矩阵的图的直观表示

做题做到了这样一道题,设用邻接矩阵A表示图G的存储结构,G的顶点为V0,V1,V2,V3,V4,V5,V6,则关于图G的说法正确的是

这道题比较简单,基本思路是:

不对称矩阵是有向图

有向图才会有入度和出度

 

学习JS做练习,刚好使用对象这一块内容,用JavaScript,canvas,prototype将数据结构中邻接矩阵的图做一个直观表示。

 

/**
 * Created by CC on 2017/9/4.
 */
var Gra=function(){
this.elem=arguments[0];
this.options=arguments[1];
this.width=this.elem.width;
this.height=this.elem.height;
this.context=this.elem.getContext('2d');
this.deName=false;
this.isSymmetric=true;
this.pointSize=15;
this.pointData=[];
this.init();
}
Gra.prototype.init=function()
{
var name=this.options.name;
 this.num=this.options.data.length;
if(name&&name.length!=0)
    {
        this.deName=true;
    }
    this.drawPoint();
    this.drawEdge();
    this.initaction();
}
//绘顶点
Gra.prototype.drawPoint=function()
{
    var ctx=this.context;
    var num=this.num;
    var dia=(this.width<this.height) ? this.width : this.height;
    var centerx=this.width/2;
    var centery=this.height/2;
    var rad=2*Math.PI/num;
    for(var i=0;i<num;i++) {
        var y = (dia / 2 - 30) * Math.sin(rad * i) + centery;
        var x = (dia / 2 - 30) * Math.cos(rad * i) + centerx;
        ctx.beginPath();
        ctx.strokeStyle='#000';
        ctx.lineWidth=1;
        ctx.arc(x, y, this.pointSize, 0, 2 * Math.PI, false);
        ctx.stroke();
        this.pointData.push({x:x,y:y})
    }
}
//绘边
Gra.
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值