Last active
October 7, 2024 14:45
-
-
Save raybellis/d6d06ac5927b5cedcea04245586d807f to your computer and use it in GitHub Desktop.
Orthographic projection per https://stackoverflow.com/questions/79061549/project-3d-coordinates-on-2d-plane
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
const p1 = { x: 59197011, y: 42146958, z: 42913136384, sx: 24, sy: 470 }; | |
const p2 = { x: 59197011, y: 21399618, z: 42913136384, sx: 254, sy: 664 }; | |
const p3 = { x: 6735114, y: 33773393, z: 4287356928 , sx: 808, sy: 65 }; | |
const p4 = { x: 26245172, y: 42146958, z: 4293136384, sx: 495, sy: 212 }; | |
function project({x, y, z}) { | |
// use p4 as the origin | |
x -= p4.x; | |
y -= p4.y; | |
z -= p4.z; | |
// and start screen coordinate calculations from there too | |
let sx = p4.sx; | |
let sy = p4.sy; | |
// calculate how much sx / sy change per unit change of x | |
// by looking at the differences between p1 and p4 where | |
// only the X coordinate changes | |
const xscale_x = (p4.sx - p1.sx) / (p4.x - p1.x); | |
const xscale_y = (p4.sy - p1.sy) / (p4.x - p1.x); | |
sx += x * xscale_x; | |
sy += x * xscale_y; | |
// calculate how much sx / sy change per unit change of y | |
// by looking at the differences between p2 and p1 where | |
// only the Y coordinate changes | |
const yscale_x = (p2.sx - p1.sx) / (p2.y - p1.y); | |
const yscale_y = (p2.sy - p1.sy) / (p2.y - p1.y); | |
sx += y * yscale_x; | |
sy += y * yscale_y; | |
// adjustment of screen Y coordinate based on Z axis is TBD | |
// since current coordinates appear incorrect and because | |
// we have no (X, Y) invariant coordinates to calculate | |
// from. The math above indicates that the X coordinate | |
// of P3 *should* be 866 instead of the given 808 | |
return [~~sx, ~~sy]; | |
} | |
console.log('P1 = ', project(p1)); | |
console.log('P2 = ', project(p2)); | |
console.log('P3 = ', project(p3)); | |
console.log('P4 = ', project(p4)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment