Ray intersection length with random 3D box [closed] - c

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have a 3d box random in space and know the nodes of it. A ray is intersecting this box. I would like to know the length of this intersection and have no idea how to code this in C or describing it mathematically.

This can be broken down into 3 parts:
a) Use "formula for intersection between line and plane" to determine if/where ray hits each plane; where each plane is determined from 3 vertices of the box (from each face of the box)
b) Determine if the intersection point for each plane (if any) is inside or outside the corresponding face of the box. For this I'd flatten everything to 2D (e.g. discard x, y or z for the plane's vertices and its intersection point, depending on which part of the plane's surface normal is largest); then pick a direction (e.g. "towards +x") and see if a line (in 2D space) from the intersection point heading in the chosen direction hits one edge of the face (and not zero edges or 2 edges). From this you get a list of intersections with faces of the box.
c) Find the closest "intersections with faces of box". This is mostly just a simplified Pythagoras thing ("distance_squared = distance_x * distance_x + distance_y * distance_y + distance_z * distance_z") with some "if(distance_squared < best_distance_squared_seen_so_far)".
Note 1: This method works for arbitrary objects/meshes with any number of faces (not just boxes); however if you wanted to support concave polygons (for faces) you'd need to determine if (after flattening to 2D) a line from the intersection point heading in a chosen direction hits an odd number of edges (and not just one edge).
Note 2: There's a tricky case where (after flattening to 2D) a line from the intersection point heading in a chosen direction hits a vertex perfectly. In this case you need to determine if it hits an edge or misses both edges by determining if both edges leading from the vertex are on the same side of your line. I don't think you need to care about this case for boxes (and can assume "hits vertex = hits an edge") but would need to care about it for arbitrary objects/meshes.

Related

