简体   繁体   English

如何在 3D 平面中获得随机点

[英]How do you get a random point in a 3D plane

I need to get a random point in a plane.我需要在飞机上随机取一个点。 Let's say we have a quad and know the positions of the four vertices that make it up.假设我们有一个四边形并且知道组成它的四个顶点的位置。 How do I get a random point that's within that quad?如何获得该四边形内的随机点?

Given a quad like:给定一个四边形:

pD ---- pC
|       |
|       |
|       |
pA ---- pB

You can get a random point by getting a random point within that normalized square and use the A-to-B and A-to-D vectors as a coordinate basis.您可以通过在该归一化正方形内获取一个随机点并使用 A-to-B 和 A-to-D 向量作为坐标基础来获得一个随机点。

In practice:在实践中:

// gets a value between 0.0 and 1.0
float randomVal();

vec3 point_in_quad(vec3 pA, vec3 pB, vec3 pC, vec3 pD) {
  return pA + (pB - pA) * randomVal() + (pD - pA) * randomVal();
}

If you assume convexity, you can do it with:如果你假设凸性,你可以这样做:

// gets a value between 0.0 and 1.0
float randomVal();

vec3 point_in_quad(vec3 pA, vec3 pB, vec3 pC, vec3 pD) 
{
    vec3 ab =  pA + (pB - pA) * randomVal();
    vec3 dc =  pD + (pC - pD) * randomVal();
    vec3 r =  ab + (dc - ab) * randomVal();
    
    return r;
}

Basically, pick two randoms points.基本上,选择两个随机点。 One on AB and one on CD. AB 一张,CD 一张。 Then pick a point randomly in between those two.然后在这两者之间随机选择一个点。

Credits to @frank for the framework.归功于 @frank 的框架。

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

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