-1

I need some help with the math behind an assignment i was given. The problem was to calculate the distance from the ant to the candy. The candy is always on top of the box and the ant can be anywhere but the sides. The ant can craw on the top and the sides but CANNOT fly. They give you the coordinates of the ant and the candy as 6 numbers in a row. The first 3 are the ant's the next 3 are the candy's. The problem i am having is on the 3rd sample input (0 0 0 5 4 3.0). How do they get 8.60 units as the answer? When the distance up is 3 and the distance to the corner is 6.40 so the total should be 9.40. My guess is they cut the corner while going up but im not sure how to make a formula for the to find the shortest length like that. Thanks for your help :D if you need more information just ask.

The box is (5,4,3) in x,z,y format.

Sample Input:                       
3 1 3 3 3 3            
2.25 0 2 2.5 2 3          
0 0 0 5 4 3.0   
0 4 3 5 0.0 3        
5 0 3 5 4.00 3           

Sample Output:               
Shortest distance = 2.00 units            
Shortest distance = 3.01 units        
Shortest distance = 8.60 units             
Shortest distance = 6.40 units            
Shortest distance = 4.00 units
Khalob C
  • 147
  • 1
  • 1
  • 10
  • Belongs on http://math.stackexchange.com/ – Aditya May 10 '13 at 17:07
  • I'm sorry i failed to mention but this was part of a programming assignment, i just needed help with the math so i guess it would have been more acquit to go to the math.stachexchange.com but i figured since it was a program it would be ok to post here. – Khalob C May 10 '13 at 20:50

1 Answers1

1

First, this is quite clearly in the wrong area, as gap_j has pointed out. That said, the answer lies in calculus -- you'll have to set the derivative equal to zero and identify the minimum(s). Based on the dimensions you've provided and an inference or two, the box is 5 units on the x-axis, 4 units on the y-axis, and 3 units on the z-axis. This means that, as you note, the shortest distance by traveling first up the z-axis would be 9.40 units:

p(z) = z + sqrt(x2 + y2)

There are two other options, with respect to traveling directly along an axis first; x-first and y-first:

p(x) = x + sqrt(y2 + z2)

p(y) = y + sqrt(x2 + z2)

These paths have values of 10 (exactly) and 9.83, respectively.

In order to achieve the p = 8.6 given in the problem, there would have to be some distance a along the x-axis such that:

p(a) = sqrt((x - a)2 + z2) + sqrt(y2 + a2)

else some distance b along the y-axis such that:

p(b) = sqrt((y - b)2 + z2) + sqrt(x2 + b2)

and the values p(a) or p(b) must be less than traveling directly along an axis. There are myriad such values for p(a).

Since this is presumably a homework problem for a calculus class, I'll leave finding the derivative to you, but the formulas are as provided. There is only one variable, so this shouldn't be particularly difficult. These can be generalized, of course, and it's a fairly simple task to calculate the results and identify the shorter path.

Community
  • 1
  • 1
cabbagery
  • 909
  • 7
  • 16
  • 1
    Sorry -- I left my browser open too long; it was open when I began the response. – cabbagery May 10 '13 at 19:52
  • Sorry for what? Anyways.. thanks for your help. I haven't quite figured out how to do it but I'am greatful for the big post you gave me. I actually haven't taken Calculus yet so that's why i was asking for help. Hopefully i can, thanks again. – Khalob C May 10 '13 at 20:53
  • Yeah i just spent 15mins trying to figure it out, but i couldn't grasp the idea. I'm sorry, I am currently in pre-calc and i have never used this stuff before. Could you help me a bit further? The program i need to write needs to find the shortest distance from the ant to the candy. The coordinates of the ant and the candy are both given but i need to find the shortest distance. What equation(s) would i use to find the different distances from the ant to the candy and compare them to find the smallest one? – Khalob C May 10 '13 at 21:11
  • ok i understand the first three formulas but the next two are confusing to me. I need a bit more help with those, ill keep working it out on paper to see if i can work anything out. – Khalob C May 10 '13 at 21:20
  • 1
    I apologized to mods -- your question was closed as off-topic. I'll still respond here in comments, though, time permitting. The two `p(a)` and `p(b)` formulas are based on the ant beginning on a corner (as in the third sample). If we're not interested in paths directly along an axis (on an edge), we'll be embarking across the surface (either the x-z plane or the y-z plane). Sketch a 3-D box, and label the axes. Place a point _a_ somewhere along the x-axis, and a point _b_ somewhere along the y-axis. It doesn't matter where. This point represents the destination on the top edge of the box. – cabbagery May 10 '13 at 22:45
  • 1
    The formulas represent the two components of the whole path: the portion along the [x-z or y-z] plane (the side), and the portion along the x-y plane (the top). The first term is the side path, the second is the top path. If you're only in a pre-Cal class, and you're expected to figure out how to code this, your instructors are dicks. This sort of problem requires a level of abstraction not usually available to beginning programmers, and while taking the derivative is fairly easy, actually solving this is a pain in the ass. – cabbagery May 10 '13 at 23:00
  • so then when i did both of those equations on example 3 i got 9.4 as the first answer and 10 as the second.. both are not equal to 8.3 (the correct answer).. so how do i get to 8.3 from (0,0,0) to (5,4,3)? Thanks for your time man, you really are awesome. – Khalob C May 10 '13 at 23:02
  • 1
    Assuming you're telling me the truth, [here](http://www.wolframalpha.com/input/?i=sqrt%28%285-a%29^2+%2B+9%29+%2B+sqrt%2816+%2B+a^2%29) is the Wolfram|Alpha analysis of the problem. You'll want to focus on "global minimum". To generalize this for any legal pair of starting positions, you'll have to modify the two `p(a)` and `p(b)` formulas to accept the variable inputs (and let Wolfram|Alpha do the work of solving it) -- but I have to go home, so I'll leave that to you. I may check back over the weekend, but no promises. – cabbagery May 10 '13 at 23:11
  • ok, but i need to know how wolframalpha does the work so that i can use it in my program. As in, how does it find the "global minimum" since that is exactly what i want. – Khalob C May 11 '13 at 00:21
  • 1
    let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/29763/discussion-between-cabbagery-and-khalob-c) – cabbagery May 11 '13 at 01:38