0
$\begingroup$

I'm trying to plot the closest point in the plane to the point P (the one that can be dragged) but in the graph appears 3 points.

in the graph I'm plotting a plane and a point P, I want to add another point Q which is the closest point between the point P and the plane, and a line that connect between them

*Note: the code might be a little bit messy *I can't add all the code because its too long...

the code :

    Manipulate[
 DynamicModule[{pts = {{2, 0, 2}, {0, 2, 1}}, index = 0, pos0, pos1, 
   AV1Cap1Obj, F, P, t, Q, normal, sol0, Q1},
  
  F[x_, y_] := (a*x0 + b*y0 + c*z0 - a*x - b*y)/c;
  
  
  AV1Cap1Obj[var_] := 
   If[var == 1, {Thickness[0.0001], Arrowheads[0.00], 
     Arrow[{{0, 0, 0}, {0, 0, 0}}]}, 
    If[var == 2, {PointSize[0.04], Point[Dynamic@pts[[2]]], 
      FaceForm[], EdgeForm[], 
      Cuboid[Dynamic[pts[[2]] - {0.5, 0.5, 0.5}]]}]];
  
  P = Dynamic[pts[[2]]];
  normal = {a, b, c};
  sol0 = 
   Flatten[ 
    Solve[{0, 0, 
        1} . (P + t normal) == (a*x0 + b*y0 + c*z0 - 
         a*{1, 0, 0} . (P + t normal) - b*{0, 1, 0} . (P + t normal))/
       c, t ]];(*return {t->value}*)
  Q1 = P + t normal /. sol0;
  
  
  EventHandler[
   
   Show[{
     Graphics3D[{
       MouseAppearance[EventHandler[
           
           Mouseover[{(*Base expr,mouse out*)
             Dynamic[If[index === #, Red, Purple]], AV1Cap1Obj[#]},
            {(*Mouse over*)
             Dynamic[If[index === 0 || index === # , Red, Purple]], 
             AV1Cap1Obj[#]}](*Mouse Over*),
           
           {"MouseDown" :> (index = #;
              pos0 = pts[[index]])},
           PassEventsUp -> True] & /@ 
         Range[Length[pts]](*Event Handler Local,#1,2,...*), 
        "Arrow"]}
      , PlotRange -> {{-10, 10}, {-10, 10}, {-10, 10}}, 
      ImageSize -> Large, Axes -> True](*Graphics3D*),
     
     (*new *)
     If[a == b == c == 0,
      Graphics3D[{Thick, Purple, Sphere[{x0, y0, z0}, .25]}, 
       PlotRange -> {{-10, 10}, {-10, 10}, {-10, 10}}, 
       PlotLabel -> 
        Style["A plane can only be described by a non-zero vector.", 
         16, Red], Axes -> True, Boxed -> False, 
       AxesOrigin -> {0, 0, 0}, SphericalRegion -> True, 
       ImageSize -> Large],
      
      Show[{
        
        Graphics3D[{Red, Thick, 
          Arrow[{{x0, y0, z0}, {x0, y0, z0} + 0.6 {a, b, c}}],
          
          Text[Style["P", 14, Black], {0.5, 0.5} + 
            Dynamic[pts[[2]]], {1, 0}],
          {Blue, Thickness[0.01], Line[{Q1, {pts[[2]]}}]},
(*here Im trying to plot point Q1 but it appears 3 points*)
          {Black, PointSize[0.02], Point[Q1]}

enter image description here

thanks in advance.

$\endgroup$
2
  • 1
    $\begingroup$ The code is not complete. $\endgroup$
    – cvgmt
    Commented Sep 29, 2022 at 13:40
  • $\begingroup$ I know that because it's too long, I can't post the whole code... I get an error message when I put all of it, I'm trying to add the whole code $\endgroup$
    – Tt Tt
    Commented Sep 29, 2022 at 18:05

1 Answer 1

2
$\begingroup$

You can probably use RegionNearest. For example,

(* given some plane region... *)
plane = InfinitePlane[{{1, 0, 0}, {0, 2, 0}, {0, 0, 3}}];

(* ...and some point not on the plane... *)
point = {0, 0, 0};

(* ...you can use NearestRegion: *)
nearest = RegionNearest[plane, point]
(* {36/49, 18/49, 12/49} *)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.