calculates all possibilites of 4 cubes - math

So, I have 4 cubes with colors on each face, the colors can be orange, blue, red or green
My objective is to know how to position and rotate the cubes in a line so there is the same colors on each face of the line
example
example2
The colors between cubes and at the left and right of the line don't matter
I've tried some stuff in python but I can't even find a way to know how to compute that
If someone can help me out to find a program that takes the colors of the cubes and show the solution would be great. I mainly use Python but if the program is made in another language is okay too

Alright, let's do a little math first. There are four cubes. Each cube has at most 24 unique orientations: first, there are 6 choices for face A; second, there are up to four choices for face B. If we compute 24^4 we find there are 331,776 unique orientations of the set of four cubes. We might as well plan on trying all of these since the number is so low. If the number were higher, we might need to be a little smarter, but bear with me for now.
So, we are going to brute-force this. We need to represent each cube in such a way that, given an orientation, we can get the front, back, top and bottom colors. It might be helpful to model these cubes by naming the faces. We can name them as follows for orientation #1, which we can consider the default orientation:
Face A/0 is the top of the cube
Face B/1 is the front of the cube
Face C/2 is the right of the cube
Face D/3 is the back of the cube
Face E/4 is the left of the cube
Face F/5 is the bottom of the cube
Now, the orientations of the cubes can be thought of as a permutation of these faces. We could try to write code that calculates what the Nth permutation is, or we could just list all 24 unique orientations and the mappings for each:
[0,1,2,3,4,5] ... the identity mapping, since face A/1 is the default
[0,2,3,4,1,5] ... rotate #1 90 deg clockwise around the positive vertical axis
[0,3,4,1,2,5] ... rotate #1 180 deg clockwise around the positive vertical axis
[0,4,1,2,3,5] ... rotate #1 270 deg clockwise around the positive vertical axis
[1,5,2,0,4,3] ... rotate #1 90 deg counterclockwise around the positive horizontal axis
[1,2,0,4,5,3] ... rotate #5 90 deg clockwise around the positive vertical axis
[1,0,4,5,2,3] ... rotate #5 180 deg clockwise around the positive vertical axis
[1,4,5,2,0,3] ... rotate #5 270 deg clockwise around the positive vertical axis
[2,1,5,3,0,4]
[2,5,3,0,1,4]
[2,3,0,1,5,4]
[2,0,1,5,3,4]
[3,0,2,5,4,1]
[3,2,5,4,0,1]
[3,5,4,0,2,1]
[3,4,0,2,5,1]
[4,1,0,3,5,2]
[4,0,3,5,1,2]
[4,3,5,1,0,2]
[4,5,1,0,3,2]
[5,3,2,1,4,0]
[5,2,1,4,3,0]
[5,1,4,3,2,0]
[5,4,3,2,1,0]
Now, we can write a function which, given an orientation and a face, will tell us the color:
GetFaceColor(orientation, face)
1. return colors[permutations[orientation][face]]
Here, permutations is an array of the permutation arrays given above, and colors is an array where the colors of the six cube faces are specified for orientation #1.
Now, all we need to do is use four nested loops to check all 24^4 possibilities:
FindSolution()
1. for w = 1 to 24 do
2. for x = 1 to 24 do
3. for y = 1 to 24 do
4. for z = 1 to 24 do
5. if FacesMatch(w, x, y, z, 0)
and FacesMatch(w, x, y, z, 1)
and FacesMatch(w, x, y, z, 3)
and FacesMatch(w, x, y, z, 5)
then return (w, x, y, z)
6. return null
FacesMatch(or1, or2, or3, or4, face)
1. color1 = cube1.GetFaceColor(or1, face)
2. color2 = cube2.GetFaceColor(or2, face)
3. color3 = cube3.GetFaceColor(or3, face)
4. color4 = cube4.GetFaceColor(or4, face)
5. return color1 = color2 and color2 = color3 and color3 = color4

Related

Find Equation for Eclipse / Inverse Parabola in 3D Space Given 2 Points

In a 3D space (x,y,z), you are given two points with no restrictions.
Let's say Point 1 = (15,10,-5), Point 2 = (-1, 0, 11)
An arbitrary point (denoted X in the image) is made by finding the mid-point between point 1 and point 2, in this case (7,5,6), and then y is incremented by 10 which creates a third point
Point 3 = (7,15,6)
Attached is an image to better portray these points
The problem is to find an equation that creates the orange line that links the points 1, 2 and 3. The line doesn't necessarily have to link on the bottom, but I assume it is easier to create an ellipse with these points than an inverse parabola.
It is rather simple to build a circle through these three points (note they must be non-collinear).
Make a plane containing given points, use arbitrary coordinate system in this plane. For example, point P1 is origin, vector P2-P1 defines OX axis, vector product of P2-P1 and P3-P1 defines normal N, and (P2-P1) x N defines OY axis
Solve "circle through three points" problem in this plane, find radius and center.
Transform center back into 3D.
Also note that there is infinite number of ellipses and parabolas through three points (until we define additional limitations),

