Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var graph = new joint.dia.Graph;
- var paper = new joint.dia.Paper({
- el: $('#paper'),
- width: 1200,
- height:800,
- gridSize: 10,
- model: graph
- });
- joint.shapes.basic.Computer = joint.shapes.basic.Generic.extend({
- markup: '<g class="rotatable"><g class="scalable"><image/></g></g><text/>',
- defaults: joint.util.deepSupplement({
- type: 'basic.Computer',
- attrs: {
- image: {
- 'width': 50,
- 'height': 50
- }
- }
- }, joint.shapes.basic.Generic.prototype.defaults)
- });
- joint.shapes.basic.Switch = joint.shapes.basic.Generic.extend({
- markup: '<g class="rotatable"><g class="scalable"><image/></g></g><text/>',
- defaults: joint.util.deepSupplement({
- type: 'basic.Switch',
- attrs: {
- image: {
- 'width': 100,
- 'height': 100
- }
- }
- }, joint.shapes.basic.Generic.prototype.defaults)
- });
- var addDashLink = function(elm1, elm2) {
- var myLink = new joint.shapes.erd.Line({
- source: { id: elm1.id },
- target: { id: elm2.id },
- attrs: { '.connection': {'stroke-width': 1 , 'stroke-dasharray': '5 2'}}
- });
- graph.addCell(myLink);
- return myLink;
- };
- var addNormalLink = function(elm1, elm2) {
- var myLink = new joint.shapes.erd.Line({
- source: { id: elm1.id },
- target: { id: elm2.id },
- attrs: { '.connection': {'stroke-width': 3 }}
- });
- graph.addCell(myLink);
- return myLink;
- };
- joint.shapes.basic.Controller = joint.shapes.basic.Generic.extend({
- markup: '<g class="rotatable"><g class="scalable"><image/></g></g><text/>',
- defaults: joint.util.deepSupplement({
- type: 'basic.Controller',
- attrs: {
- image: {
- 'width': 150,
- 'height': 150
- }
- }
- }, joint.shapes.basic.Generic.prototype.defaults)
- });
- var id = 0;
- var addSwitch = function(){
- var _switch = new joint.shapes.basic.Switch({
- size: { width: 100 , height: 100},
- attrs:{
- image: { 'xlink:href':'./switch.png', id: id },
- }
- });
- // console.log("AAAAAAAAAA");
- switch_[switch_.length] = _switch;
- console.log(switch_.length);
- }
- var controller = new joint.shapes.basic.Controller({
- size: { width: 150 , height: 150},
- attrs:{
- image: { 'xlink:href': "./nrs.png"}
- }
- });
- var same_sign = function(a, b){
- return (( a * b) >= 0);
- }
- var intersect = function(x1, y1, x2, y2, x3, y3, x4,y4){
- var a1, a2, b1, b2, c1, c2;
- var r1, r2 , r3, r4;
- var denom, offset, num;
- // Compute a1, b1, c1, where line joining points 1 and 2
- // is "a1 x + b1 y + c1 = 0".
- if( (x2 == x3 && y2 == y3) ||(x2 == x4 && y2 ==y4)){
- return false;
- }
- a1 = y2 - y1;
- b1 = x1 - x2;
- c1 = (x2 * y1) - (x1 * y2);
- // Compute r3 and r4.
- r3 = ((a1 * x3) + (b1 * y3) + c1);
- r4 = ((a1 * x4) + (b1 * y4) + c1);
- // Check signs of r3 and r4. If both point 3 and point 4 lie on
- // same side of line 1, the line segments do not intersect.
- if ((r3 != 0) && (r4 != 0) && same_sign(r3, r4)){
- return false;
- }
- // Compute a2, b2, c2
- a2 = y4 - y3;
- b2 = x3 - x4;
- c2 = (x4 * y3) - (x3 * y4);
- // Compute r1 and r2
- r1 = (a2 * x1) + (b2 * y1) + c2;
- r2 = (a2 * x2) + (b2 * y2) + c2;
- // Check signs of r1 and r2. If both point 1 and point 2 lie
- // on same side of second line segment, the line segments do
- // not intersect.
- if ((r1 != 0) && (r2 != 0) && (same_sign(r1, r2))){
- return false;
- }
- //Line segments intersect: compute intersection point.
- denom = (a1 * b2) - (a2 * b1);
- if (denom == 0) {
- return true;
- }
- if (denom < 0){
- offset = -denom / 2;
- }else{
- offset = denom / 2 ;
- }
- // The denom/2 is to get rounding instead of truncating. It
- // is added or subtracted to the numerator, depending upon the
- // sign of the numerator.
- num = (b1 * c2) - (b2 * c1);
- if (num < 0){
- x = (num - offset) / denom;
- }else {
- x = (num + offset) / denom;
- }
- num = (a2 * c1) - (a1 * c2);
- if (num < 0){
- y = ( num - offset) / denom;
- } else {
- y = (num + offset) / denom;
- }
- // lines_intersect
- return true;
- }
- //[0 , 1 .. ,n ] ,each element stores information such as location or color..
- var switch_ =[];
- //relationship between each node
- var list = [[0,5],[1,2] ,[1,3], [ 1,4], [2,3] ,[2,4] , [3,4] , [4,5] , [ 3,5], [1,5]];
- //record min or max value of X (y) coordinates
- var minLocationX;
- var minLocationY;
- var maxLocationX;
- var maxLocationY;
- //init;
- console.log(graph) ;
- var numbers = 6;
- for(var i = 0 ; i <numbers ; i++){
- addSwitch();
- id = id +1;
- }
- //declare two dimensional array, which[i][0] stands for x coordinates of ith element
- //[i][1] stands for y of ith element
- var switchLocation = new Array(switch_.length);
- for (var i =0; i <switch_.length ;i++){
- switchLocation[i] =new Array(2);
- }
- var alreadyAddElement = 0;
- //set position
- console.log("DDDDDDDDDDDDDDDDD");
- for(var i = 0 ; i <switch_.length ; i++){
- console.log(switch_[i]);
- switch_[i].set({
- position: { x : 400+180 * (Math.cos(Math.PI/180*(360/switch_.length)*i)),
- y: 500+180 *Math.sin(Math.PI/180*(360/ switch_.length)*i) }
- });
- switchLocation[i][0] =400+180 * (Math.cos(Math.PI/180*(360/ switch_.length)*i))
- console.log(switchLocation[i][0] +"locationx");
- switchLocation[i][1] = 500+180 *Math.sin(Math.PI/180*(360/ switch_.length)*i)
- console.log(switchLocation[i][1]+"locationy");
- //console.log("------------------------------------------");
- //console.log(50 * Math.cos(Math.PI/180*(360/switchArray.length)*i));
- }
- //add all switch into graph
- for(var i =0 ; i < switch_.length ; i++){
- console.log("AAERRR");
- graph.addCell(switch_[i]);
- }
- console.log("BBBBBBBBBBBBBBBBBBBBBBB");
- addLinkWithDetection();
- console.log("BBBBBBBBBBBBBBBBBBBBBBB2");
- controller.set({
- position: { x :(maxLocationX)/2 ,
- y: 0 }
- });
- console.log("BBBBBBBBBBBBBBBBBBBBBBB3");
- graph.addCell(controller);
- for(var i =0 ; i < switch_.length ; i++){
- addDashLink(controller , switch_[i]);
- };
- paper.$el.css('pointer-events', 'none');
- for( var i = 0 ; i < numbers ; i++){
- $('#' + i).hover(function(){
- // Hover over code
- var title = $(this).attr('title');
- var text = "";
- var txt = $('<div class="tooltip"></div>');
- $(this).data('tipText', title).removeAttr('title');
- //console.log(list.length)+ "AAA");;
- for(var k =0 ; k < list.length ; k++){
- console.log(list[k][0] + "LIST" +$(this).attr('id'));
- console.log(txt.val());
- if (list[k][0] == $(this).attr('id')){
- //text += "->" +list[k][1] + "\r\n";
- text += "->" +list[k][1] + '<br />';
- console.log(list[k][1] + "ENTER");
- }
- else if( list[k][1] ==$(this).attr('id')){
- //text = text.concat(list[k][0] +"->");
- text += list[k][0] +"->" + '<br / >';
- console.log(list[k][0] + "ENTER");
- }
- }
- txt.appendTo('body')
- .html(text)
- txt.fadeIn('slow');
- //txt.text(txt.val());
- console.log(text + "text");
- }, function() {
- // Hover out code
- $(this).attr('title', $(this).data('tipText'));
- $('.tooltip').remove();
- }).mousemove(function(e) {
- var mousex = e.pageX + 20; //Get X coordinates
- var mousey = e.pageY + 10; //Get Y coordinates
- $('.tooltip')
- .css({ top: mousey, left: mousex })
- });
- $('#' + i).css('pointer-events' , 'auto');
- }
- //addComputer();
- /*
- var computer = new joint.shapes.basic.Computer({
- position: { x: 335, y: 50 },
- size: { width: 50, height: 50 },
- attrs: {
- image: {'xlink:href': "./computer.png"}
- }
- });
- graph.addCell(computer);
- */
- /*
- var link = new joint.dia.Link({
- source: { id: computer1.id },
- target: { id: computer2.id },
- attrs: { '.connection': {'stroke-width': 5 }}
- });
- graph.addCell(link);
- */
- function addLinkWithDetection (){
- for(var i = 0 ; i <list.length; i++){
- nodeLocation();
- //addNormalLink(switchArray[list[i][0]], switchArray[list[i][1]];
- alreadyAddElement ++;
- }
- minLocationX = Number.MAX_VALUE;
- minLocationY = Number.MAX_VALUE;
- maxLocationX = Number.MIN_VALUE;
- maxLocationY = Number.MIN_VALUE;
- for(var i = 0 ; i < switch_.length ; i++){
- console.log(switchLocation[i][0] + " " + i);
- //console.log(locationX);
- if(switchLocation[i][0] < minLocationX ){
- minLocationX = switchLocation[i][0];
- }
- if(switchLocation[i][1] < minLocationY ){
- minLocationY = switchLocation[i][1];
- }
- if(switchLocation[i][0] > maxLocationX ){
- maxLocationX = switchLocation[i][0];
- }
- if(switchLocation[i][1] > maxLocationY ){
- maxLocationY = switchLocation[i][1];
- }
- }
- console.log(maxLocationX +"MAX");
- console.log(maxLocationY);
- for(var i =0 ; i < switch_.length ; i++){
- switch_[i].set({
- position: { x :(switchLocation[i][0]-minLocationX) * 1000 / (maxLocationX-minLocationX),
- y: (switchLocation[i][1]-minLocationY)*600 / (maxLocationY-minLocationY)+100 }
- });
- }
- }
- var x1;
- var y1;
- var x2;
- var y2;
- function nodeLocation (){
- x1 =switchLocation[list[alreadyAddElement][0]][0]
- y1 =switchLocation[list[alreadyAddElement][0]][1]
- x2 =switchLocation[list[alreadyAddElement][1]][0]
- y2 = switchLocation[list[alreadyAddElement][1]][1]
- var distancexy = Math.sqrt((x2 -x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
- var _continue = true;
- var angle =0;
- var leftTryTime = 360;
- //just for debug
- var record;
- var recordangle;
- while(_continue == true && leftTryTime >0){
- console.log("------------------------------------------------------" + alreadyAddElement);
- _continue = false;
- for(var i = 0 ; i < alreadyAddElement ; i++){
- //console.log(alreadyAddId);
- var tx1 =switchLocation[list[i][0]][0]
- var ty1 =switchLocation[list[i][0]][1]
- var tx2 =switchLocation[list[i][1]][0]
- var ty2 = switchLocation[list[i][1]][1]
- var sx2; // store x2 if need
- var sy2;
- var distancetxty = Math.sqrt((tx2 -tx1) * (tx2 - tx1) + (ty2 - ty1) * (ty2 - ty1));
- var intersection = intersect(x1,y1,x2,y2,tx1,ty1,tx2,ty2);
- //console.log(tx1 + " " + tx2 + " " + ty1+" " + ty2);
- //console.log(list[i][0] + " " + list[i][1]);
- if( intersection){
- //console.log("184");
- var ok = true
- for(var i = 0 ; i < alreadyAddElement ; i++){
- var dot = ((x2-x1) * (tx2-tx1) + (y2-y1) * (ty2 - ty1)) / Math.sqrt((y2-y1) * (y2-y1) + (x2-x1) * (x2-x1)) / Math.sqrt((ty2-ty1) * (ty2-ty1) + (tx2-tx1) * (tx2-tx1))
- var tx1 =switchLocation[list[i][0]][0]
- var ty1 =switchLocation[list[i][0]][1]
- var tx2 =switchLocation[list[i][1]][0]
- var ty2 = switchLocation[list[i][1]][1]
- if(dot < -0.992 || dot > 0.992){
- ok = false
- }
- }
- if(ok){
- sx2 = x2;
- sy2 = y2;
- record = alreadyAddElement;
- recordangle = angle;
- }
- _continue = true;
- }
- else if(intersection == false){
- var dot = ((x2-x1) * (tx2-tx1) + (y2-y1) * (ty2 - ty1)) / Math.sqrt((y2-y1) * (y2-y1) + (x2-x1) * (x2-x1)) / Math.sqrt((ty2-ty1) * (ty2-ty1) + (tx2-tx1) * (tx2-tx1))
- if( !((x2 == tx1 || y2 == ty1) ||
- (x2 ==ty2 && y2 ==ty2))){
- //console.log(dot + " BCC");
- if(!isFinite(dot) || (dot < -0.992 || dot > 0.992)){
- _continue = true;
- //console.log("ENTER");
- }
- }
- }
- //console.log(_continue);
- }
- if(_continue == true){
- x2 = x1 + distancexy *Math.cos(angle * Math.PI /180)
- //console.log(angle +" angle");
- y2 = y1 + distancexy * Math.sin(angle * Math.PI/180)
- //console.log(x1 + " " + x2 + " " + y1+" " + y2);
- //console.log(x2 + " " + y2+ "motify");
- angle = angle +1;
- leftTryTime --;
- for(var i = 0 ; i < alreadyAddElement ; i++){
- for(var k = 0 ; k < 2 ; k++){
- //console.log(list[i][k] +" " + list[alreadyAddElement][1]);
- //console.log(list[i][k] == list[alreadyAddElement][1]);
- if(list[i][k] == list[alreadyAddElement][1]){
- switchLocation[list[i][k]][0] = x2;
- switchLocation[list[i][k]][1] = y2;
- }
- }
- }
- }
- }
- if(leftTryTime == 0){
- console.log(record +" record " + angle + "angle" );
- x2 = sx2;
- y2 = sy2;
- console.log(x2 + "ABCDEFG" + y2);
- for(var i = 0 ; i < alreadyAddElement ; i++){
- for(var k = 0 ; k < 2 ; k++){
- //console.log(list[i][k] +" " + list[alreadyAddElement][1]);
- //console.log(list[i][k] == list[alreadyAddElement][1]);
- if(list[i][k] == list[alreadyAddElement][1]){
- switchLocation[list[i][k]][0] = x2;
- switchLocation[list[i][k]][1] = y2;
- }
- }
- }
- }
- switch_[list[alreadyAddElement][1]].set({
- position: { x : x2, y: y2 }
- });
- switchLocation[list[alreadyAddElement][1]][0] = x2;
- switchLocation[list[alreadyAddElement][1]][1] = y2;
- addNormalLink(switch_[list[alreadyAddElement][0]], switch_[list[alreadyAddElement][1]]);
- //}
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement