Indexes for triangles of dodecahedron centered at the origin - c#

Wikipedia says that dodecahedron at the origin has vertices with this coordinates(x,y,z):
(±1, ±1, ±1)
(0, ±1/φ, ±φ)
(±1/φ, ±φ, 0)
(±φ, 0, ±1/φ)
where φ is golden ratio (φ = (1 + √5) / 2 ≈ 1.618 )
Let's say that I'll have this vertexes in vertexBuffer - which will be an array of Point3D.
I need prepare indexes of triangles for indexBuffer(which is an array of int). Dodecahedron has 12 faces, each face is pentagon and I will create each face from 3 triangles this way:
first triangle: a,e,b
second triangle: b,e,d
third triangle: d,c,b
For easier polyhedron I can draw it and then mark vertices and then easily get the indexes, but in this case it's not good way, cause after this Icosahedron, which has 20 faces, is waiting for me :/
So my question is: Is there any easier way how to get indexes for this vertices according requirements specified above?
Note:
I should also mentioned, that I couldn't use openGL or DirectX. We should practise 3D graphics without this libraries.

The first set of 8 vertices defines a cube.
The 3x4 remaining points come in 6 pairs that lie outside each of the 6 faces of the cube.
Each set of six points (four vertices of the cube face and the corresponding two points further away from the origin) form a pattern that repeats six times. You can get 6 triangles from each set.
An icosahedron is actually simpler: it has only 20 triangles instead of 36. It has a similar pattern, which you can see on its Wikipedia page.

Related