How do I triangulate a 3d coordinate from two 2d points?

I'm working on a project with two infrared positioning cameras which output the (X,Y) coordinate of any IR source. I'm placing them next to each other and my goal is to measure the 3D coordinate (X,Y,Z) of the IR source, using the same technique our eyes use to measure depth.
I have drawn a (lousy) sketch here
which illustrates what I'm trying to calculate. The red dot is my IR source, which can also be seen on the 'views' of the camera to the right. I am trying to measure the length of the blue line.
I have a few known variables:
The cameras have a resolution of 1024x768 (which also means that this is the maximum of the (X,Y) coordinate mentioned earlier)
Horizontally the field of view is 41deg, vertically 31deg.
I have yet to decide on the distance between cameras (AB), but this will be a known variable. Let's make it 30 cm for now.
Sadly I cannot seem to find the focal length of the camera.
Ultimately I'm hoping for an (X,Y,Z) coordinate relative to the middle point of AB. How would I go about measuring (Z)?
I am not sure how well aligned your cameras are, but from your pictures I am beginning to assume that the camera A and camera B are so well aligned that the rectangle representing the camera B's screen is simply horizontal translation of the rectangle representing the camera A's screen. What I mean by that is that the corresponding edges of the screens' rectangles are parallel to each other and the two rectangular screens lie in a common vertical plane perpendicular to the ground. Now, consider the plane parallel to the vertical plane that contains the two camera screens and passing through the focal points A and B of the two cameras. Call this latter plane the screen_plane. Also, the focal points A and B are at an equal height from the ground. If that is the case, and if I assume that c = |AB| is the distance between the focal points of the two cameras, and if I put a coordinate system at A, so that the x axis is horizontal to the ground, the y axis is perpendicular to the ground, and the z axis is parallel to the ground but perpendicular to the screen, then the focal point of camera B would have coordinates ( c, 0, 0 ). As an example, you have given c = 30 cm. Also the screen_plane is spanned by the x and y axes described above and the z axis is perpendicular to the screen_plane.
If that is the setting you want to work with, then the red point P will appear on both screens with the same coordinate Y_A = Y_B but different coordinates X_A and X_B.
Then let us denote by theta the horizontal field of view angle, which you have determined as theta = 41 deg. Just to be clear, I am assuming the angle between the leftmost side to the rightmost side of view is 2 * theta = 82 deg.
If I understand correctly, you are trying to calculate the distance Z between the vertical plane screen_plane that contains both camera focal points and the plane parallel to screen_plane and passing through the red point P, i.e. you are trying to calculate the distance from P to the vertical plane screen_plane.
Then, here is how you calculate Z:
Step 1: From the image of point P on screen A calculate the distances (e.g. the number of pixels) from P to the vertical edges of the screen. Say they are dist_P_to_left_edge and dist_P_to_right_edge. Set
a_A = dist_P_to_left_edge / (dist_P_to_left_edge + dist_P_to_right_edge) (this one is not really necessary)
b_A = dist_P_to_right_edge / (dist_P_to_left_edge + dist_P_to_right_edge)
Step 2: Do the same with the image of point P on screen B:
a_B = dist_P_to_left_edge / (dist_P_to_left_edge + dist_P_to_right_edge)
b_B = dist_P_to_right_edge / (dist_P_to_left_edge + dist_P_to_right_edge) (this one is not really necessary)
Step 3: Apply the formula:
Z = c * cot(theta) / (2 * (1 - b_A - a_B) )
So for example, from the pictures of the screens of camera A and B you have provided, I measured with a ruler, that
b_A = 4/38
a_B = 12.5/38
and from the data you have included
theta = 41 deg
c = 30 cm
so I have calculated that the length of the blue segment on your picture is
Z = 30 * cos(41*pi/180) / (sin(41*pi/180) * (1 - 4/38 - 12.5/38))
= 60.99628 cm

How do I calculate the angle between two normalized vectors and an up direction?

How would one go about calculating the angle of a vector versus an "up" direction while looking down the axis of another vector? To illustrate what I'm asking, I've drawn two diagrams:
http://i.imgur.com/0JUb8uA.jpg
In this image, you can see the general setup. The green arrows are the Y axis (up is positive, down is negative), the red arrows are the X axis (left is negative, right is positive), and the blue arrows are the Z axis (towards the screen is positive, away is negative).
The cyan and yellow arrows represent two normalized vectors (length = 1) centred around 0, 0, 0. The vectors can be anywhere in 3D space, I'm just using this as an example- the only thing guaranteed is that their length will always be 1 and the up direction will always be Vector(0, 1, 0).
http://i.imgur.com/rdlEesZ.jpg
This is what I'm trying to figure out. I've oriented the camera so it points directly down the cyan arrow. I'm attempting to calculate the angle between the yellow arrow and the green (Y) axis while looking down the line specified by the cyan arrow.
I'm having troubles researching this on my own probably because I lack the vocabulary to adequately define my question (hence the diagrams). It seems like it might be possible to determine a rotation between the cyan arrow and a known axis, then rotate everything (including the yellow arrow) around the origin, at which point it's just a matter of using atan2() to determine the angle based on two coordinates of the yellow vector (ignoring the depth component), but I'm guessing there's probably a simpler way of doing things. What, if anything, might that be?
If you have two vectors v=(v1, v2, v3) and w=(w1, w2, w3), then their dot product is v · w = v1 · w1 + v2 · w2 + v3 · w3. However another expression for their dot product is v · w = |v| |w| cos a, where |v| and |w| are the lengths of the two vectors, and a is the angle between them.
If you're representing these vectors as components (that is, the (v1, v2, v3)), then it's easy to calculate both their dot product and their respective lengths. Then cos a = v · w / (|v| |w|), and take arccos of that.
Working out rotations that would take one into the other is the hard way of doing this.
This is similar to various questions including 1 and 2, but I think they make it sound more complicated than it is.

