////////////////////////////////////////////// // Chart creation code goes here // ////////////////////////////////////////////// var sampleData = [ { "x": 1, "y": 5 }, { "x": 20, "y": 20 }, { "x": 40, "y": 10 }, { "x": 60, "y": 40 }, { "x": 80, "y": 5 }, { "x": 100, "y": 60 } ]; function drawLine () { /* ================= settings ======================= */ var vis = d3.select("#graph"); var xRange = d3.scale.linear() .range([40, 400]) .domain([ d3.min(sampleData, function (d) {return (d.x);}), d3.max(sampleData, function (d) {return d.x;}) ]); var yRange = d3.scale.linear() .range([400, 40]) .domain([ d3.min(sampleData, function (d) {return d.y;}), d3.max(sampleData, function (d) {return d.y;}) ]); var xAxis = d3.svg.axis().scale(xRange); var yAxis = d3.svg.axis().scale(yRange).orient("left"); vis.append("svg:g").call(xAxis).attr("transform", "translate(0,400)"); vis.append("svg:g").call(yAxis).attr("transform", "translate(40,0)"); /*@url: http://code.tutsplus.com/tutorials/building-a-multi-line-chart-using-d3js--cms-22935*/ var lineGen = d3.svg.line() .x(function(sampleData) { return xRange(sampleData.x); }) .y(function(sampleData) { return yRange(sampleData.y); }); vis.append('svg:path') .attr('d', lineGen(sampleData)) .attr('stroke', 'green') .attr('stroke-width', 2) .attr('fill', 'none'); } //////////////////////////////////////////////////////// // FUNCTIONS TO MOVE AROUND THE ORIGIN // //////////////////////////////////////////////////////// //SETTINGS var moves = 0; var dir = 'r'; var coords = [{'x':0, 'y':0}]; var tmp = {'x': 0, 'y':0}; $('#generate-numbers').click(function() { var max = $('#max-num').val(); //variable dir gives the position of the last movement direction to be able //to determine which next direction to take. /* * u(up) => move_left * l(left) => move_down * d(down) => move_right * r(right) => move_up ***** default is r(right) so that i can choose to go up */ while(moves <= max){ switch(dir){ case 'r': up(); dir = 'u'; break; case 'u': left(); dir = 'l'; break; case 'l': down(); dir = 'd'; break; case 'd': right(); dir = 'r'; break; default: alert("wrong option. Please, start all over"); return ; } ++moves; } sampleData = coords; $('#graph').empty(); drawLine(); }) function left (argument) { do{ console.log("l_tmpXY (" + tmp.x + " " + tmp.y + ") moves " + moves) --tmp.x; ++moves; coords.push({ 'x': tmp.x, 'y': tmp.y }); }while(!isPrime(moves)); } function right (argument) { do{ console.log("r_tmpXY (" + tmp.x + " " + tmp.y + ") moves " + moves) ++tmp.x; ++moves; coords.push({ 'x': tmp.x, 'y': tmp.y }); }while(!isPrime(moves)); } function up (argument) { do{ console.log("u_tmpXY (" + tmp.x + " " + tmp.y + ") moves " + moves) ++tmp.y; ++moves; coords.push({ 'x': tmp.x, 'y': tmp.y }); }while(!isPrime(moves)); } function down (argument) { do{ console.log("d_tmpXY (" + tmp.x + " " + tmp.y + ") moves " + moves) --tmp.y; ++moves; coords.push({ 'x': tmp.x, 'y': tmp.y }); }while(!isPrime(moves)); } function isPrime (argument) { } function isPrime(n) { // If n is less than 2 or not an integer then by definition cannot be prime. if (n < 2) {return false} if (n != Math.round(n)) {return false} // Now assume that n is prime, we will try to prove that it is not. var isPrime = true; // Now check every whole number from 2 to the square root of n. If any of these divides n exactly, n cannot be prime. for (var i = 2; i <= Math.sqrt(n); i++) { if (n % i == 0) {isPrime = false} } // Finally return whether n is prime or not. return isPrime; }