How do you arrange 3D points in a clockwise order in C? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I have a load of 3D points (points on the cross section of a 3D shape) but they are in a random order... how would I go about putting them in the correct order so that when they are joined together they form the cross section of the 3D shape?
I think the answer is "You can't".
Why depends on what "points on the cross section" actually means. There's at least 3 potential interpretations:
a) "points on the cross section" are just random points floating in the middle of the shape of the cross section. In this case a circle (and any other shape) that is large enough will encompass all of the points.
b) "points on the cross section" are random points on the perimeter of the shape of the cross section (e.g. 12 points that could describe a square but none of the points are corners so it could also be an octagon or something else). In this case you still can't determine what the shape is.
c) "points on the cross section" describe the corners of the shape of the cross section. In this case consider points like this:
* *
*
* *
It could be:
*-----*
\ |
,* |
*-----*
..or:
*-----*
| /
| * __
*-----*
..or something else (there's at least 4 possible shapes - more if a "twist" is allowed).
In any case; if you can't determine the shape, then you can't determine the correct order of the points.
If you increase the number of points, then it doesn't really help at all. For example, with 12345 million points that all look like they could describe a rough circle (with minor bumps and dips on the perimeter), then you can't know that it's not supposed to be missing a tiny little sliver straight through its center.
Mostly; I think you need to figure out how the "points on the cross section" were created and see if you can modify that to give you more information about the points. For example, if (as I suspect) you're starting with a 3D mesh (vertices and polygons in 3D space) and slicing a cross section into it with a cutting plane; then you could track "sliced polygon number" and associate that with the corresponding pair of points.

Connect points to plane/Draw Polygon

I'm currently working on a project where I want to draw different mathematical objects onto a 3D cube. It works as it should for Points and Lines given as a vector equation. Now I have a plane given as a parametric equation. This plane can be somewhere in the 3D space and may be visible on the screen, which is this 3D cube. The cube acts as an AABB.
First thing I needed to know was whether the plane intersects with the cube. To do this I made lines who are identical to the edges of this cube and then doing 12 line/plane intersections, calculating whether the line is hit inside the line segment(edge) which is part of the AABB. Doing this I will get a set of Points defining the visible part of the plane in the cube which I have to draw.
I now have up to 6 points A, B, C, D, E and F defining the polygon ABCDEF I would like to draw. To do this I want to split the polygon into triangles for example: ABC, ACD, ADE, AED. I would draw this triangles like described here. The problem I am currently facing is, that I (believe I) need to order the points to get correct triangles and then a correctly drawn polygon. I found out about convex hulls and found QuickHull which works in three dimensional space. There is just one problem with this algorithm: At the beginning I need to create a three dimensional simplex to have a starting point for the algorithm. But as all my points are in the same plane they simply form a two dimensional plane. Thus I think this algorithm won't work.
My question is now: How do I order these 3D points resulting in a polygon that should be a 2D convex hull of these points? And if this is a limitation: I need to do this in C.
Thanks for your help!
One approach is to express the coordinates of the intersection points in the space of the plane, which is 2D, instead of the global 3D space. Depending on how exactly you computed these points, you may already have these (say (U, V)) coordinates. If not, compute two orthonormal vectors that belong to the plane and take the dot products with the (X, Y, Z) intersections. Then you can find the convex hull in 2D.
The 8 corners of the cube can be on either side of the plane, and have a + or - sign when the coordinates are plugged in the implicit equation of the plane (actually the W coordinate of the vertices). This forms a maximum of 2^8=256 configurations (of which not all are possible).
For efficiency, you can solve all these configurations once for all, and for every case list the intersections that form the polygon in the correct order. Then for a given case, compute the 8 sign bits, pack them in a byte and lookup the table of polygons.
Update: direct face construction.
Alternatively, you can proceed by tracking the intersection points from edge to edge.
Start from an edge of the cube known to traverse the plane. This edge belongs to two faces. Choose one arbitrarily. Then the plane cuts this face in a triangle and a pentagon, or two quadrilaterals. Go to the other the intersection with an edge of the face. Take the other face bordered by this new edge. This face is cut in a triangle and a pentagon...
Continuing this process, you will traverse a set of faces and corresponding segments that define the section polygon.
In the figure, you start from the intersection on edge HD, belonging to face DCGH. Then move to the edge GC, also in face CGFB. From there, move to edge FG, also in face EFGH. Move to edge EH, also in face ADHE. And you are back on edge HD.
Complete discussion must take into account the case of the plane through one or more vertices of the cube. (But you can cheat by slightly translating the plane, constructing the intersection polygon and removing the tiny edges that may have been artificially created this way.)

How can I know if the given points form a 2d rectangle [duplicate]

This question already has answers here:
find if 4 points on a plane form a rectangle?
(11 answers)
Closed 8 years ago.
How to know if the given points can form a 2d rectangle? Assume that the user has to put 4 inputs:
Point 1: 0 0
Point 2: 20 0
Point 3: 20 50
Point 4: 0 50
How do I know if these given points form a 2d triangle or not? is there like a formula or something to follow? Please explain to me because I'm trying to understand!
This has to do with C because I'm programming it on C. If not, please move it to the relevant forum.
If you want to check for an unrotated rectangle (i.e., two vertical and two horizontal lines), then the co-ordinates will need to be of the form
(a,x)
(a,y)
(b,x)
(b,y)
(If you want to allow for rotations then you should look up scalar products: you'll need to check whether the lines have 90 degree angles between them.)
You also mention triangles. I'm not sure if that was intentional, or if you meant the whole question to be about rectangles.
Any three points will form a triangle, as long as they're not collinear (all in a line). If you want to know whether four points all lie on the edges of the same triangle, then you'll need to check that you have three points that are collinear and one that's not on that same line. That will mean that three of the points form the vertices of your triangle, and the fourth is somewhere on one of the edges.
(I can't think of another interpretation of your question with respect to triangles. Please clarify if that's not what you meant.)

Cannibals and Missionaries with Strength [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
i need some help with a math task our professor gave us. Any suggestions would help.
The problem is:
There are N cannibals and M missinaries. All missionaries have a strenght attribute, which can be 1 or any positive whole number. Strenght indicates how many cannibals he can fight off.
Basically: there are two sides of the river, there is a 2-slot boat, and you have to transfer all the guys to the other side, without letting the cannibals eat the missionaries.
How would you write a program for this? What would be the transferring-grouping algorythm?
Thanks in anticipation,
Mark.
Model your problem as a states graph.
In here, a state is ({L,R}n,{L,R}m,{L,R}) Where:
First n entries: one for each missionary - where he is: left/right bank of the river
next,m entries: one for each canibal- where he is: left/right bank of the river
Last entry is for the boat
These are your vertices - you should also trim the invalid states - where strength of missionaries is not enough in one (or more) side. It is easy to calculate it for each state.
Your edges are:
E = { (S1,S2) | Can move in one boat ride from S1 to S2 }
All is left to do - use some shortest path algorithm to find the shortest path from: (L,L,....,L) to (R,R,...,R).
You can use BFS for this task, or even bi-directional search - or an informed algorithm (with admissible heuristic) such as A* Algorithm.
PS. The 'graph' is just conceptual, in practice you will have a function next:S->2^S, that given a state - returns all valid successors of this state (states that you can get to them using one edge on the graph from S). This will allow you to "generate the graph" on the fly.
Your next(S) function should be something like (high level pseudo code, without optimizations):
next(S):
let x be the bank where the boat is, and y the other bank
for each person p1 on bank x:
S' = S where boat and p1 moved from x to y
if S' is valid according to strength limitations, yield S'
for each p2 != p1 on bank x:
S' = S where boat and p1 and p2 moved from x to y
if S' is valid according to strength limitations, yield S'

Closest pair of points algorithm variation

I know this may be a duplicate, but it seems like a variation on the 'Closest pair of Points' algorithm.
Given a Set of N points (x, y) in the unit square and a distance d, find all pair of points such that the distance between them is at most d.
For large N the brute force method is not an option. Besides the 'sweep line' and 'divide and conquer' methods, is there a simpler solution? These pair of points are the edges of an undirected graph, that i need to traverse it and say if it's connected or not (which i already did using DFS, but when N = 1 million it never finishes!).
Any pseudocode, comments or ideas are welcome,
Thanks!
EDIT: I found this on Sedgewick book (i'm looking at the code right now):
Program 3.18 uses a two-dimensional array of linked lists to improve the running time of Program 3.7 by a factor of about 1/d2 when N is sufficiently large. It divides the unit
square up into a grid of equal-sized smaller squares. Then, for each square, it builds a linked list of all the
points that fall into that square. The two-dimensional array provides the capability to access immediately
the set of points close to a given point; the linked lists provide the flexibility to store the points where
they may fall without our having to know ahead of time how many points fall into each grid square.
We are really looking for points that are inside of a circle of center (x,y) and radius d.
The square that encloses circle is a square of center (x,y) and sides 2d. Any point out of this square does not need to be checked, it's out. So, a point a (xa, ya) is out if abs(xa - x) > d or abs (ya -yb) > d.
Same for the square that is enclosed by that circle is a square of center (x,y) and diagonals 2d. Any point out of this square does not need to be checked, it's in. So, a point a (xa, ya) is in if abs(xa - x) < (d * 1.412) or abs(ya -yb) < (d * 1.412).
Those two easy rules combined reduce a lot the number of points to be checked. If we sort the point by their x, filter the possible points, sort by their y, we come to the ones we really need to calculate the full distance.
For any given point, you can use a Manhattan distance (x-delta plus y-delta) heuristic to filter out most of the points that are not within the distance "d" - filter out any points whose Manhattan distance is greater than (sqrt(2) * d), then run the expensive-and-precise distance test on the remaining points.

Resources