简体   繁体   English

使用 c# 创建 3D 矩形

[英]create 3D rectangle using c#

I read about rectangle structure in c# and the intersection function in it在 c# 和交叉点 function 中阅读了有关矩形结构的信息

My Question is: how to custom it such that I can have a 3D rectanlge, have x,y,z coordinates我的问题是:如何自定义它,以便我可以拥有 3D 矩形,拥有 x、y、z 坐标

and get it intersection with another one?并让它与另一个相交?

Any idea任何想法

Just create your own.只需创建自己的。 Here are some ideas:这里有一些想法:

  • a 3D rectangle not only has a width and a height, but also a plane一个 3D 矩形不仅有宽度和高度,还有一个平面
  • planes can be described with a normal vector and a point (origin)平面可以用法线向量和点(原点)来描述
  • the origin would be similar to the (x, y) in the 2D rectangle, that is, the "upper left" point, but any would do原点类似于 2D 矩形中的 (x, y),即“左上”点,但任何都可以
  • intersecting with another rectangle could be as easy as intersecting the two plains and then checking to see if the intersection line "cuts" any of the rectangles与另一个矩形相交可能就像将两个平原相交然后检查相交线是否“切割”任何矩形一样简单
  • there are tons of math related websites to check for the formulas on how to do this有大量与数学相关的网站可以检查有关如何执行此操作的公式
  • chances are pretty good, that in your application you won't need to do this in an optimized manner.机会非常好,在您的应用程序中,您不需要以优化的方式执行此操作。 Really.真的。 Just code it already and try it out.只需编写代码并尝试一下。 You can optimize later.您可以稍后进行优化。

EDIT:编辑:

Wait.等待。 On second thoughts: An origin, a height, a width and a normal vector won't really cut it, since you don't have a sense of "up" as you do in 2D.再想一想:原点、高度、宽度和法线向量不会真正削减它,因为你没有像在 2D 中那样“向上”的感觉。

So, scratch that.所以,从头开始。 Thinking about it reveals that the width and the height in 2D are actually vectors two, except that their direction is implied: Width is the length of a vector in x direction, Height is the length of a vector in y direction.仔细想想,二维中的宽度和高度实际上是向量二,只是它们的方向是隐含的:宽度是向量在x方向的长度,高度是向量在y方向的长度。

So, model your rectangle like this:所以,model 你的矩形是这样的:

  • a point ( Origin )一个点( Origin
  • a vector Width (this is often called u in maths)一个向量Width (这在数学中通常称为u
  • a vector Height (this is often called v in maths)向量Height (这在数学中通常称为v
  • the normal vector is not necessary anymore since it is can be calculated by the vectorial product of Width x Height不再需要法线向量,因为它可以通过Width x Height的向量积来计算

The three other points of your rectangle can then be calculated as:然后可以将矩形的其他三个点计算为:

  • Origin + Width
  • Origin + Width + Height
  • Origin + Height

The rectangle class you have linked to models a 2D rectangle (I don't know what a 3D rectangle would be, BTW).您链接到的矩形 class 为二维矩形建模(我不知道 3D 矩形是什么,顺便说一句)。

Pretty much the whole System.Drawing namespace deals with 2D, so you can't customise it that way.几乎整个System.Drawing命名空间都处理 2D,因此您不能以这种方式自定义它。

The System.Drawing parent namespace contains types that support basic GDI+ graphics functionality. System.Drawing 父命名空间包含支持基本 GDI+ 图形功能的类型。 Child namespaces support advanced two-dimensional and vector graphics functionality, advanced imaging functionality, and print-related and typographical services.子命名空间支持高级二维和矢量图形功能、高级成像功能以及与打印相关的印刷服务。

(emphasis mine) (强调我的)

(about the intersection function) (关于交集函数)

You cannot create such a function.您不能创建这样的 function。

The intersecting function of 2 rectangles in 2D is interesting because it returns you a third rectangle (than can be empty). 2D 中 2 个矩形的相交 function 很有趣,因为它返回了第三个矩形(可以是空的)。

Intersection of 2 "3D rectangles" in space is not always a 3D rectange!空间中 2 个“3D 矩形”的交点并不总是 3D 矩形!

(for example take 2 identical rectangles and rotate one, then take the intersection...) (例如取 2 个相同的矩形并旋转一个,然后取交叉点...)

So you cannot just create a rectangle object, then an intersection function that returns a rectangle object.所以你不能只创建一个矩形 object,然后创建一个交集 function,它返回一个矩形 object。

You need more complete 3D object management library.您需要更完整的 3D object 管理库。

remark:评论:

A 3D rectangle is delimited by 6 planes.一个 3D 矩形由 6 个平面分隔。 so you can identify it by 6 constraints on x,y,z所以你可以通过 x,y,z 上的 6 个约束来识别它

Then the intersection of 2 3D rectangles will just be a 3D object identified by 12 contraints.那么 2 个 3D 矩形的交集将只是一个由 12 个约束标识的 3D object。

If these 12 constraints can be simplfied to 6 ones it can be a rectange (but it's not always the case) and if it cannot then it's not a rectangle.如果这 12 个约束可以简化为 6 个,则它可以是矩形(但并非总是如此),如果不能,则它不是矩形。

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

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