var Network = function(networkDivId, networkWidth, networkHeight, cellWidth, cellHeight){ this.networkWidth = networkWidth - 2; this.networkHeight = networkHeight - 2; this.cellWidth = cellWidth; this.cellHeight = cellHeight; this.walkPathList = []; d3.select("#" + networkDivId) .append("svg") .attr("id", "network") .attr("width",networkWidth) .attr("height",networkHeight); }; Network.prototype.loadImage = function(width, height, imagePath){ d3.select("#network").append("image") .attr("xlink:href", imagePath); }; Network.prototype.getNetwork = function(networkWidth, networkHeight, cellWidth, cellHeight){ var data = new Array(); var xpos = 1; var ypos = 1; // iterate for rows for (var row = 0; row < networkHeight / cellHeight; row++) { data.push( new Array() ); // iterate for cells/columns inside rows for (var column = 0; column < networkWidth / cellWidth; column++) { data[row].push({ x: xpos, y: ypos, width: cellWidth, height: cellHeight, row : row, column : column }) // increment the x position. I.e. move it over by 50 (width variable) xpos += cellWidth; } // reset the x position after a row is complete xpos = 1; // increment the y position for the next row. Move it down 50 (height variable) ypos += cellHeight; } return data; }; Network.prototype.drawNetwork = function(networkWidth, networkHeight, cellWidth, cellHeight){ var gridData = this.getNetwork(networkWidth, networkHeight, cellWidth, cellHeight); var row = d3.select("#network").selectAll(".row") .data(gridData) .enter().append("g") .attr("class", "row"); var column = row.selectAll(".square") .data(function(d) { return d; }) .enter().append("rect") .attr("class","square") .attr("x", function(d) { return d.x; }) .attr("y", function(d) { return d.y; }) .attr("width", function(d) { return d.width; }) .attr("height", function(d) { return d.height; }) .attr("row", function(d) { return d.row; }) .attr("column", function(d) { return d.column; }) .attr("centerX", function(d) { // 셀의 중심점 X var x = ((d.width * d.column) + (d.width * (d.column + 1))) / 2; return x; }) .attr("centerY", function(d) { // 셀의 중심점 Y var y = ((d.height * d.row) + (d.height * (d.row + 1))) / 2; return y; }) .style("fill", "white") .style("opacity", 1) .style("stroke", "#222"); }; Network.prototype.drawObject = function(objectNo, polygonNo, points, x, y) { var polygon = d3.select("#network").append('polygon') .attr('class', 'objects') .attr('points', points) .attr('objectNo', objectNo) .attr('polygonNo', polygonNo) .attr('objectType', '01') .attr('x', x) .attr('y', y) .attr("transform", function(){ return "translate(" + x + "," + y + ")"; }) .attr("walkerable", "false") .style("fill", "black") .style("opacity", "0.3"); }; Network.prototype.intersectObject = function(){ var exits = []; d3.select("#network").selectAll(".objects").each(function(){ var object = d3.select(this); var objectType = object.attr("objectType"); var points = object.attr("points").split(" "); var x = object.attr("x"); var y = object.attr("y"); var polygon = []; for(var i=0; i= nx2 || x2 <= nx1 || y1 >= ny2 || y2 <= ny1)){ colliding = true; return; } }); return colliding; } }; Network.prototype.drawController = function(maxLength){ var self = this; d3.select("#controller").selectAll("*").remove(); var svg = d3.select("#controller"), margin = {right: 30, left: 30}, width =+ svg.attr("width") - margin.left - margin.right - 40, height =+ svg.attr("height"); var play = svg.append("g") .attr("class", "play") .attr("id", "player") .attr("maxLength", maxLength); play.append("circle") .attr("r", 15) .attr("transform", "translate(" + margin.left + "," + height / 2 + ")"); play.append("path") .attr("d", "M-22,-30l60,30l-60,30z") .attr("transform", "translate(" + margin.left + "," + height / 2 + ") scale(.2)"); play.append("rect") .attr("width", 60) .attr("height", height) .on("click", function() { self.play(); }); var x = d3.scaleLinear() .domain([0, maxLength]) .range([0, width]) .clamp(true); var slider = svg.append("g") .attr("class", "slider") .attr("transform", "translate(" + (margin.left + 40) + "," + height / 2 + ")"); slider.append("line") .attr("class", "track") .attr("x1", x.range()[0]) .attr("x2", x.range()[1]) .select(function() { return this.parentNode.appendChild(this.cloneNode(true)); }) .attr("class", "track-inset") .select(function() { return this.parentNode.appendChild(this.cloneNode(true)); }) .attr("class", "track-overlay") .call( d3.drag().on("start.interrupt", function() { slider.interrupt(); }) .on("start drag", function() { self.seeking(x.invert(d3.event.x), x, maxLength); }) ); // 눈금 단위 표시 slider.insert("g", ".track-overlay") .attr("class", "ticks") .attr("transform", "translate(0, 18)") .selectAll("text") .data(x.ticks(maxLength)) .enter() .append("text") .attr("x", x) .attr("text-anchor", "middle") .text(function(d) { if(d == 0){ return "Start" }else if(d == maxLength){ return "Finish" }else{ return d % 2 == 1 ? d + "'" : ""; } }); // 눈금바 표시 slider.insert("g", ".track-overlay") .attr("class", "ticks-bar") .attr("transform", "translate(0, -8)") .selectAll("line") .data(x.ticks(maxLength)) .enter() .append("line") .attr("x1", x) .attr("y1", 0) .attr("x2", x) .attr("y2", 15) .attr("stroke-width", function(d){ if(d == 0){ return 0 }else if(d == maxLength){ return 0 }else{ return 0.5; } }) .attr("stroke", "gray"); var handle = slider.insert("circle", ".track-overlay") .attr("class", "handle") .attr("r", 9); }; // 시뮬레이션 seeking Network.prototype.seeking = function(sliderCurrentT, x, sliderMaxLength) { var handle = d3.select("#controller").select(".handle"); handle.attr("cx", x(sliderCurrentT)); for(var i=0; i .clamp(true); d3.select("#controller").select(".slider").transition() .ease(d3.easeLinear) .duration(maxLength * 1000) .tween("seeking", function() { var i = d3.interpolate(0, maxLength); return function(t) { self.seeking(i(t), x, maxLength); }; }); /* for(var i=0; i