# Attempting to find the local minimum of a plane

I'm trying to find the local minimum of a plane. This is being done algebraically because I'm doing it in a computer program for a game I'm making. Sadly, I suck at math and am only in algebra II so I probably screwed a lot up with my minimal knowledge of calculus. When testing, it got a completely random spot and here is the math I used to get there :

$(x,y,z)$=point finding

$(q,r,s)$=player position

$(a,b,c,d)$=plane stuff

$ax+by+cz=d \Longrightarrow z=dx+\frac{-b}{c}y-\frac{a}{c}$

$dist = \sqrt{(x-q)^2+(y-r)^2+(z-s)^2}$

$dist^2 = f(x,y) = (x-q)^2+(y-r)^2+\Bigl((dx+\frac{-b}{c}y+(\frac{a}{c}-s)\Bigr)^2$

$x = 2(x-q)+r^2(dx+\frac{-b}{c}y+(\frac{a}{c}-s))=2x-2q+r^2dx+r^2\frac{-b}{c}y+r^2(\frac{a}{c}-s)$

$x = (2x+r^2dx+r^2\frac{-b}{c}y) + 2q+r^2(\frac{a}{c}-s)$

$x = (2+r^2d)x+(r^2\frac{-b}{c})y + 2q+r^2(\frac{a}{c}-s)$

$x = \frac{r^2\frac{-b}{c}}{-((2+r^2d)-1)}y+2q+r^2\frac{\frac{a}{c}-s}{-((2+r^2d)-1)}$

$y = (x-q)^2+(y-r)^2+(dx+\frac{-b}{c}y+(\frac{a}{c}-s))^2$

$y = 2(y-r)+q^2(dx+\frac{-b}{c}y+(\frac{a}{c}-s))$

$y = 2y-2r+q^2dx+\frac{-b}{c}q^2y+(\frac{a}{2}-s)q^2$

$y = (2y+q^2dx+\frac{-b}{c}q^2y) + 2r+(\frac{a}{2}-s)q^2$

$y = (2+\frac{-b}{c}q^2)y + q^2dx + 2r+(\frac{a}{2}-s)q^2$

$y = \frac{q^2d}{1+\frac{-b}{c}q^2}x + \frac{2r+(\frac{a}{2}-s)q^2}{1+\frac{-b}{c}q^2}$

--new variables for simplification

$m=2q+r^2\frac{\frac{a}{c}-s}{-((2+r^2d)-1)}$

$g=\frac{r^2\frac{-b}{c}}{-((2+r^2d)-1)}$

$n=\frac{2r+(\frac{a}{2}-s)q^2}{1+\frac{-b}{c}q^2}$

$h=\frac{q^2d}{1+\frac{-b}{c}q^2}$

--substitute

$y = h(gy+m)+n$

$y = hgy+hm+n$

$y = \frac{hm+n}{-(hg-1)}$

$x = gy+m$

-
Your first equation, $ax + by + cz = d$, is not equivalent to $z=dx + (-b/c)y - (a/c)$. I'm assuming => was meant to be an implication arrow... –  Arturo Magidin Nov 13 '11 at 8:50
@Arturo Magidin Yeah sorry, I was trying to go off of an example I had seen. –  SDuke Nov 13 '11 at 16:05

I'll assume that Joriki's interpretation is right and you want to find the point on the plane $ax+by+cz=d$ that is closed to $(q,r,s)$. You can do that by minimizing the (squared) distance using calculus, but it is much easier to apply a little geometry.

The line between your target point and $(q,r,s)$ must be perpendicular to the plane; otherwise you could move the target point closer. Therefore what you want to do is draw the line through $(q,r,s)$ that is normal to the plane, and find its intersection with the plane. The equation for the plane directly gives us the direction of the normal, namely $(a,b,c)$. So we can write a parametric equation of the line: $$t\mapsto (q,r,s)+t(a,b,c) = (q+ta, r+tb, s+tc)$$ Finding the intersection is then just a matter of inserting into the plane's equation: $$(q+ta)a + (r+tb)b + (s+tc)c = d$$ and solve for $t$, which easily gives $t=\frac{d-qa-rb-sc}{a^2+b^2+c^2}$. Plug that into your parametric equation, and you have your target point.

-
thanks for that, it makes since up until the plugging into the parametric. Once again, algebra II sorry. I understand what a parametric equation is, but that looks like I would only get one number out of it? –  SDuke Nov 13 '11 at 16:38
No, you get $(q+ta, r+tb, s+tc)$, which is a point. (There's nothing left to do except arithmetic because you now know the value of $t$). –  Henning Makholm Nov 13 '11 at 16:42
Ok thank you, I thought that could be it but I wasn't entirely certain. –  SDuke Nov 13 '11 at 16:45

You're not attempting to find the local minimum of a plane; there's no such thing. It seems that you're trying to find the point on a plane at least distance from the player position. You made an error at the very beginning in solving the plane equation for $z$; the result is $z=(d-ax-by)/c$. I suggest you try again using that, and if you don't succeed, present your work in a more systematic form, not just writing lots of equations but explaining what you did, and using $\TeX$ to make the equations more readable (by enclosing them in dollar signs).

-
+1 on "not just writing lots of equations but explaining what you did". The TeXing can reasonably be left to editors if the question is otherwise clear; I'd rather see ASCII equations than TeXed math that use $*$ as a multiplication sign. –  Henning Makholm Nov 13 '11 at 15:13
@joriki Thank you, I'm sorry I couldn't get it to look that way, I was trying to but couldn't figure it out. I changed it for you. Also I was going off of some example I had seen, they did it with all numbers and that's what it appeared to be, so that's what I went with. Thank you for the correction. –  SDuke Nov 13 '11 at 16:10