Calculate rectangle's points from diagonal in 3D - c#

I know 3 points in a 3D plane. Two points are the ends of a diagonal and an other one which is random point on the plane. How can I calculate the two other points of a rectangle from the known diagonal line? (Later I will use the points to calculate the perimeter of the rectangle in C#.)

There's no single right answer. All you can calculate using a diagonal and a random point on the plane is a whole sets of possible answers.
Imagine rotating the diagonal to create a circle - now every second line inscribed in that circle and going through the center can be the second diagonal. The only limit is your third point.

Since you know the end points of the diagonal, you can calculate the length of the diagonal; from there you can determine the rectangle side length; having diagonal coordinates and the side length, you can determine the other two points of the rectangle using add/subtraction.

Related

How to snap a point in the normal 3d grid to a point on a sphere coordinate

So I need to snap any floating point in 3D space to the nearest point on a sphere, the size of the sphere also needs to change as the point gets further from the origin, idk if that made sense or I'm just thinking about something incorrectly, but I made a picture of it in blender:
In the picture I have green dots which represent the possible places(there are more of them) that any point in space could snap to.. So how can I make any point in space snap to the closest green dot? And IK I could just loop through those points to check the closest one, but there are gonna be MANY of these points so its not a solution and so any point has to find its closest green dot without distance checks. An example of how this could work if I didn't need it to be on a sphere is this,
So lets say I have point x, and I want to snap it to its closest green dot(here the green dot isn't a point on a sphere but actually a point on a "cartesian" grid), so to snap point x I just do round(x / gridSize) * gridSize; and that snaps x to its closest grid point, but in my case I need it on a sphere, so how can I convert that to snap to a spehre? IDK if I'm making any sesne.
compute spherical coordinates a,b,r of point-center
where a=<0,2*PI> is longitude, b=<-PI/2,+PI/2> is latitude and r>=0 is radius
r=snapping_sphere_radius
snap a angle a= 2*PI*round(a*n/(2*PI))/n
where n is number snapping points on equator
snap b angle b= PI*round(b*m/PI)/m
where m is number of snapping circles (layers of sphere) usually m=n/2 ...
recompute Cartesian position of point from spherical (a,b,r) and add center

calculate rotation of two joined lines to reach point with some coordinates

I have two jointed bars, first can spin on 360deg around 0 coords. Second jointed with first, and can rotate on 360 around joint.
Can I calculate how the rotation of both rods should be so that the end of the second one reaches a point with some coordinates
You can write usual geometry formula

Algorithm to reshape a deformed cube to normal cube with max size (furthest vertex)

I am wondering if there is an algorithm, or a mathematical function that starts from a deformed cube, gets the furthest vertices and reshapes the cube to a normal cube pased on the furthest vertices.
To make my question more clear, have a look at the following picture:
So I start with a deformed cube on the left and I want to transform it such that it will look like the second cube on the right. Since the vertex (2,2,3) is the furthest of them all, I believe it's correct to state that all other vertices should have the value 3 as their coördinates, which will result in a cube on the right?
To achieve this, is there an existing formula that I can apply in my programming language? Or should I write my own, something like: "loop over all vertices to find the furthest one. Then, from this vertex it's x,y,z coördinates which has the highest value (in this case z:3). Then put this value as the corresponding x,y,z coördinates of all other vertices?
update
Ofcourse I am aware that the same should apply if the furthest vertex has a negative coördinate, but this can easily be calculated.
no 3 is not the size because your deformed cube's center is not (0,0,0) !!! You should clarify what properties you need to preserve.
size
position
orientation (of what)
There are more ways to do this but if you need to preserve all of the above (with constrains to max the size):
compute center (avg point)
find out biggest diagonal and from that compute half size of your cube.
compute basis vectors
so just edges of your cube in 3 axises and set them to correct size and also make them perpendicular exploiting cross product.
construct new vertexes
so just translate from center along each basis vector to both directions and that is it. In case you do not need to preserve rotation you can skip #3 and use (1,0,0),(0,1,0),(0,0,1) as basis vectors directly.

C# maths, collision detection, xna

Im doing collision detection in a game.
All the surfaces are orthagonal so I only need to determine which face of an obstacle my moving object has collided with.
I have the rectangle defining the intersection of the two objects and the vector representing the moving objects speed and direction of movement.
I reckon I need to translate the intersection rectangle along my moving objects vector until the intersection becomes a line, then I'll know which face was collided with 1st.
I have no idea how to do this mathematically of programatically however
Calculate the vector from (the corner of the obstacle internal to your object) to (the corner of your object internal to the obstacle). Whichever "side" of this vector your movement vector is on gives the "side" that touched first. If they have the same angle, the corner touched first.
E.g. here your movement vector is at about 260 degrees and then calculated vector is at about 240 degrees. Anticlockwise from calculated to movement is 20 degrees, clockwise is 340 degrees. Thus the anticlockwise side (bottom) collided first.
You need able to tell which two sides are in question, and which is "clockwise" and "anticlockwise" - I hope this is trivial.
You can compare the aspect ratio of the velocity vector to the aspect ratio of the intersection rectangle.
For this particular example, if the velocity vector is steeper than the intersection rectangle (i.e. defines a taller and skinnier rectangle), then the collision was on the bottom face.
If the velocity vector is shallower, then the collision was on the left face of the moving rectangle.
If the velocity vector is the same aspect ratio as the intersection rectangle (i.e. the velocity lays on the diagonal of the intersection rectangle), then they collided on the corners.
Actually I may have figured it out...
Find the point on the intersection rectangle that isnt on the objects
rectangle
draw a line from there in the vectors direction
whatever side it intersects with is the side that collided 1st

How to hide/not draw part of the model

I have an animated model that's spinning.
I want to hide/not draw any part of the model that's Y<0
what are the ways I can do it?
ideas:
1) draw a giant rectangular box right below y=0
2) tweak the camera matrix so that y<0 is outside of clipping plane (but i have no idea how)
can someone point me into the right direction? =)
A purely mathematical approach:
Don't draw the polygons whose y's are all less than 0.
Draw the polygons whose y's are all greater than or equal to 0.
Clip the rest of the polygons with the y=0 plane and draw them.
If the polygons making up the model are triangles, clipping them is pretty trivial. You need to clip the two sides intersecting with the y=0 plane and replace the original vertices whose y's are less than 0 with the intersection points of those two sides with the clipping plane.
Use the line equations:
(x-x1) = (x2-x1)*(y-y1)/(y2-y1)
(z-z1) = (z2-z1)*(y-y1)/(y2-y1)
where 1 and 2 are the vertices of the side being clipped by the y=0 plane. Substitute their coordinates (x1, y1, z1, x2, y2, z2) and y=0 into the equations to get x and z of the intersection point. Use this point's coordinates instead of vertex 1's or 2's (whichever has y < 0).
If the polygons are texture-mapped, you'll need to recalculate the texture coordinates for the vertices that you got from the clipping. You do that in the same fashion.
It sounds like you need to introduce MSDN Bounding Frustum
Here is a good tutorial from Nic's GameDev Site.

Categories