forked from CodingTrain/website-archive
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added quadTree in Processing (only Part 1) as (CC_98_QuadTree) and re…
…named quadTree for js (CC_98_QuadTree) to (CC_98_QuadTree.js).
- Loading branch information
1 parent
3f17942
commit 3adc944
Showing
10 changed files
with
179 additions
and
0 deletions.
There are no files selected for viewing
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// Daniel Shiffman | ||
// http://codingtra.in | ||
// http://patreon.com/codingtrain | ||
//Videos | ||
//Part 1: | ||
//https://www.youtube.com/watch?v=OJxEcs0w_kE | ||
//Part 2: | ||
//https://www.youtube.com/watch?v=QQx_NmCIuCY | ||
|
||
//This code is only part 1 video of the challenge. | ||
|
||
Quadtree qtree; | ||
public void setup (){ | ||
|
||
size(400 ,400); | ||
background(0); | ||
qtree = new Quadtree (new Rectangle (width/2 , height/2 , width/2 , height/2) , 4); | ||
} | ||
|
||
public void draw (){ | ||
if(mousePressed){ | ||
for (int i = 0 ; i < 5 ; i++){ | ||
qtree.insert(new Point ( mouseX +random(-5,5) , mouseY+random(-5,5))); | ||
} | ||
} | ||
qtree.show(); | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// Daniel Shiffman | ||
// http://codingtra.in | ||
// http://patreon.com/codingtrain | ||
//Videos | ||
//Part 1: | ||
//https://www.youtube.com/watch?v=OJxEcs0w_kE | ||
//Part 2: | ||
//https://www.youtube.com/watch?v=QQx_NmCIuCY | ||
|
||
//This code is only part 1 video of the challenge. | ||
|
||
class Point{ | ||
double x; | ||
double y ; | ||
|
||
public Point (double x , double y){ | ||
this.x = x; | ||
this.y = y; | ||
} | ||
|
||
public double getX(){ | ||
return this.x; | ||
} | ||
public double getY(){ | ||
return this.y; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// Daniel Shiffman | ||
// http://codingtra.in | ||
// http://patreon.com/codingtrain | ||
//Videos | ||
//Part 1: | ||
//https://www.youtube.com/watch?v=OJxEcs0w_kE | ||
//Part 2: | ||
//https://www.youtube.com/watch?v=QQx_NmCIuCY | ||
|
||
//This code is only part 1 video of the challenge. | ||
|
||
|
||
class Rectangle { | ||
double x; | ||
double y; | ||
double height ; | ||
double width; | ||
|
||
public Rectangle (double x , double y , double height , double width){ | ||
this.x = x; | ||
this.y = y; | ||
this.height = height; | ||
this.width = width; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
// Daniel Shiffman | ||
// http://codingtra.in | ||
// http://patreon.com/codingtrain | ||
//Videos | ||
//Part 1: | ||
//https://www.youtube.com/watch?v=OJxEcs0w_kE | ||
//Part 2: | ||
//https://www.youtube.com/watch?v=QQx_NmCIuCY | ||
|
||
//This code is only part 1 video of the challenge. | ||
|
||
import java.util.ArrayList; | ||
|
||
class Quadtree { | ||
Rectangle boundry; | ||
int capacity; // max no. of points | ||
ArrayList <Point> points ; | ||
boolean divide = false; | ||
Quadtree northeast; | ||
Quadtree northwest; | ||
Quadtree southeast; | ||
Quadtree southwest; | ||
|
||
public Quadtree (Rectangle rect , int cap){ | ||
this.boundry = rect; | ||
this.capacity = cap; | ||
points = new ArrayList<Point>(); | ||
} | ||
|
||
public boolean contains (Point p){ | ||
return p.x <= this.boundry.x+this.boundry.width && | ||
p.x >= this.boundry.x-this.boundry.width && | ||
p.y <= this.boundry.y+this.boundry.height && | ||
p.y >= this.boundry.y-this.boundry.height ; | ||
} | ||
|
||
public boolean insert (Point p){ | ||
if (!this.contains(p)) return false; //<>// | ||
if (this.points.size() < this.capacity ){ | ||
this. points.add(p); | ||
return true; | ||
} | ||
else { | ||
if (!this.divide) { | ||
subDivide(); | ||
} | ||
if (this.northeast.insert(p)){ | ||
return true; | ||
} | ||
else if (this.northwest.insert(p)){ | ||
return true; | ||
} | ||
else if (this.southeast.insert(p)){ | ||
return true; | ||
} | ||
else if(this.southwest.insert(p)){ | ||
return true; | ||
} | ||
else return false; //<>// | ||
} | ||
} | ||
public void subDivide (){ | ||
double x = this.boundry.x; | ||
double y = this.boundry.y; | ||
double w = this.boundry.width; | ||
double h = this.boundry.height; | ||
this.northeast = new Quadtree(new Rectangle (x+w/2 , y-h/2 , w/2 , h/2),capacity); | ||
this.northwest = new Quadtree(new Rectangle (x-w/2 , y-h/2 , w/2 , h/2),capacity); | ||
this.southeast = new Quadtree(new Rectangle (x+w/2 , y+h/2 , w/2 , h/2),capacity); | ||
this.southwest = new Quadtree(new Rectangle (x-w/2 , y+h/2 , w/2 , h/2),capacity); | ||
this.divide = true; | ||
} | ||
public void show (){ | ||
stroke(255); | ||
strokeWeight(1); | ||
noFill(); | ||
rectMode(CENTER); | ||
rect ((float)this.boundry.x , (float)this.boundry.y , (float)this.boundry.width *2 , (float)this.boundry.height*2); | ||
if (this.divide){ | ||
this.northeast.show(); | ||
this.northwest.show(); | ||
this.southeast.show(); | ||
this.southwest.show(); | ||
} | ||
/* (for showing points) | ||
for (Point p:points){ | ||
strokeWeight(4); | ||
point(p.x , p.y); | ||
} | ||
*/ | ||
|
||
} | ||
|
||
|
||
|
||
} |