Translation coordinates for a circle under a certain angle

I have 2 circles that collide in a certain collision point and under a certain collision angle which I calculate using this formula :
C1(x1,y1) C2(x2,y2)
and the angle between the line uniting their centre and the x axis is
X = arctg (|y2 - y1| / |x2 - x1|)
and what I want is to translate the circle on top under the same angle that collided with the other circle. I mean with the angle X and I don't know what translation coordinates should I give for a proper and a straight translation!
For what I think you mean, here's how to do it cleanly.
Think in vectors.
Suppose the centre of the bottom circle has coordinates (x1,y1), and the centre of the top circle has coordinates (x2,y2). Then define two vectors
support = (x1,y1)
direction = (x2,y2) - (x1,y1)
now, the line between the two centres is fully described by the parametric representation
line = support + k*direction
with k any value in (-inf,+inf). At the initial time, substituting k=1 in the equation above indeed give the coordinates of the top circle. On some later time t, the value of k will have increased, and substituting that new value of k in the equation will give the new coordinates of the centre of the top circle.
How much k increases at value t is equal to the speed of the circle, and I leave that entirely up to you :)
Doing it this way, you never need to mess around with any angles and/or coordinate transformations etc. It even works in 3D (provided you add in z-coordinates everywhere).

How to transform a co-ordinate value in 45 deg-135 deg co-ordinate system to earth co-ordinate system?

I get a series of square binary images as in the picture below,
I want to find the red point, which is the point of intersection of four blocks (2 black and 2 white). For doing so, I use to get the sum of all pixel values along the diagonal directions of the square image, which is 45 deg and 135 deg respectively. The intersection of maximum pixel sum 45 deg line and minimum pixel sum 135 deg line is where my red point is.
Now that I get the co-ordinate of the red point in 45 deg-135 deg co-ordinate system, how to I transform them to earth co-ordinates?
In other words, say I have a point in 45deg-135deg co-ordinate system; How do I find the corresponding co-ordinate values in x-y co-ordinate system? What is the transformation matrix?
some more information that might help:
1) if the image is a 60x60 image, I get 120 values in 45deg-135deg system, since i scan each row followed by column to add the pixels.
I don't know much about matlab, but in general all you need to do is rotate your grid by 45 degrees.
Here's a helpful link; shows you the rotation matrix you need
wikipedia rotation matrix article
The new coordinates for a point after 2D rotation look like this:
x' = x \cos \theta - y \sin \theta.
y' = x \sin \theta + y \cos \theta.
replace theta with 45 (or maybe -45) and you should be all set.
If your red dot starts out at (x,y), then after the -45 degree rotation it will have the new coordinates (x',y'), which are defined as follows:
x' = x cos(-45) - y sin (-45)
y' = x sin (-45) + y cos (-45)
Sorry when I misunderstood your question but why do you rotate the image? The x-value of your red point is just the point where the derivative in x-direction has the maximum absolute value. And for the y-direction it is the same with the derivative in y-direction.
Assume you have the following image
If you take the first row of the image it has at the beginning all 1 and the for most of the width zeroes. The plot of the first column looks like this.
Now you convolve this line with the kernel {-1,1} which is only one nested loop over your line and you get
Going now through this result and extracting the position of the point with the highest value gets you 72. Therefore the x-position of the red point is 73 (since the kernel of the convolution finds the derivative one point too soon).
Therefore, if data is the image matrix of the above binary image then extracting your red point position is near to one line in Mathematica
Last[Transpose[Position[ListConvolve[{-1, 1}, #] & /#
{data[[1]],Transpose[data][[1]]}, 1 | -1]]] + 1
Here you get {73, 86} which is the correct position if y=0 is the top row. This method should be implemented in a few minutes in any language.
Remarks:
The approximated derivative which is the result of the convolution can either be negative or positive. This depends whether it is a change from 0 to 1 or vice versa. If you want to search for the highest value, you have to take the absolute value of the convolution result.
Remember that the first row in the image matrix is not always in top position of the displayed image. This depends on the software you are using. If you get wrong y values be aware of that.

Resources