Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 | 1x 5476x 30x 30x 30x 30x 30x 30x 512x 512x 512x 512x 512x 5476x 5476x 5476x 5476x 5476x 5476x 5476x 5476x 5476x 512x 512x 512x 512x 30x 30x 30x 30x 256x 256x 2738x 256x 30x 30x 256x 256x 30x 30x 30x 30x 30x 30x 30x 30x 512x 512x 512x 512x 512x 5476x 5476x 5476x 2738x 512x 30x 256x 2738x 30x 512x 512x 512x 512x 512x 512x 512x 226x 226x 226x 226x 226x 226x 512x 512x 512x 512x 512x 512x 512x 512x 4964x 4964x 4964x 2482x 2482x 512x 512x | /**
* @module Engine.Maps.POV
* Maps actions
*/
export interface POVPoint {
x: number;
y: number;
}
export type POVRay = POVPoint[];
export class POV {
private get getListRayon() {
return this.listRay.map(r => r.map(p => ({
x: Math.abs(p.x),
y: Math.abs(p.y),
})));
}
public static generatePOV(size: number) {
const middle = Math.floor(size / 2);
const pov = new POV(middle + 1);
pov.run();
const baseRays = pov.getListRayon;
const relativeRays = {};
baseRays.forEach(ray => {
const relativeRay1: POVRay = [];
const relativeRay2: POVRay = [];
const relativeRay3: POVRay = [];
const relativeRay4: POVRay = [];
ray.forEach(point => {
const { x, y } = point;
const offsetN = middle - x;
const offsetO = middle - y;
const offsetS = x + middle;
const offsetW = y + middle;
relativeRay1.push({ x: offsetN, y: offsetO });
relativeRay2.push({ x: offsetN, y: offsetW });
relativeRay3.push({ x: offsetS, y: offsetW });
relativeRay4.push({ x: offsetS, y: offsetO });
});
relativeRays[JSON.stringify(relativeRay1)] = relativeRay1;
relativeRays[JSON.stringify(relativeRay2)] = relativeRay2;
relativeRays[JSON.stringify(relativeRay3)] = relativeRay3;
relativeRays[JSON.stringify(relativeRay4)] = relativeRay4;
});
return Object.values(relativeRays);
}
private grid: boolean[][];
private size: number;
private listRay: POVRay[];
private constructor(size: number) {
this.size = size;
this.grid = [];
for (let line = 0; line < this.size; line += 1) {
const arr = [];
for (let column = 0; column < this.size; column += 1) {
arr.push(false);
}
this.grid.push(arr);
}
}
private run() {
const cases: POVRay = [];
for (let i = 0; i < this.size; i += 1) {
cases.push({ x: i, y: this.size - 1 });
cases.push({ x: this.size - 1, y: i });
}
this.listRay = [];
this.generateRay(cases);
let recurse = true;
do {
const testCase = this.recurse(false, this.grid);
Eif (testCase === false) {
recurse = false;
} else {
this.generateRay([testCase]);
}
} while (recurse);
}
private generateRay(cases: POVRay) {
cases.forEach(ray => {
const position_x = ray.x;
const position_y = ray.y;
const line = this.bresenham(0, 0, position_x, position_y, false);
line.push({ x: position_x, y: position_y });
line.forEach(point => {
const x = Math.abs(point.x);
const y = Math.abs(point.y);
if (!this.grid[x][y]) {
this.grid[x][y] = true;
}
});
this.listRay.push(line);
});
}
private recurse(search: any, arr: any[][]): POVPoint | false {
arr.forEach((line, x) => {
line.forEach((col, y) => {
Iif (col === search) {
return [x, y];
}
});
});
return false;
}
/**
* Virtually draw a line from (x1, y1) to (x2, y2) using Bresenham algorithm, returning the coordinates of the points that would belong to the line.
* @param $x1 (Int)
* @param $y1 (Int)
* @param $x2 (Int)
* @param $y2 (Int)
* @return (Array of couples forming the line) Eg: array(array(2,100), array(3, 101), array(4, 102), array(5, 103))
* Public domain Av'tW
*/
private bresenham(x1: number, y1: number, x2: number, y2: number, guaranteeEndPoint = true): POVRay {
const xBegin = x1;
const yBegin = y1;
const xEnd = x2;
const yEnd = y2;
const dots: POVPoint[] = []; // Array of couples, returned array
const steep = Math.abs(y2 - y1) > Math.abs(x2 - x1);
// Swap some coordinateds in order to generalize
// tslint:disable: no-parameter-reassignment
if (steep) {
let tmp = x1;
x1 = y1;
y1 = tmp;
tmp = x2;
x2 = y2;
y2 = tmp;
}
Iif (x1 > x2) {
let tmp = x1;
x1 = x2;
x2 = tmp;
tmp = y1;
y1 = y2;
y2 = tmp;
}
// tslint:enable: no-parameter-reassignment
const deltax = Math.floor(x2 - x1);
const deltay = Math.floor(Math.abs(y2 - y1));
let error = 0;
const deltaerr = deltay / deltax;
let y = y1;
let x;
const ystep = (y1 > y2) ? 1 : -1;
for (x = x1; x < x2; x += 1) {
dots.push(steep ? { x: y, y: x } : { x, y });
error += deltaerr;
if (error >= 0.5) {
y += ystep;
error -= 1;
}
}
Iif (guaranteeEndPoint) {
// Bresenham doesn't always include the specified end point in the result line, add it now.
if (((xEnd - x) * (xEnd - x) + (yEnd - y) * (yEnd - y)) <
((xBegin - x) * (xBegin - x) + (yBegin - y) * (yBegin - y))) {
// Then we're closer to the end
dots.push({ x: xEnd, y: yEnd });
} else {
dots.push({ x: xBegin, y: yBegin });
}
}
return dots;
}
}
|