forked from aimacode/aima-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontinuousworld.js
More file actions
71 lines (71 loc) · 2.06 KB
/
continuousworld.js
File metadata and controls
71 lines (71 loc) · 2.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
var latest_output_area ="NONE"; // Jquery object for the DOM element of output area which was used most recently
function handle_output(out, block){
var output = out.content.data["text/html"];
latest_output_area.html(output);
}
function polygon_complete(canvas, vertices){
latest_output_area = $(canvas).parents('.output_subarea');
var world_object_name = canvas.dataset.world_name;
var command = world_object_name + ".handle_add_obstacle(" + JSON.stringify(vertices) + ")";
console.log("Executing Command: " + command);
var kernel = IPython.notebook.kernel;
var callbacks = { 'iopub' : {'output' : handle_output}};
kernel.execute(command,callbacks);
}
var canvas , ctx;
function drawPolygon(array) {
ctx.fillStyle = '#f00';
ctx.beginPath();
ctx.moveTo(array[0][0],array[0][1]);
for(var i = 1;i<array.length;++i)
{
ctx.lineTo(array[i][0], array[i][1]);
}
ctx.closePath();
ctx.fill();
}
var pArray = new Array();
function getPosition(obj,event) {
canvas = obj;
ctx = canvas.getContext('2d');
var x = new Number();
var y = new Number();
x = event.pageX;
y = event.pageY;
x -= $(canvas).offset().left;
y -= $(canvas).offset().top;
drawPoint(x,y);
//draw dot
if(pArray.length>1)
{
drawPoint(pArray[0][0],pArray[0][1]);
}
//check overlap
if(ctx.isPointInPath(x, y) && (pArray.length>1)) {
//Do something
drawPolygon(pArray);
polygon_complete(canvas,pArray);
}
else {
var point = new Array();
point.push(x,y);
pArray.push(point);
}
}
function drawPoint(x, y) {
ctx.beginPath();
ctx.arc(x, y, 5, 0, Math.PI*2);
ctx.fillStyle = '#00f';
ctx.fill();
ctx.closePath();
}
function initalizeObstacles(objects) {
canvas = $('canvas.main-robo-world').get(0);
ctx = canvas.getContext('2d');
$('canvas.main-robo-world').removeClass('main-robo-world');
for(var i=0;i<objects.length;++i) {
drawPolygon(objects[i]);
}
pArray.length = 0;
}
initalizeObstacles(all_polygons);