{ "metadata": { "language": "ruby", "name": "", "signature": "sha256:12e4650b0ddfdc23e65f465bbab4831a38c81655fb49b0513b96652849a4aa06" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "code", "collapsed": false, "input": [ "require_relative '../sim'" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "" ], "metadata": {}, "output_type": "pyout", "prompt_number": 1, "text": [ "\"if(window['d3'] === undefined){\\n require.config({paths: {\\n\\t d3: 'http://cdnjs.cloudflare.com/ajax/libs/d3/3.4.4/d3.min'\\n }});\\n}\\n\"" ] }, { "html": [ "" ], "metadata": {}, "output_type": "pyout", "prompt_number": 1, "text": [ "\"define(\\\"Viewer\\\", function(){\\n return {\\n init_svg: function(svg){\\n svg.attr(\\\"class\\\", \\\"viz\\\").style({\\n position: 'relative',\\n width: 400,\\n height: 400\\n });\\n\\n svg.append(\\\"line\\\").attr({\\\"class\\\": \\\"L1\\\"});\\n svg.append(\\\"line\\\").attr({\\\"class\\\": \\\"L2\\\"});\\n svg.selectAll(\\\"circle\\\")\\n .data([1,2])\\n .enter()\\n .append(\\\"circle\\\")\\n .attr({\\n \\\"class\\\": function(d){return \\\"J\\\"+d;},\\n \\\"r\\\": 5,\\n \\\"fill\\\": function(d){return [\\\"#8dd3c7\\\", \\\"#bebada\\\"][d-1];}\\n });\\n\\n svg.selectAll(\\\"line\\\").attr({\\n 'stroke': \\\"rgb(0,0,0)\\\",\\n 'stroke-width': 3\\n });\\n },\\n draw: function(parent, q){\\n var L1=1, L2=1;\\n var sin = Math.sin, cos = Math.cos, PI = Math.PI;\\n var height = 200, rate=100;\\n var s2px = function(x_val){return 200 + x_val*rate;},\\n s2py = function(y_val){return height + y_val*rate;};\\n\\n parent.select(\\\".J1\\\").attr({\\n cx: s2px(L1*sin(q[0])),\\n cy: s2py(L1*cos(q[0]))\\n });\\n\\n parent.select(\\\".J2\\\").attr({\\n cx: s2px(L1*sin(q[0]) + L2*sin(q[1])),\\n cy: s2py(L1*cos(q[0]) + L2*cos(q[1]))\\n });\\n\\n parent.select(\\\".L1\\\")\\n .attr({\\n x1: s2px(0),\\n y1: s2py(0),\\n x2: s2px(L1*sin(q[0])),\\n y2: s2py(L1*cos(q[0]))\\n });\\n\\n parent.select(\\\".L2\\\")\\n .attr({\\n x1: s2px(L1*sin(q[0])),\\n y1: s2py(L1*cos(q[0])),\\n x2: s2px(L1*sin(q[0]) + L2*sin(q[1])),\\n y2: s2py(L1*cos(q[0]) + L2*cos(q[1]))\\n });\\n }};\\n});\\n\\nrequire([\\\"d3\\\", \\\"underscore\\\", \\\"widgets/js/widget\\\", \\\"Viewer\\\"], function(d3, _, WidgetManager, Viewer){\\n // Define the DatePickerView\\n var SimView = IPython.DOMWidgetView.extend({\\n render: function(){\\n this.$el.attr('width', 700);\\n this.$el.attr('height', 700);\\n },\\n\\n update: function() {\\n return SimView.__super__.update.apply(this);\\n },\\n\\n initialize: function(parameters){\\n SimView.__super__.initialize.apply(this, [parameters]);\\n\\n //*** init view ***\\n this.$el.addClass('widget-hbox');\\n var dom = document.createElementNS(\\\"http://www.w3.org/2000/svg\\\", \\\"svg:svg\\\");\\n this.$el.append(dom);\\n var svg = d3.select(dom);\\n Viewer.init_svg(svg);\\n\\n // params\\n var buff_size = 50;\\n var duration = 10;\\n\\n // arr is ret value from p2\\n var main = _.bind(function(arr){\\n var dfd = new $.Deferred();\\n\\n var p2 = (function(){\\n var deferred = new $.Deferred();\\n\\n if(arr.length == 0){\\n deferred.resolve();\\n }else{\\n var cnt = 0;\\n var loop = function(){\\n Viewer.draw(svg, arr[cnt]);\\n\\n if(cnt == buff_size-1){\\n deferred.resolve();\\n }else{\\n cnt+=1;\\n window.setTimeout(loop, duration);\\n }\\n };\\n loop();\\n }\\n return deferred.promise();\\n })();\\n\\n var p1 = (_.bind(function(){\\n var deferred = new $.Deferred();\\n\\n this.model.send({\\n num: buff_size\\n });\\n\\n // send custom message through Comm. See the following article to learn more:\\n // http://ipython.org/ipython-doc/dev/development/messaging.html\\n this.model.on('msg:custom', $.proxy(function(content){\\n if(content.error != true)\\n deferred.resolve(content.new_arr);\\n else\\n deferred.reject();\\n }), this);\\n\\n return deferred.promise();\\n }, this))();\\n\\n $.when(p1, p2).then(\\n main,\\n function(err1, err2){\\n console.log(\\\"Visualizer stopped.\\\");\\n }\\n );\\n\\n dfd.resolve();\\n return dfd.promise();\\n }, this);\\n\\n main([]);\\n }\\n });\\n \\n WidgetManager.register_widget_view('SimView', SimView);\\n});\\n\"" ] }, { "metadata": {}, "output_type": "pyout", "prompt_number": 1, "text": [ "true" ] } ], "prompt_number": 1 }, { "cell_type": "code", "collapsed": false, "input": [ "sim = Simulator.new([Math::PI/2,Math::PI/2,0.0001,0])" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 3, "text": [ "#" ] } ], "prompt_number": 3 }, { "cell_type": "code", "collapsed": false, "input": [ "sim.run" ], "language": "python", "metadata": {}, "outputs": [ { "": "Y2ZmMTM5MTAxYWZiMGYyYzFiNWFhNmE0ODZhMmU1YTg2ZmYwYTk3YmFlMTQ1YmJjY2MwODY1YjYxYjRlMGIxMw==", "metadata": {}, "output_type": "pyout", "prompt_number": 3, "text": [ "#\"username\", \"msg_id\"=>\"C3B5EC170020447082D291EDEA74F335\", \"msg_type\"=>\"execute_request\", \"session\"=>\"7614508EBCA448E88C0DCB8DE1BDF504\"}>, @pub_socket=#, @name=\"PUB\", @int_cache=nil, @longlong_cache=nil, @more_parts_array=[], @option_lookup=[nil, nil, nil, nil, 1, 2, 2, 2, 0, 0, nil, 0, 0, 1, 0, 0, 0, 0, 0, 0, nil, 0, 1, 0, 0, 0, nil, 0, 0, nil, nil, 0, 2, 0, 0, 0, 0, 0, 0, 0, nil, nil, 0, 0, 0, 2, 2, 0, 2, 2, 2, 0, 0, 0, 0, 2]>, @msg_callback=#>, @msg_callback=#>" ] } ], "prompt_number": 3 }, { "cell_type": "code", "collapsed": false, "input": [ "sim.save_movie(3000)" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "\n", " \n", " \n", " \n", " \n", "
\n", "
\n", "
\n", " \n", " \n", " \n", "
\n", "
\n", " \n", "\n" ], "metadata": {}, "output_type": "pyout", "prompt_number": 4, "text": [ "\"\\n \\n \\n \\n \\n
\\n
\\n
\\n \\n \\n \\n
\\n
\\n \\n\\n\"" ] } ], "prompt_number": 4 }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }