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 Part 2 of QuadTree in separate Folder as CC_98_QuadTree_Part_2 .
- Loading branch information
1 parent
3adc944
commit 6c4e638
Showing
4 changed files
with
212 additions
and
0 deletions.
There are no files selected for viewing
45 changes: 45 additions & 0 deletions
45
CodingChallenges/CC_98_QuadTree_Part_2/CC_98_QuadTree_Part_2.pde
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,45 @@ | ||
// 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 part 2 video of the challenge. | ||
|
||
Quadtree qtree; | ||
public void setup (){ | ||
size(400 ,400); | ||
background(0); | ||
Rectangle boundry =new Rectangle (width/2 , height/2 , width/2 , height/2); | ||
qtree = new Quadtree (boundry , 4); | ||
for (int i = 0 ; i < 300 ; i++){ | ||
float x = randomGaussian(width /2, width / 8); | ||
float y = randomGaussian(height / 2, height / 8); | ||
Point p = new Point (x,y); | ||
qtree.insert(p); | ||
} | ||
|
||
} | ||
|
||
public void draw (){ | ||
background(0); | ||
qtree.show(); | ||
|
||
ArrayList <Point> points = new ArrayList <Point> (); | ||
stroke(0 , 255 , 0); | ||
rectMode(CENTER); | ||
Rectangle range =new Rectangle (mouseX , mouseY , 25,25); | ||
strokeWeight(1); | ||
rect((float)range.x,(float) range.y,(float) range.width * 2, (float)range.height * 2); | ||
qtree.query(range , points); | ||
for (Point p : points) { | ||
strokeWeight(4); | ||
point((float)p.x, (float)p.y); | ||
} | ||
} | ||
public float randomGaussian(float min , float max){ | ||
return min + randomGaussian() * (max - min); | ||
} |
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 part 2 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,37 @@ | ||
// 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 part 2 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; | ||
} | ||
public boolean intersects(Rectangle range) { | ||
return !(range.x - range.width> this.x + this.width|| | ||
range.x + range.width< this.x - this.width|| | ||
range.y - range.height> this.y + this.height|| | ||
range.y + range.height< this.y - this.height); | ||
} | ||
public boolean contains (Point p){ | ||
return p.x <= this.x+this.width && | ||
p.x >= this.x-this.width && | ||
p.y <= this.y+this.height && | ||
p.y >= this.y-this.height ; | ||
} | ||
} |
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,102 @@ | ||
// 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 part 2 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 insert (Point p){ | ||
if (!this.boundry.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 query (Rectangle range , ArrayList <Point> points){ | ||
|
||
if (range.intersects(this.boundry)){ | ||
for (int i = 0 ; i < this.points.size() ; i++){ | ||
if (range.contains (this.points.get(i))) | ||
points.add(this.points.get(i)); | ||
} | ||
if (this.divide){ | ||
this.northeast.query(range , points); | ||
this.northwest.query(range , points); | ||
this.southeast.query(range , points); | ||
this.southwest.query(range , points); | ||
} | ||
} | ||
|
||
} | ||
|
||
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(2); | ||
stroke(255); | ||
point((float)p.x ,(float) p.y); | ||
} | ||
} | ||
} |