Fastest way to check balls that will be hit in a segment [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
There are N red balls and a white ball, all with the same radius. The white ball moves from position p1 to p2.
My objective is to predict all red balls the white ball will hit in its path and turn them yellow.
I tried iterating through all balls and taking the distance to the line formed by p1 and p2, but the balls that are behind the white turned yellow too, but they shouldn't. How should I approach this task? Is there a fast way to do it?
You can suppose that the white follows its path ignoring all collisions, the only objective is to predict what balls are in its way.
You should color the balls whose centres lie within this rectangle:
The short sides of that rectangle are perpendicular to the line through P1-P2, going through the centres of P1 and P2 respectively. Their length is 4 x radius, with P1/P2 at the middle of the line segment.
The long sides are parallel with the line through P1-P2.
Now you only need to check whether a red ball's centre point is on the right side of each of these four lines.
Check "How to tell whether a point is to the right or left side of a line?" on how you can do that.
I assume you are using Ax+By+C = 0 to describe the line.
In this case you can get perpendicular lines using Bx -Ay +C = 0. A and B have to match but C can vary. Figure out C1 and C2 corresponding to the start and end position. Now we are looking for points that lie between these two. More specifically the signs of the two equation, results have to be oposite. That is (Bx -Ay +C1) * (Bx -Ay +C2) <=0. This will filter the red balls that would collide outside the segment you are interested in. You may have to move the start/end points along the line if you want to catch collisions at the start and end point.
To speed things up you can do few tricks. Remember that divisions and square root are expensive operations. For distance since you need to divide by sqrt(A*A + B*B) store it's inverse and multiply by it instead of recomputing for each point.
If you need to compute many such collisions over a static set of balls you can consider some space partitioning. Regular grid or more fancy BSP or quad trees will allow you to remove large number of tests easily, at the expense of a more complex implementation and some static setup time.

Get Intersection line between two rectangles in 3D

I am trying to find a way to get the line (two points in 3D space) of the intersection between two rectangles.
I ran into this question: Intersection between two rectangles in 3D
But this is not my issue. In that question, the rectangle is treated as only the bounds (the perimeter), while I am looking for the rectangle as a whole (think about a picture frame vs the picture itself).
I've figured out that, in every case, there will either be an intersection line (two points), or no intersection at all. If the intersection was just on the borders, therefore just a point, it can be treated as no intersection in my case.
My scenario is that one of these rectangle represents a "static" surface, which cannot move or change. The other one represents a "dynamic" surface, which I have to adapt to avoid crossing
Example:
Once I obtain p1 and p2, which are points in the 3D space, my goal is to modify the Dynamic rectangle into a 3d polygon, which will no longer cross the static rectangle, like this:
So you can see why "edge intersections" are irrelevant to my situation. I am turning "real" intersections into edge intersections, so any edge intersection doesn't require me to do anything with it.
I am only looking for a formula, starting with two sets of 4 points (the rectangles), that would give me the two points of the line of their intersection, or would tell me that there is no (relevant) intersection.
Every formula I've found on this site or others doesn't fit my needs, or doesn't let me input arbitrary rectangles (for example, I can't fix my problem with a formula that uses planes or that treats a rectangle as simply 4 lines)
I am, of course, trying to code it (in C#), therefore any code answer is a great help, but I am confident that even a math-only answer would suffice for me to produce the code from it, therefore I will accept an answer that is only composed of pseudo-code or straight up mathematical formulas, provided they are either simple enough or explained well enough for me to understand what is happening.
If you are OK with just algorithm rather than full code here is a sketch:
Build 2 planes from the rectangles (any 3 points will do as in this answer)
Find the intersection line I of those 2 planes as in this answer or find out that the planes are parallel so there is no intersection
Find the intersections of the I line with the lines containing all sides of the rectangles as in this answer
Check whether some points found in the previous step lie inside the corresponding sides of the rectangles (line segments). This step potentially can be merged with the previous one, but I put it separately for simplicity. Now you potentially have 0, 1 or 2 segments that represent the intersections of the I line with your two rectangles (note that here point is treated as an edge case of a segment where both ends are the same). If you don't have 2 segments, there is no intersection of the rectangles.
Assuming at the previous step you found 2 segments (one in each rectangle) on the line I, you just need to find their intersection and it will be your answer (again, empty means no intersection).

Draw segments of circle on xna

How do I draw a circle sector (as in a slice of pizza shape) in xna?
I'd like to use one as a timer indicator, so would like to be able change its angle dynamically.
In an ideal world I'm looking for something like this:
Drawsector (float startAngle, float endAngle, ... )
Does such a thing exist?
And if it does - how would I go about drawing a more graphically involved one (as opposed to just block colour)
No. XNA only provides an API for drawing primitive elements called surprisingly primitives.
All is not lost because drawing a circle can be viewed as simply drawing a series of very short interconnected line segments, small enough that you can't tell they are lines, but not too small so as to be inefficient.
In XNA you would draw a PrimitiveType.LineStrip.
MSDN:
The data is ordered as a sequence of line segments; each line segment is described by one new vertex and the last vertex from the previous line seqment. The count may be any positive integer.
e.g. (from MSDN)
GraphicsDevice.DrawUserIndexedPrimitives<VertexPositionColor>(
PrimitiveType.LineStrip,
primitiveList,
0, // vertex buffer offset to add to each element of the index buffer
8, // number of vertices to draw
lineStripIndices,
0, // first index element to read
7 // number of primitives to draw
);
You would need to create your own function to determine the vertices that match the arc you want to draw. You should save that into a permanent index and vertice buffer rather than performing a DrawSector() all the time in your game loop.
Tell me more
Drawing 3D Primitives using Lists or Strips

Finding the distance of a point from a triangle in 3d space

Imagine the surface of a triangle in cartesian space. How can I find the distance of a given point from the surface of that triangle?
Point 1: [ 30, 24, 22 ]
Point 2: [ 35, 13, 19 ]
Point 3: [ 21, 29, 85 ]
In this case, how far is the point [ 40, 25, 77 ] from the surface of the triangle defined above?
Or specifically - what would the formula be to determine that distance?
I will assume you mean minimal distance. Otherwise, the distance changes depending on which point on the triangle you pick. To see that, just draw a triangle on your table and hold a pen above it.
The short answer is, to find the minimal distance, you need to build a matrix with one point per column, like so:
[ 30 35 21 ]
A = [ 24 13 29 ]
[ 22 19 85 ]
And read about projections for the details. If it has been a while since you studied this kind of stuff, don't be intimidated. It isn't rocket science (actually, maybe it is in intro to rocket science), but it will take some investment to understand. To help you along, I'll mention a bit about the theory first, and then talk about implementation.
To give you some intuition, hold on to that pen.
Lets start with the easiest case: To get the minimal distance, hold the pen exactly perpendicular to the table, with the end of the pen in the triangle somewhere. Picture your point as the tip of the pen. You just proved that the point in the triangle closest to your point is the projection of that point onto the space defined by the three points in the triangle. In other words, the other end of the pen. Any other point in the triangle must have distance longer then the pen length.
Now lets look at a complication. Suppose that the point you want is not exactly "above" the triangle, but rather to the side somewhere. Say, off of the surface of the table altogether. In this case, the point in the triangle closest to the point will either be one of three points themselves, or a point on the three line segments connecting the three initial points.
Now for the implementation: the skinny is, if you can use a simple library for linear algebra, you will save yourself a lot of grief. The hardest step is inverting the matrix, and while inverting 3x3 matrices is usually easy, it can get complicated when points are co-linear (i.e., think about three points that are aligned), when magnitudes are very different (i.e., think of a very long skinny triangle), etc.
You could write a problem-specific algorithm for this problem, given that it is only 3x3, but at the end of the day you will need to solve a projection system, so your algorithm will be doing essentially the same math that is on the Wikipedia page. So you may end up rediscovering linear algebra, which is a sure way to waste A LOT of time.
My last implementation suggestion is to check the keyword "projection" in gaming or 3D libraries. If you are lucky there is a function you can just call. Otherwise get some decent CLR linear algebra library, build the matrix, and do the entire computation efficiently and in 100 lines of code or so.
It all depends on where abouts you want the distance to come from (Which part of the triangle?). If you know the point of the triangle to start from (centre maybe?) then, from a maths perspective, the formula is:
Point 1: [a1,b1,c1] (point on triangle)
Point 2: [a2,b2,c2] (point to find distance to)
distance vector = [a2-a1, b2-b1, c2-c1]
distance = the magnitude of above = sqrt((a2-a1)^2 + (b2-b1)^2 + (c2-c1)^2))
I don't know C#, but this would be the pseudo code for it (the maths behind it)
Edit: To find centre of triangle...
Point 1: [x1, y1, z1]
Point 2: [x2, y2, z2]
Point 3: [x3, y3, z3]
Centre = [(x1+x2+x3)/3, (y1+y2+y3)/3, (z1+z2+z3)/3]

