简体   繁体   English

垂直化三角形

[英]Verticalize Triangle

I have a triangle where two points have the same Z-Value and one has a different Value. 我有一个三角形,其中两个点具有相同的Z值而一个点具有不同的值。 Now I want to transform the point with the different Z-Value, so that it optically generates a "vertical" Triangle. 现在,我想用不同的Z值变换该点,以便它光学地生成一个“垂直”三角形。 Assuming point C is the one with the different height-Value, I somehow need to move the X and Y Coordinates of point C orthogonal to the Difference-Vector of A and B, until they vertically line up, eg the slope is exactly 90 degrees. 假设点C是具有不同高度值的点,我不知何故需要移动点C的X和Y坐标,使其正交于A和B的差矢量,直到它们垂直对齐,例如,坡度正好是90度。 But unfortunately, I am a complete idiot concerning rotations and stuff. 但是不幸的是,我是一个关于轮换和东西的白痴。 Could you give me any hints how to solve that? 您能给我一些解决方法的提示吗?

The code I need it for is written in C++, but a simple pseudo-code would be enough :) 我需要的代码是用C ++编写的,但是简单的伪代码就可以了:)

But preferably a rather quick way, because it has to be called up to 700000 times per player, per chunk load 但最好是一种相当快速的方法,因为每位玩家每次块加载最多必须调用700000次

Let's say you have points ABC, and Az == Bz, and z represents the vertical direction. 假设您有ABC点,Az == Bz,z代表垂直方向。

First, project the x,y co-ordinates of C onto the line between A and B in 2D: 首先,将C的x,y坐标投影到2D中A和B之间的线上:

// 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 for length == 0 to avoid div by zero
AB.x /= length;
AB.y /= length; // note: you could save a division by dividing dot by length instead

// project C onto AB:
AC.x = C.x - A.x;
AC.y = C.y - A.y;
// this gives us how far along the line AB the 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 as B.z

Next find the 3D distance between the projected point and C, which will be the vertical height above the AB line of the new point, if the triangle were rotated into a vertical position using AB as a hinge: 接下来,如果使用AB作为铰链将三角形旋转到垂直位置,则找到投影点和C之间的3D距离,该距离将是新点的AB线上方的垂直高度:

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 the z value: 设置z值:

newC.z += height;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM