Towers of Providence problem - recursion

The Towers of Providence is a variation of the classical Towers of Hanoi problem. There are four pegs, denoted A, B, C, and D, and N disks of different sizes. Originally, all the disks are on peg A, stacked in decreasing size from bottom to top. Our goal is to transfer all the disks to peg D, and the rules are that we can only move one disk at a time, and no disk can be moved onto a smaller one. We can solve this problem with a recursive method: If N = 1, move this disk directly to peg D, and we are done. Otherwise (N > 1), perform the following steps:
(a) transfer the top N-2 disks on peg A to peg B applying the method recursively;
(b) move the second largest disk from peg A to peg C;
(c) move the largest disk from peg A to peg D;
(d) move the second largest disk from peg C to peg D;
(e) fill in this step

Step e: Transfer N-2 pegs from B to D by applying the method recursively.

Related

Having 3 vectors, how to check that a straight line can be drawn through them

I get the positions of 5 enemies in the game in vectors. Depending on the distance I choose, the number of enemies can vary from 0 to 5. I need to know their vectors each time to check whether it is possible to draw a straight line through a certain number of heroes (vectors).
After that, my hero will have to use his ability called wall. It consists of 2 start and end vectors. Thus, check whether my hero can put a wall on the enemies in the line to catch them
Let's say there are 3 enemy heroes whose positions I can get. I need to find out if I can pass through them directly, in order to use the ability on them.
Here's what using the ability looks like in the game
Here is getting the vector of one of the heroes
The ability itself can be twisted at a certain point. But anyway, it is necessary that the wall would touch several heroes
Wherever I move the mouse, I can put it in the desired position. But unfortunately it takes a lot of time, so I would like to automate
The coordinates of the wall itself, or rather its two edges, I can also get, but only after the ability has been used
If one prefers geometry to linear algebra...
Then One can compute the dot product of (unit-vector1. Unit-Vector2). That is equal to the SIN of the angle between them.
So if unit vector is the shooter position to target1, unit vector2 is the shooter to target2, etc... then when DOTPRODUCT(Vector1,vector2) = 1 and DOTPRODUCT(Vector1,vector3) = 1, then the three points are in syzygy.
And repeat from shooter to as many targets as you have to determine whether some or all of the points are in syzygy.
From your statement that there is a start and an endpoint I take that you select two enemys and want to trap anything in between.
So you're actually not looking for a straight line that can be drawn through your enemy positions but if they are withn a rectangle. It would be very unlikely and for more points nearly impossible that they are all collinear anyway.
So it becomes quite trivial. You draw a line through start and end enemy. Then you check the remaining enemies distance to that line vs the width of your AoE. Maye you want to also handle some body width in that calculation.
https://en.wikipedia.org/wiki/Distance_from_a_point_to_a_line
You can describe all points belonging to line (x0, y0) + (dx, dy)t = (x1, y1). Chose any two points and t as 1 and you will get (dx, dy) for line connecting two dots. Now you will need to find distance between this line and (x2, y2). it is distance between (x2, y2) and (xd, yd), where on one hand (xd, yd) = (x0, y0) + t1(dx, dy) and on other hand (xd, yd) = (x2, y2) + t2*(-dy, dx). Solving this two equations you will find t1, t2, (xd, yd) and distance between (x2, y2) and (xd, yd), which is distance between (x2, y2) and line, connecting (x0, y0) and (x1, y1).
Knowing this, you select dots with min_x and max_x and calculate ditance between line, connecting said dots and rest of the dots. If distance is lesser then some threshold of your choice, then you can assume that you can have line passing through all dots.
Any line in the plane can be described by an equation a*x + b*y + c = 0 with (a, b) ≠ (0, 0). Note that if you have an equation of this form, then multiplying each coefficient a, b, c with the same number yields an equation describing the same line. That's the reason (a, b, c) is called a homogeneous coordinate vector for that line.
How do you find a, b, c? One simple approach would be treating this as three linear equations in three unknowns. You plug in the x and y coordinates for all your three points, and get tree equations for a thorough c. However, there is a catch. Since the right hand side of each equation is zero, a = b = c = 0 is always a solution. In those cases where there is only one solution, that will be it. So in order for there to be a line, you need more than one solution. The mathematical tool to determine whether a set of equations had more than one solution is the determinant. It is zero if the system has no single unique solution.
Long story short: three points are collinear (on a line) if
⎛x1 y1 1⎞
det ⎜x2 y2 1⎟ = 0
⎝x3 y3 1⎠
The homogeneous coordinate vector describing the line world correspond to the kernel of that matrix.
Of course, if your input coordinates are floating point numbers, exact zero is unlikely. Presumably that wall does allow for some error in some way, and you'd need to tell us about that in order to get an answer that models this aspect correctly. In the mean time, know that the absolute value of the determinant above is proportional to the area of the triangle created by these three points. So if your were to pick a constant threshold value, the farther your enemies are apart along the direction of the wall, the less they could deviate from the straight line without violating that threshold.

Interception of two objects in 3D space

I am trying to workout the interception of two objects and try to code it into an application
A cannon is in Position A and a plane is in position B
The plane is moving with vector b (without affected by gravity)
A cannonball is shot (affected by gravity) with unit vector a and magnitude of m
They intercept at position C at T seconds
Knowns: A, m, B, b
Unknowns: a, C, T
The only thing I can think of to solve it in terms of code is split the equations into X, Y and Z component and substitute T as a value and increment it.
It will be nice if someone can kindly tell how to find one of the unknowns
Thanks
The answer is in here:
https://blog.forrestthewoods.com/solving-ballistic-trajectories-b0165523348c
from meowgoesthedog

For a cubic, given 8 coordinates of vertices, how to get coordinate pair of 12 edges?

I thought about this quesion for a while, and googling "edge" or "vertices" does not return anything useful.
Yes it's very simple for cubic, but not so easy for arbitrary shape in 3D. E.g. a concave body. You might find some diagonal lines but it's not the edge.
This general term for this question is called Convex Hull
It's widely used in GIS
https://gis.stackexchange.com/questions/1200/concave-hull-definition-algorithms-and-practical-solutions
And the most famous algorithm is done by Graham Scan from ACM
GRAHAM_SCAN(Q)
Find p0 in Q with minimum y-coordinate (and minimum x-coordinate if there are ties).
Sorted the remaining points of Q (that is, Q ? {p0}) by polar angle in counterclockwise order with respect to p0.
TOP [S] = 0 ? Lines 3-6 initialize the stack to contain, from bottom to top, first three points.
PUSH (p0, S)
PUSH (p1, S)
PUSH (p2, S)
for i = 3 to m ? Perform test for each point p3, ..., pm.
do while the angle between NEXT_TO_TOP[S], TOP[S], and pi makes a nonleft turn ? remove if not a vertex
do POP(S)
PUSH (S, pi)
return S
http://en.wikipedia.org/wiki/Graham_scan
http://www.personal.kent.edu/~rmuhamma/Compgeometry/MyCG/ConvexHull/GrahamScan/grahamScan.htm
Fun fact: convex OR concave hull has a patent:
https://stackoverflow.com/a/2241263/41948

Find all points with integer coordinates inside tetrahedron

I am trying to find all the points with integer coordinates that lie inside of a tetrahedron (I want to somehow be able to loop through them). I know the coordinates of the four points (A, B, C, D) that define the tetrahedron.
What I'm currently doing is I find the bounding box of the tetrahedron (minimum and maximum x, y, z coordinates of A, B, C, D) and then do a loop through all of the points inside the bounding box. For every such point, I calculate the barycentric coordinates (using the equations from Wikipedia) and check if the point is inside the tetrahedron (if any of the barycentric coordinates is negative or bigger than 1, the point isn't inside).
Is there a better way to do this? Currently there is around 1/6 chance that the point I am testing (from the bounding box) really lies inside the tetrahedron, so I think I'm doing too many unnecessary computations.
I am working with a list of tetrahedra that I generated by triangulating a bigger volume (I am expanding the volume and want to interpolate the missing values using tetrahedral interpolation). I am not using any external libraries.
Another idea for improving:
check if a "rod" parrallel to z-axis (i.e. x=4, y=6) runs through the tetrahedron. If not, no values with (x=4, y=5, z) can be inside.
Else, find where the rod intersects the edge of the tetrahedron (by finding out where the planes that make up the edge of the tetrahedron intersect it).
Say these planes intersect at z=1.3 and z= 10.04. Then you know all points (4,5, 2) to (4,5,10) are inside.
Repeat for all values of x and y.
This should be faster in practice, because it will save you 1 loop.
Your approach is the correct one. There are some possible optimisations, which might be worth it or not depending on the requirements. For example:
There is an easier way to check if a given point is inside or outside of the tetrahedron.
It amounts to checking the which half-space the point belongs to with respect to each of the 4 sides of the tetrahedron:
Each side is defined by 3 points (say A, B, C). Then a plane normal is a (C-A)x(B-A) (that's cross product of vectors in the plane). If this coordinates are (a,b,c), then the plane equation is F(x,y,z) = ax+by+cz = 0. For a given point (x0, y0, z0) the sign of F(x0,y0,z0) determines which half-plane the points belong to.
The point is that you can precompute plane quations for each side of the tetrahedron as well as the sign which corresponds to 'outside' an then the check for a given point amounts to doing at most 4 evaluations (one for each side), each taking 3 multiplications and 2 additions.

Circle to tangent mapping

Assume you're given a circle with the line AB containing its center O, such that A and B are on the circle (OA=OB=radius). A tangent t is drawn on the point A, and
I should calculate the mapping of certain points (a,b,c,d...) of the circle to the points on the tangent (at, bt, ct, dt, ...) such that the distance Aa (the distance along the circle) is the same as the distance Aat (the distance along the tangent) (and the same for the distances Ab, Ac, Ad). But, here, certain constraint should be considered: those points of the circle (among (a, b, c, d)) that are from one side of the circle from A to B should be placed on one side of the tangent (the nearer), and those from the other side of the circle form A to B should be placed on the other side. Basically, the circle should be split at B, and then mapped to the tangent. I hope this explanation is sufficient enough.
It should be noted that I have information about coordinates of A, B, O, a, b, c, d. I supposed to calculate (at, bt, ct, dt).
For solving this problem, I have two approaches, but I'm not sure how I could make sure they always work correctly.
1) I calculate the equation of the tangent at point A. Then for each point (a, b, c, d) I calculate the distance from A (along the circle), and use these distances for calculating (at, bt, ct, dt...) along the tangent. What I dont know here is how to calculate the distances
from A to (a, b, c, d). The problem is the 'proper side' determination, meaning how should I determine whether the point should be mapped on one side of the tangent or the other. What would be the way to determine this.
2) I calculate the equation of the tangent at point A. Then for each point (a, b, c, d) I calculate the distance from A (along the circle), and use these distances for calculating (at, bt, ct, dt...) along the tangent. To determine the 'proper side' of a given point, I might use the projection of that point to the tangent. But, even with this, how I know 'which side is which'? Perhaps there are much simpler ways to do this.
Any suggestion on how to do this is welcome. In case I was not precise enough, I'll elaborate.
A better suggestion would be to calculate a coordinate transformation that would map the circle into a unit circle with the centre at the origin, so that A will have coordinates (1, 0) (and B respectively (-1, 0)).
The transformation should be dilation with rotation.
Now, the distance on Aa is just the angle aOA measured in radians. So you can easily calculate at, it is (1, atan2(y, x)) where (x, y) are the coordinates of a.
Now, the only thing you need is to return to the original coordinate system, applying the inverse transformation.
To determine which "side" of the circle you're on, you basically need to determine which side of the line AB you're on. For the answer to that, see e.g. Determine which side of a line a point lies.

Resources