How To Extrude a Flat 2D Mesh, Giving it Depth

I have a set of co-planar, connected triangles, i.e., a 2D mesh. Now I need to extrude it out a few units in the z-axis. The mesh is defined by a set of vertices which the renderer makes sense of by matching up against an array of triangles.
Example Mesh:
Vertices: (0,0,0), (10,0,0), (10,10,0), (0,10,0) <-- (x,y,z)
Triangles: (1, 2, 3) & (3, 4, 1) <-- numbers here reference the position of a vertex above.
So here we have a 2D square. Now I need to add more vertices and triangles to that list to make an extruded shape. Triangles must be in clockwise direction, otherwise they're backface-culled.
Is there a simple algorithm for this? Thank you.
Assuming you want to extrude by a distance z, you need to follow these steps:
0) let n be the original number of vertices (4 in your example)
1) For each vertex in your vertex array, add (0,0,z) to it, and add the result to your vertex array, for a total of 2*n vertices. So, for your example, you will add the vertices (0,0,z), (10,0,z), (10,10,z), (0,10,z) to your vertex array, for a total of 2*4=8 vertices.
2) Create a list of boundary (as opposed to internal) edges for your original mesh. To do this, create a list of all triangle edges (3 edges going in clockwise order for each triangle). Then remove pairs of equal but opposite edges (these are the internal edges). For your example, you will start with 6 edges, and end up with 4 edges after removing the edge pair (3,1) and (1,3).
3) for each triangle (a,b,c) in your triangle list, create a corresponding triangle (a+n,b+n,c+n). These will be the extruded faces
4) Finally, you want to create the sides of your extruded shape. For each edge (a,b) in the boundary edge list you created in step 2, add the triangles (a,b,b+n) and (b+n,a+n,a)
That's it. Assuming no typos on my part, and no typos on your part, you should now have your desired mesh.

Categories

Resources