create 3D rectangle using c# - 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

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

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)

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

Related

Calculate 2D plane within 3D array using 3 points

I've been trying to calculate an arbitrary plane shaped intersection in a 3-dimensional array but am unable to find any solution for my problem using C#.
I have a 3D array which is basically a stack of images (x and y correspond to the height and width of the images, z to the number of the image within the stack). The user can define three points (x,y,z), which indicate the position and orientation of a plane within the array. It can lie straight or angled in any direction within the 3D array.
I find it tricky to find a solution to get all values from this plane, from between the points as well as beyond them as they do not necessarily lay on the edge of the 3D array. As the points can be arranged in any way (all three are not colinear), the width and height of the plane is also unknown.
Does someone have an idea how to approach this problem please?

Free transform corners of image in C# [duplicate]

This question already has answers here:
Non-Affine image transformations in .NET
(3 answers)
Closed 1 year ago.
I need to combine two images in C# ( 4.7.2 ), and have the top image transformed putting each of the four corners at specific coordinates in the image.
Is that possible? Preferably with a solution that doesn't require spending a ton of money. As far as I can tell i can't do it with the Bitmap/Graphics classes.
Image of what I'm trying to do
Shear (or skew), which is what an affine transform such as used in GDI+ or WPF, is unlikely to do what you want, if I understand the question correctly. With shear/skew the transformed coordinate space is still a parallelogram, whereas in your image, the original rectangle is squeezed or stretched arbitrarily.
Assuming that's correct, I would recommend using the features in the WPF Media3D namespace (WPF, simply because it's the most accessible 3D API in the .NET context). In particular, you will want to define a texture that is your original bitmap. Then you will want to define a quadrilateral 2D surface in 3D coordinate space with sufficient resolution (i.e. triangles) for your purposes (see below), and where the triangles in that surface are constructed by tessellating the shape that you want as your final image, and where you've interpolated the texture (UV) coordinates for that shape across the vertexes that result from the tessllation.
How many triangles you actually want depends on the desired quality. In theory, you could use just two. This is the simplest approach, and determining the UV coordinates is trivial, because you only have your original four corners. But there will be a visual discontinuity along the diagonal where the two triangles meet, where the interpolation of the texture pixels changes direction due to the triangles not being square to each other.
For better results, you'll need to use more triangles. But then this complicates the assignment of the UV coordinates. For each inner vertex of this surface, you'll need to interpolate across the surface. This is probably easier to do if you generate the tessellation in the first place by subdividing the quadrilateral with lines connecting opposite sides (which will form smaller interior quadrilaterals bounded by intersecting lines) and then just divide each of those quadrilaterals into pairs of triangles. If you do it this way, then you can use the distance along each line to determine the appropriate U or V coordinate at each vertex that line goes through.
Having created the appropriate texture and geometry, it's a simple matter to render the result into a RenderTargetBitmap via the Viewport3DVisual class, and then do whatever you want with that bitmap.
Now, all that said…
If it turns out that your problem can be simplified such that shear/skew is sufficient for your needs, you can look at De-skew characters in binary image for help with that. In that particular example, they are trying to undo skew caused by optical effects, but skewing is skewing; the same exact principle works in either direction.
Even if your problem is not amenable to shear/skew approaches, before you implement your own solution (e.g. based on my outline above), you may want to look at other available tools. Information about some options can be found in, for example, Image Modification (cropping and de-skewing) in C# and Image comparison - rotation, alignment and scaling.

How do I find the height of a point in a triangle using the height of the given vertices in C#?

As the title suggests, I am trying to find the height of an arbitrary point in a triangle given the height of the vertices of that triangle. The triangle would be a polygon in a height map and the point I need to find would be the height I need to assign to the player.
I have searched methods like Barycentric Coordinates and Bilinear Interpolation, but I can't seem to find how to actually implement them using C#.
Here is a visual of what I am trying to find:
I am trying to find the height of the red dot using the heights of the vertices, which in this case are 4, 5 and 2
I would start with a generic triangle-ray intersection algorithm. Assuming your point is in x,y coordinates you would use origin = x,y,0 and direction 0,0,1. You can then simplify a bunch of the operations since you will know that some of the parameters are always zero.
There might be some clever math that could be used to make it even faster, but that is the approach I would take, and performance would probably more depend on putting triangles in some fast search structure than optimizing the intersection test..

Compare two sets of XY Coordinates and align them

I have two sets of X,Y co-ordinates as separate lists. Both represent the same irregular polygonal shape, but in different orientations and sizes/scale.
Need to write a program in C#, to compare both the points set, rotate any one of the shape such that it aligns with the another, so that they are in same orientation.
Tried searching for solution, and got to know using concave hull with angles difference can help, but could not find a good C# implementation for the same.
Can some one help me, if there is a minimal way to achieve this?
Edit: The two points-set might not be the same. One may contain more points than other.
I have contour co-ordinates of a shape and a PNG which is of same shape, but orientation is different. I want to read the PNG, calculate the angle to turn it to the fit the Contour.
Calculate image moments for point cloud
Evaluate orientation of both clouds with Theta angle.
Rotate one cloud by theta difference.
Use other moments (centroid etc) to find translation and scale

Draw a one pixel line around square sprite

I have a 15 x 15 pixel box, that I draw several off in different colours using:
spriteBatch.Draw(texture, position, colour);
What I'd like to do is draw a one pixel line around the outside, in different colours, thus making it a 17 x 17 box, with (for example), a blue outline one pixel wide and a grey middle.
The only way I can think of doing it is to draw two boxes, one 17x17 in the outline colour, one 15x15 with the box colour, and layer them to give the appearance of an outline:
spriteBatch.Draw(texture17by17, position, outlineColour);
spriteBatch.Draw(texture15by15, position, boxColour);
Obviously the position vector would need to be modified but I think that gives a clear picture of the idea.
The question is: is there a better way?
You can draw lines and triangles using DrawUserIndexedPrimitives, see Drawing 3D Primitives using Lists or Strips on MSDN for more details. Other figures like rectangles and circles are constructed from lines, but you'll need to implement them yourself.
To render lines in 2D, just use orthographic projection which mirrors transformation matrix from SpriteBatch.
You can find a more complete example with the PrimitiveBatch class which encapsulates the logic of drawing in the example Primitives from XBox Live Indie Games.
Considering XNA can't draw "lines" like OpenGL immediate mode can, it is far more efficient to draw a spite with a pre-generated texture quad (2 triangles) than to draw additional geometry with dynamic texturing particularly when a single "line" each requiring 1 triangle; 2 triangles vs 4 respectfully. Less triangles and vertices in the former too.
So I would not try to draw a "thin" line using additional geometry that is trying to mimic lines around the outside of the other, instead continue with what you are doing - drawing 2 different sprites (each is a quad anyway)
Every object drawn in 3D is drawn using triangles. - Would you like to know more?

Categories

Resources