Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add dirt cleanup

  • Loading branch information...
commit 9d24230a1a0fbcb925c45165e04b72aa8e3af68c 1 parent 607704e
@gigasquid authored
Showing with 121 additions and 19 deletions.
  1. +2 −2 index.html
  2. +19 −0 point_poly.js
  3. +100 −17 roomba.js
View
4 index.html
@@ -3,11 +3,11 @@
<title>Roomba Dreams</title>
<script type="text/javascript" src="lib/d3.v2.js"></script>
<script type="text/javascript" src="lib/jquery-1.7.1.min.js"></script>
+ <script type="text/javascript" src="point_poly.js"> </script>
<link rel="stylesheet" type="text/css" href="roomba.css" />
</style>
</head>
<body>
- <script type="text/javascript" src="roomba.js">
- </script>
+ <script type="text/javascript" src="roomba.js"> </script>
</body>
</html>
View
19 point_poly.js
@@ -0,0 +1,19 @@
+function inside_poly(point, vs) {
+ // ray-casting algorithm based on
+ // http://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html
+
+ var x = point[0], y = point[1];
+
+ var inside = false;
+ for (var i = 0, j = vs.length - 1; i < vs.length; j = i++) {
+ var xi = vs[i][0], yi = vs[i][1];
+ var xj = vs[j][0], yj = vs[j][1];
+
+ var intersect = ((yi > y) != (yj > y))
+ && (x < (xj - xi) * (y - yi) / (yj - yi) + xi);
+ if (intersect) inside = !inside;
+ }
+
+ return inside;
+};
+
View
117 roomba.js
@@ -1,6 +1,9 @@
var roomba_data = [[200, 200]];
var tweets = ["(Becomes Self-Aware)"];
var tweet_num = 0;
+var dirt_data = [];
+var new_dirt_data = [];
+var cleaned_dirt = [];
function getTweets() {
var url = 'http://api.twitter.com/1/statuses/user_timeline.json?screen_name=SelfAwareROOMBA&count=200&callback=?'
@@ -14,7 +17,35 @@ function getTweets() {
});
}
-getTweets();
+//getTweets();
+
+function random_from_to(from, to){
+ return Math.floor(Math.random() * (to - from + 1) + from);
+}
+
+function next_position(){
+ return random_from_to((roomba_image + 10), (490 - roomba_image))
+}
+
+function show_tweet(){
+ if (tweets.length > tweet_num +1){
+ tweet_num++;
+ }
+ if (tweets.length <= tweet_num +1){
+ tweet_num = 0;
+ }
+ d3.select(".tweet").text(tweets[tweet_num]);
+}
+
+var dirt_radius = 2;
+var dirt_num = 500;
+function init_dirt(){
+ for (i=0; i<dirt_num; i++){
+ dirt_data.push([random_from_to(10, 490), random_from_to(10,490), i])
+ }
+}
+init_dirt();
+
//Width and height
var w = 500;
@@ -28,8 +59,17 @@ svg.append("rect").attr("width", w).attr("height", h).attr("fill", "#ffffee")
.attr("stroke", "black").attr("stroke-width", "10");
//setup the tweet area
d3.select("body").append("div").attr("class", "tweet");
+//now add the dirt
+
+var dirt = svg.selectAll("dirt").data(dirt_data, function(d){ return d[2];}).enter()
+ .append("circle")
+ .attr("class", "dirt")
+ .attr("cx", function(d) {return d[0];})
+ .attr("cy", function(d) {return d[1];})
+ .attr("r", dirt_radius)
+ .attr("fill", "#996663");
-var roomba = svg.selectAll("roomba").data(roomba_data).enter()
+svg.selectAll("roomba").data(roomba_data).enter()
.append("g")
.append("svg:image")
.attr("class", "roomba")
@@ -42,34 +82,77 @@ var roomba = svg.selectAll("roomba").data(roomba_data).enter()
var tweet = svg.selectAll("g")
.append("text");
-function random_from_to(from, to){
- return Math.floor(Math.random() * (to - from + 1) + from);
-}
+function dirt_clean_up(old_x, old_y, new_x, new_y){
+ cleaned_dirt = [];
+ new_dirt_data = [];
+ var roomba_radius = 50;
+ var poly = [
+ [ old_x, old_y],
+ [old_x, old_y + roomba_radius],
+ [new_x, new_y + roomba_radius],
+ [new_x, new_y]
+ ];
-function next_position(){
- return random_from_to((roomba_image + 10), (490 - roomba_image))
-}
+ // svg.append("circle")
+ // .attr("cx", old_x)
+ // .attr("cy", old_y )
+ // .attr("r", 10)
+ // .attr("fill", "red");
-function showTweet(){
- if (tweets.length > tweet_num +1){
- tweet_num++;
- }
- if (tweets.length <= tweet_num +1){
- tweet_num = 0;
+ // svg.append("circle")
+ // .attr("cx", old_x)
+ // .attr("cy", (old_y + roomba_radius))
+ // .attr("r", 10)
+ // .attr("fill", "red");
+
+ // svg.append("circle")
+ // .attr("cx", new_x)
+ // .attr("cy", (new_y + roomba_radius))
+ // .attr("r", 10)
+ // .attr("fill", "red");
+
+ // svg.append("circle")
+ // .attr("cx", new_x)
+ // .attr("cy", new_y)
+ // .attr("r", 10)
+ // .attr("fill", "red");
+
+ for (i=0; i<dirt_data.length; i++){
+ var dirt_x = dirt_data[i][0];
+ var dirt_y = dirt_data[i][1];
+ if(inside_poly([dirt_x, dirt_y], poly)){
+ cleaned_dirt.push(dirt_data[i]);
+ console.log("cleaned");
+ }else{
+ new_dirt_data.push(dirt_data[i]);
+ }
}
- d3.select(".tweet").text(tweets[tweet_num]);
+ console.log(poly);
+ console.log(cleaned_dirt);
+
+ dirt_data = new_dirt_data;
+ svg.selectAll(".dirt").data(cleaned_dirt, function(d){ return d[2];}).remove();
+
+
}
function move(){
+ var roomba = svg.selectAll("image.roomba");
+ var old_x = parseInt(roomba.attr("x"));
+ var old_y = parseInt(roomba.attr("y"));
var new_x = next_position();
var new_y = next_position();
+
+
svg.selectAll("image.roomba").transition()
.attr("x", new_x)
.attr("y", new_y).duration(1000).delay(100);
- showTweet();
+ dirt_clean_up(old_x, old_y, new_x, new_y);
+
+ show_tweet();
- setTimeout(move, 5000);
+// setTimeout(move, 5000);
}
move();
Please sign in to comment.
Something went wrong with that request. Please try again.