简体   繁体   English

给定Z来计算三角形的两个点

[英]Calculate two points of triangle with given Z

I want to calculate the two points of a 3D triangle that have a specified Z coordinate. 我想计算具有指定Z坐标的3D三角形的两个点。 I guess the way to do this would be to somehow create a plane which is perfectly flat with my given Z coordinate and then calculate where the triangle intersects with it (or is this wrong). 我猜想这样做的方法是以某种方式创建一个平面,该平面与我给定的Z坐标完全平整,然后计算三角形与其相交的位置(或者这是错误的)。

If you know how to do this in any way please help. 如果您知道如何执行此操作,请提供帮助。 I have searched for but have not really found anything that seems to work. 我已经搜索过,但还没有真正找到任何可行的方法。

Assuming your triangle is defined by three points where T0 is base left, T1 is base right and T2 is tip. 假设您的三角形由三个点定义,其中T0是左下,T1是右下,T2是尖端。 And a lot of other assumptions about how the triangle is aligned etc: 以及关于三角形如何对齐的许多其他假设:

public bool GetIntersect(Vector3 T0, Vector3 T1, Vector3 T2, float Z, out Vector3 P0, out Vector3 P1)
{
    if (!((Z <= T2.Z) && (Z >= T0.Z) && (Z >= T1.Z)))
        return false;

    var left = T2 - T0;
    left *= 1f / left.Z;

    var right = T2 - T1;
    right * 1f / right.Z;

    P0 = T0 + (left * (Z - T0.Z));
    P1 = T1 + (right * (Z - T1.Z));

    return true;
}

PS; PS; Uppercase in parameter names are bad! 参数名称中的大写字母是错误的! don't do that! 不要那样做! I just don't care to fix my mistake right now :P 我只是不在乎现在要纠正我的错误:P

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

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