简体   繁体   English

C#Vector3得到百分比的剩余面积

[英]C# Vector3's get the remaining area in percentage

I have two Vector3, that each represent the size of an area. 我有两个Vector3,每个Vector3代表一个区域的大小。 First one is the total amount of the area. 第一个是该区域的总量。 Second one is just a part of the first one. 第二个只是第一个的一部分。 How do i get the percentage, that remains of the total area, when i subtract the smaller area? 当我减去较小的面积时,如何获得占总面积的百分比?

If both represents size (they are positive in components by definition) and fully interesects (one fully lies in borders of another), then you just need to calculate percentage of their volume difference: 如果两者都表示大小(按定义它们在分量上为正)并且完全相交(一个完全位于另一个的边界内),那么您只需要计算它们的体积差的百分比即可:

|v1.volume - v2.volume|/max(v1.volume, v2.volume)

So in code it will look like this: 因此,在代码中它将如下所示:

double v1Volume = v1.x * v1.y * v1.z;
double v2Volume = v2.x * v2.y * v2.z;
double percentageDiff = 100 * Math.Abs(v1Volume - v2Volume) / Math.Max(v1Volume, v2Volume);

This way, it doesn't even matter which one small and which one big, just put both in this function and it will show correct answer. 这样一来,哪一个大小都没有关系,只要将两者都放在此函数中,它将显示正确的答案。

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

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