var c = document.getElementById("mainCanvas");
mgmain = new MGCanvas(c);
- /*
+
mgmain.setGraph([[
"A","B","C","D","E","F",
],[
["A", "E"],
["A", "B"],
]]);
- */
+
/*
mgmain.setGraph([[
"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P",
["A", "P"],
]]);
*/
-
+ /*
//Petersen Graph
mgmain.setGraph([[
"A","B","C","D","E","F","G","H","I","J",
],[
- ["A", "B"],
- ["B", "C"],
- ["C", "D"],
+ ["A", "B", "AB"],
+ ["B", "C", "BC"],
+ ["C", "D", "CD"],
["D", "E"],
["E", "A"],
//
["F", "I"],
["G", "J"],
]]);
-
+ */
/*
mgmain.setGraph([[
"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O",
this.isFrozen = false;
this.nodeList = new Array();
this.edgeList = new Array();
+ this.selectedNode = null;
this.selectedEdge = null;
var that = this;
//console.log(p.x + "," + p.y);
var node = that.getNodeAtPointP(p);
that.selectNode(node);
+ var edge = that.getEdgeAtPointP(p);
+ that.selectEdge(edge);
that.isMouseDown = true;
};
this.canvas.onmouseup = function (e){
}
return null;
},
+ getEdgeAtPointP: function(p){
+ var r = new Rectangle(p.x - 10, p.y - 10, 20, 20);
+
+ var el = this.edgeList;
+ for(var i = 0, iLen = el.length; i < iLen; i++){
+ if(r.isIncludePointP(el[i].centerPoint)){
+ return el[i];
+ }
+ }
+ return null;
+ },
selectNode: function(node){
if(this.selectedNode){
this.selectedNode.isSelected = false;
}
this.selectedNode = node;
},
+ selectEdge: function(edge){
+ if(this.selectedEdge){
+ this.selectedEdge.isSelected = false;
+ }
+ if(edge){
+ edge.isSelected = true;
+ }
+ this.selectedEdge = edge;
+ },
setIdentifierForSelectedNode: function(str){
if(this.selectedNode){
this.selectedNode.identifier = str;
this.env.context.strokeStyle = this.strokeStyle;
}
this.env.drawCircle(this.position.x, this.position.y, this.size);
- this.env.drawText(this.identifier.toString(), this.position.x + 10, this.position.y + 10);
+ if(this.identifier){
+ this.env.drawText(this.identifier.toString(), this.position.x + 10, this.position.y + 10);
+ }
},
tick: function(){
var e;
//
this.strokeStyle = "rgba(0, 0, 0, 1)";
this.isSelected = false;
+ //
+ this.centerPoint = new Point2D(0, 0);
}
MGEdge.prototype = {
draw: function(){
this.env.context.strokeStyle = this.strokeStyle;
}
if(this.node0 && this.node1){
- this.env.drawLineP(this.node0.position, this.node1.position);
+ this.drawCurvedLineP(this.node0.position, this.node1.position);
+ this.env.strokeRect(this.centerPoint.x - 8, this.centerPoint.y - 8, 16, 16);
+ if(this.identifier){
+ this.env.drawText(this.identifier.toString(), this.centerPoint.x, this.centerPoint.y);
+ }
}
},
tick: function(){
this.node1.vector.x -= e.x;
this.node1.vector.y -= e.y;
}
+ this.centerPoint = new Point2D((this.node0.position.x + this.node1.position.x) / 2, (this.node0.position.y + this.node1.position.y) / 2);
+ },
+ drawCurvedLineP: function(p, q){
+ var that = this;
+ var d = function(x){ return that.env.drawCoordinatesInInteger(x); };
+ var v = this.env.getUnitVectorP(p, q);
+ var w = new Point2D(-(v.y * 50), v.x * 50);
+ this.env.context.beginPath();
+ this.env.context.moveTo(d(p.x), d(p.y));
+ this.env.context.bezierCurveTo(d(this.centerPoint.x + w.x), d(this.centerPoint.y + w.y), d(this.centerPoint.x - w.x), d(this.centerPoint.y - w.y), d(q.x), d(q.y));
+ //this.env.context.closePath();
+ this.env.context.stroke();
},
}