AMH
AMH

Reputation: 6451

create 3D rectangle using c#

I read about rectangle structure in c# and the intersection function in it

My Question is: how to custom it such that I can have a 3D rectanlge, have x,y,z coordinates

and get it intersection with another one ?

Any idea

Upvotes: 0

Views: 2830

Answers (3)

Ricky Bobby
Ricky Bobby

Reputation: 7608

(about the intersection function)

You cannot create such a function.

The intersecting function of 2 rectangles in 2D is interesting because it returns you a third rectangle (than can be empty).

Intersection of 2 "3D rectangles" in space is not always a 3D rectange!

(for example take 2 identical rectangles and rotate one, then take the intersection...)

So you cannot just create a rectangle object, then an intersection function that returns a rectangle object.

You need more complete 3D object management library.

remark:

A 3D rectangle is delimited by 6 planes. so you can identify it by 6 constraints on x,y,z

Then the intersection of 2 3D rectangles will just be a 3D object identified by 12 contraints.

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.

Upvotes: 2

Daren Thomas
Daren Thomas

Reputation: 70314

Just create your own. Here are some ideas:

  • a 3D rectangle not only has a width and a height, but also a plane
  • 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
  • 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.

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.

So, model your rectangle like this:

  • a point (Origin)
  • a vector Width (this is often called u in maths)
  • a vector Height (this is often called v in maths)
  • the normal vector is not necessary anymore since it is can be calculated by the vectorial product of Width x Height

The three other points of your rectangle can then be calculated as:

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

Upvotes: 3

Oded
Oded

Reputation: 498904

The rectangle class you have linked to models a 2D rectangle (I don't know what a 3D rectangle would be, BTW).

Pretty much the whole System.Drawing namespace deals with 2D, so you can't customise it that way.

The System.Drawing parent namespace contains types that support basic GDI+ graphics functionality. Child namespaces support advanced two-dimensional and vector graphics functionality, advanced imaging functionality, and print-related and typographical services.

(emphasis mine)

Upvotes: 2

Related Questions