Created
November 23, 2022 10:21
-
-
Save aolo2/f8ff39316b4580716a88d40474de0edb to your computer and use it in GitHub Desktop.
Example of simple 2 step processing you can run on raw points in a drawing program
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
function rdp_find_max(points, start, end) { | |
const EPS = 0.5; | |
let result = -1; | |
let max_dist = 0; | |
const a = points[start]; | |
const b = points[end]; | |
const dx = b.x - a.x; | |
const dy = b.y - a.y; | |
const dist_ab = Math.sqrt(dx * dx + dy * dy); | |
const sin_theta = dy / dist_ab; | |
const cos_theta = dx / dist_ab; | |
for (let i = start; i < end; ++i) { | |
const p = points[i]; | |
const ox = p.x - a.x; | |
const oy = p.y - a.y; | |
const rx = cos_theta * ox + sin_theta * oy; | |
const ry = -sin_theta * ox + cos_theta * oy; | |
const x = rx + a.x; | |
const y = ry + a.y; | |
const dist = Math.abs(y - a.y); | |
if (dist > EPS && dist > max_dist) { | |
result = i; | |
max_dist = dist; | |
} | |
} | |
return result; | |
} | |
function process_rdp_r(points, start, end) { | |
let result = []; | |
const max = rdp_find_max(points, start, end); | |
if (max !== -1) { | |
const before = process_rdp_r(points, start, max); | |
const after = process_rdp_r(points, max, end); | |
result = [...before, points[max], ...after]; | |
} | |
return result; | |
} | |
function process_rdp(points) { | |
const result = process_rdp_r(points, 0, points.length - 1); | |
result.unshift(points[0]); | |
result.push(points[points.length - 1]); | |
return result; | |
} | |
function process_ewmv(points) { | |
const result = []; | |
const alpha = 0.4; | |
result.push(points[0]); | |
for (let i = 1; i < points.length; ++i) { | |
const p = points[i]; | |
const x = alpha * p.x + (1 - alpha) * result[result.length - 1].x; | |
const y = alpha * p.y + (1 - alpha) * result[result.length - 1].y; | |
result.push({'x': x, 'y': y}); | |
} | |
return result; | |
} | |
function process_stroke(points) { | |
const result0 = process_ewmv(points); | |
const result1 = process_rdp(result0); | |
return result1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment