-
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.
- Loading branch information
1 parent
955e740
commit 2237df0
Showing
9 changed files
with
296 additions
and
33 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
node_modules | ||
dist | ||
.idea | ||
.DS_STORE |
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
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,131 @@ | ||
import { Vector } from './Vector'; | ||
|
||
export class DensityGrid { | ||
width: number; | ||
height: number; | ||
binSize: number; | ||
xBinCount: number; | ||
yBinCount: number; | ||
bins: number[]; | ||
maxDensity: number; | ||
constructor(width: number, height: number, binSize: number) { | ||
this.width = width; | ||
this.height = height; | ||
this.binSize = binSize; | ||
this.xBinCount = Math.ceil(this.width / this.binSize); | ||
this.yBinCount = Math.ceil(this.height / this.binSize); | ||
this.bins = Array(this.xBinCount * this.yBinCount).fill(0); | ||
this.maxDensity = 0; | ||
} | ||
getDensityAtBin(x: number, y: number): number { | ||
if (x < 0 || x >= this.xBinCount || y < 0 || y >= this.yBinCount) return 0; | ||
return this.bins[Math.floor(y) * this.xBinCount + Math.floor(x)]; | ||
} | ||
getDensityAtPoint(x: number, y: number): number { | ||
return this.getDensityAtBin(x / this.binSize, y / this.binSize); | ||
} | ||
addDensityAtBin(x: number, y: number, density: number): boolean { | ||
if (x >= 0 && x < this.xBinCount && y >= 0 && y < this.yBinCount) { | ||
this.bins[Math.floor(y) * this.xBinCount + Math.floor(x)] += density; | ||
if (this.bins[Math.floor(y) * this.xBinCount + Math.floor(x)] > this.maxDensity) { | ||
this.maxDensity = this.bins[Math.floor(y) * this.xBinCount + Math.floor(x)]; | ||
} | ||
return true; | ||
} | ||
return false; | ||
} | ||
addDensityAtPoint(x: number, y: number, density: number): boolean { | ||
return this.addDensityAtBin(x / this.binSize, y / this.binSize, density); | ||
} | ||
// https://www.geeksforgeeks.org/anti-aliased-line-xiaolin-wus-algorithm/ | ||
addDensityXW(p: Vector, q: Vector, rejectionLimit: number): boolean { | ||
function iPartOfNumber(x: number): number { | ||
return Math.floor(x); | ||
} | ||
function fPartOfNumber(x: number): number { | ||
return x > 0 ? x - iPartOfNumber(x) : x - (iPartOfNumber(x) + 1); | ||
} | ||
function rfPartOfNumber(x: number): number { | ||
return 1 - fPartOfNumber(x); | ||
} | ||
function getDensityAdditions( | ||
x0: number, | ||
y0: number, | ||
x1: number, | ||
y1: number, | ||
): { x: number; y: number; density: number }[] { | ||
let densityAdditions = []; | ||
const steep = Math.abs(y1 - y0) > Math.abs(x1 - x0); | ||
if (steep) { | ||
[x0, y0] = [y0, x0]; | ||
[x1, y1] = [y1, x1]; | ||
} | ||
if (x0 > x1) { | ||
[x0, x1] = [x1, x0]; | ||
[y0, y1] = [y1, y0]; | ||
} | ||
const [dx, dy] = [x1 - x0, y1 - y0]; | ||
const gradient = dy / dx || 1; | ||
let [xpxl1, xpxl2] = [Math.floor(x0), Math.floor(x1)]; | ||
let intersectY = y0; | ||
if (steep) { | ||
for (let x = xpxl1; x <= xpxl2; x++) { | ||
densityAdditions.push({ | ||
x: Math.floor(intersectY), | ||
y: x, | ||
density: rfPartOfNumber(intersectY), | ||
}); | ||
densityAdditions.push({ | ||
x: Math.floor(intersectY) - 1, | ||
y: x, | ||
density: fPartOfNumber(intersectY), | ||
}); | ||
intersectY += gradient; | ||
} | ||
} else { | ||
for (let x = xpxl1; x <= xpxl2; x++) { | ||
densityAdditions.push({ | ||
x: x, | ||
y: Math.floor(intersectY), | ||
density: rfPartOfNumber(intersectY), | ||
}); | ||
densityAdditions.push({ | ||
x: x, | ||
y: Math.floor(intersectY) - 1, | ||
density: fPartOfNumber(intersectY), | ||
}); | ||
intersectY += gradient; | ||
} | ||
} | ||
return densityAdditions; | ||
} | ||
const densityAdditions = getDensityAdditions( | ||
p.x / this.binSize, | ||
p.y / this.binSize, | ||
q.x / this.binSize, | ||
q.y / this.binSize, | ||
); | ||
let isRejected = false; | ||
for (let densityAddition of densityAdditions) { | ||
if ( | ||
this.getDensityAtBin(densityAddition.x, densityAddition.y) + | ||
densityAddition.density > | ||
rejectionLimit | ||
) { | ||
isRejected = true; | ||
break; | ||
} | ||
} | ||
if (!isRejected) { | ||
for (let densityAddition of densityAdditions) { | ||
this.addDensityAtBin( | ||
densityAddition.x, | ||
densityAddition.y, | ||
1 - densityAddition.density, | ||
); | ||
} | ||
return true; | ||
} | ||
return false; | ||
} | ||
} |
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
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,100 @@ | ||
import { BoundingBox } from './BoundingBox'; | ||
import { Vector } from './Vector'; | ||
|
||
export class Quadtree { | ||
boundingBox: BoundingBox; | ||
capacity: number; | ||
points: Vector[]; | ||
divided: boolean; | ||
northeast: Quadtree | null = null; | ||
northwest: Quadtree | null = null; | ||
southeast: Quadtree | null = null; | ||
southwest: Quadtree | null = null; | ||
|
||
constructor(boundingBox: BoundingBox, capacity: number) { | ||
this.boundingBox = boundingBox; | ||
this.capacity = capacity; | ||
this.points = []; | ||
this.divided = false; | ||
} | ||
|
||
insert(x: number, y: number): boolean { | ||
if (!this.boundingBox.contains(x, y)) { | ||
return false; | ||
} | ||
const point = new Vector(x, y); | ||
if (this.points.length < this.capacity) { | ||
this.points.push(point); | ||
return true; | ||
} else { | ||
if (!this.divided) { | ||
this.subdivide(); | ||
} | ||
// if (this.northeast?.insert(point.x, point.y)) { | ||
// return true; | ||
// } | ||
// if (this.northwest?.insert(point.x, point.y)) { | ||
// return true; | ||
// } | ||
// if (this.southeast?.insert(point.x, point.y)) { | ||
// return true; | ||
// } | ||
// if (this.southwest?.insert(point.x, point.y)) { | ||
// return true; | ||
// } | ||
// return false; | ||
return <boolean>( | ||
(this.northeast?.insert(point.x, point.y) || | ||
this.northwest?.insert(point.x, point.y) || | ||
this.southeast?.insert(point.x, point.y) || | ||
this.southwest?.insert(point.x, point.y)) | ||
); | ||
} | ||
} | ||
|
||
subdivide(): void { | ||
const w = new Vector(this.boundingBox.width, 0); | ||
const h = new Vector(0, this.boundingBox.height); | ||
const hw = new Vector(this.boundingBox.halfWidth, 0); | ||
const hh = new Vector(0, this.boundingBox.halfHeight); | ||
const ne = new BoundingBox( | ||
this.boundingBox.min.add(hw), | ||
this.boundingBox.min.add(w.add(hh)), | ||
); | ||
const nw = new BoundingBox( | ||
this.boundingBox.min.copy(), | ||
this.boundingBox.min.add(hw.add(hh)), | ||
); | ||
const se = new BoundingBox( | ||
this.boundingBox.min.add(hw.add(hh)), | ||
this.boundingBox.min.add(w.add(h)), | ||
); | ||
const sw = new BoundingBox( | ||
this.boundingBox.min.add(hh), | ||
this.boundingBox.min.add(hw.add(h)), | ||
); | ||
this.northeast = new Quadtree(ne, this.capacity); | ||
this.northwest = new Quadtree(nw, this.capacity); | ||
this.southeast = new Quadtree(se, this.capacity); | ||
this.southwest = new Quadtree(sw, this.capacity); | ||
this.divided = true; | ||
} | ||
|
||
query(range: BoundingBox, found: Vector[] = []): Vector[] { | ||
if (!this.boundingBox.intersects(range)) { | ||
return found; | ||
} | ||
for (const point of this.points) { | ||
if (range.contains(point.x, point.y)) { | ||
found.push(point); | ||
} | ||
} | ||
if (this.divided) { | ||
this.northeast?.query(range, found); | ||
this.northwest?.query(range, found); | ||
this.southeast?.query(range, found); | ||
this.southwest?.query(range, found); | ||
} | ||
return found; | ||
} | ||
} |
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
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
Oops, something went wrong.