c++ - Verticalize Triangle -
i have triangle 2 points have same z-value , 1 has different value. want transform point different z-value, optically generates "vertical" triangle. assuming point c 1 different height-value, somehow need move x , y coordinates of point c orthogonal difference-vector of , b, until vertically line up, e.g. slope 90 degrees. unfortunately, complete idiot concerning rotations , stuff. give me hints how solve that?
the code need written in c++, simple pseudo-code enough :)
but preferably rather quick way, because has called 700000 times per player, per chunk load
let's have points b c, , a.z == b.z, , z represents vertical direction.
first, project x,y co-ordinates of c onto line between , b in 2d:
// find normalized ab vector: ab.x = b.x - a.x; ab.y = b.y - a.y; length = sqrt(ab.x * ab.x + ab.y * ab.y); // test length == 0 avoid div 0 ab.x /= length; ab.y /= length; // note: save division dividing dot length instead // project c onto ab: ac.x = c.x - a.x; ac.y = c.y - a.y; // gives how far along line ab projected point is: dot = (ac.x * ab.x) * (ac.y * ab.y); newc.x = a.x + (ab.x * dot); newc.y = a.y + (ab.y * dot); newc.z = a.z; // same b.z
next find 3d distance between projected point , c, vertical height above ab line of new point, if triangle rotated vertical position using ab hinge:
newcc.x = c.x - newc.x; newcc.y = c.y - newc.y; newcc.z = c.z - newc.z; height = sqrt((newcc.x * newcc.x) + (newcc.y * newcc.y) + (newcc.z * newcc.z));
set z value:
newc.z += height;