-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathquadTree.h
More file actions
66 lines (59 loc) · 2.27 KB
/
Copy pathquadTree.h
File metadata and controls
66 lines (59 loc) · 2.27 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
#pragma once
#include <bits/stdc++.h>
using namespace std;
// Define a class to represent 2D points.
class Point
{
public:
double x, y;
Point(double x = 0, double y = 0) : x(x), y(y) {}
bool operator==(const Point & p){
if(this->x==p.x && this->y==p.y) return true;
return false;
}
};
// Define a class to represent rectangles.
class Rectangle
{
public:
double x, y, w, h; // w = width, h = height
Rectangle(double x, double y, double w, double h) : x(x), y(y), w(w), h(h) {}
bool contains(const Point &); // Method to check if a given Point is contained within this Rectangle.
bool intersects(const Rectangle &); // Method to check if this Rectangle intersects with another Rectangle.
};
// Define a class for a quadtree.
class quadTree
{
private:
void knnSearchRecursive(const Point &target, int k, vector<Point> &nearestPoints);
public:
int capacity; // Maximum number of points a node can hold.
bool divided; // Indicates if the quadTree has been divided into sub-quadrants.
Rectangle boundary; // The boundary of the quadTree.
vector<Point> points; // Points contained within quadTree node.
// Pointers to sub-quadrants.
quadTree *northwest;
quadTree *northeast;
quadTree *southwest;
quadTree *southeast;
public:
// Constructor for a quadTree node.
quadTree(const Rectangle &boundary, int capacity) : boundary(boundary), capacity(capacity), divided(false), northwest(nullptr), northeast(nullptr), southwest(nullptr), southeast(nullptr) {}
// Method to subdivide the current quadTree node into four sub-quadrants.
void subdivide();
// Method to insert a Point into the quadTree.
bool insert(const Point &);
// Method to perform a range query and retrieve points within a Rectangle.
vector<Point> rangeQuery(Rectangle &);
void display();
// Method to create a quadTree from Bulk Loading Algorithm.
void bulkLoadquadTree(vector<Point> &);
// Method to perform a k-Nearest Neighbor search and return the k nearest points to a target Point.
vector<Point> knnSearch(const Point &query, int k);
// Destructor
~quadTree();
//method to know whether the point is present
bool search(Point p);
// method to get height
int getHeight();
};