# Thread: Help with distance in square problem...

1. ## Help with distance in square problem...

If I have a square of length n (the center of the square is on the origin), then if in the interior and boundary of the square, I have the point (x,y), and an angle theta, how do I get the distance of the length of the line connecting (x,y) to the edge of the square in the direction of theta?

2. ## Re: Help with distance in square problem...

Start by calculating the length of the line from the origin to the edge:

For theta between -45 and +45 degrees h= n/(2 cos(theta)),
For theta between 45 and 135 degrees h = n/(2 sin(theta))
For theta between 135 and 225 degrees h = -n/(2 cos(theta)),
For theta between 225 and 315 degrees h = -n/(2 sin(theta))

The distance from the origin to (x,y) is sqrt(x^2+y^2). Hence the length from (x,y) to the edge is h-sqrt(x^2+y^2).

3. ## Re: Help with distance in square problem...

This works nicely, but I realized my problem was more complex . I shall restate the question.

In the cube in a 3d grid, where the origin is the center of the cube. If I have an arbitrary point on the cube (x1,y1,z1), and a direction vector (x2,y2,z2), and half of the length of the cube is n (so (2n)^3 = volume of cube), how do I get the distance of the line that connects the (x1,y1,z1) point to the edge of the cube in the direction of the directional vector (x2,y2,z2)?

Thanks

4. ## Re: Help with distance in square problem...

hello sneaky. Sorry for my delay in getting back to you on this.

Let's call the unit vector that defines the direction from the point (x2,y2,z2) to be $a \hat i + b \hat j + c \hat k$, where $\hat i$, $\hat j$, and $\hat k$ represent the unit vectors in the x, y and z direction, respectively, and the magnitudes of a, b and c are:

$a = \frac {x_2} {\sqrt{ x_2^2 + y_2^2 + z_2^2}}$
$b = \frac {y_2} {\sqrt{ x_2^2 + y_2^2 + z_2^2}}$
$c = \frac {z_2} {\sqrt{ x_2^2 + y_2^2 + z_2^2}}$

Now we need to find the factor L that makes the vector $L \times (a \hat i + b \hat j + c \hat k)$ run into a face of the cube. This requires comparing the factors L for three cases: when the vector reaches the plane $x = \pm n/2$, when it reaches $y = \pm n/2$, and when it reaches $z = \pm n/2$. Let's call these factors Lx, Ly, and Lz. Then:

1. Distance to $x = \pm n/2$:
If a>0, $L_x = \frac {(n/2-x_1)} a$

If a<0, $L_x = \frac {(-n/2-x_1)} a$

If a = 0, then $L_x = \infty$

2. Distance to $y = \pm n/2$:

If b>0, $L_y = \frac {(n/2-y_1)} b$

If b<0, $L_y = \frac {(-n/2-y_1)} b$
If b = 0, then $L_y = \infty$

3. Distance to $z = \pm n/2$:
If c>0, $L_z = \frac {(n/2-z_1)} c$

If c<0, $L_z = \frac {(-n/2-z_1)} c$

If c = 0, then $L_z = \infty$

Now compare the values for $L_x$, $L_y$ and $L_z$, and pick the smallest one - that's the distance to the nearest face of the cube.

5. ## Re: Help with distance in square problem...

I have two questions

1) Does the n value here represent half the length of a side of the cube or the full length?

2) Your last statement says "that's the distance to the nearest face of the cube. ". So then which is the distance that connects the point to the intersection of the cube in the direction of the directional vector?

6. ## Re: Help with distance in square problem...

Originally Posted by Sneaky
I have two questions

1) Does the n value here represent half the length of a side of the cube or the full length?
n = the length of a side (i.e., the center of the faces of the cube are each n/2 from the origin)

Originally Posted by Sneaky
2) Your last statement says "that's the distance to the nearest face of the cube. ". So then which is the distance that connects the point to the intersection of the cube in the direction of the directional vector?
What I meant is that this is the distance in the direction (x2,y2,z2) to intersect a face.

7. ## Re: Help with distance in square problem...

OK ebaines, what's e^e * i^i - SIN(60)/100 - pi ?

8. ## Re: Help with distance in square problem...

Originally Posted by Wilmer
OK ebaines, what's e^e * i^i - SIN(60)/100 - pi ?
Pretty cute - one answer is quite close to zero: I figure about 8.7E-6. This is if you use $i^i = e^ {-\pi/2} = 0.20787..$. But of course $i^i$ has multiple values - it could take on any value $e^{(-\pi/2 \pm 2 k \pi)}$ for k = 0, 1, 2, ...