简体   繁体   中英

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. 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).

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. 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; Uppercase in parameter names are bad! don't do that! I just don't care to fix my mistake right now :P

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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