3
$\begingroup$

When I create a Graphics3D, I often want to place an object right at the centre of the box. Initially I thought that the centre was specified by {0, 0, 0}, but I soon realised that this is actually the corner.

I've realised that I can find the centre by dividing the dimensions of the box in half. For example if

PlotRange -> {{0, 100}, {0, 100}, {0, 100}}

then if I want to put my Sphere dead centre I'll put it at:

Sphere[{100/2, 100/2, 100/2, 1]

This is all very well if I explicitly know the dimensions of the box, but if I don't know the PlotRange, how can I ensure that my object is placed right at the centre?

Is there a way to tell the size of the PlotRange if you haven't specified it explicitly?

For example, consider this function:

balls = Table[Sphere[RandomReal[{0, 100}, 3], 1], {50}]~
   Join~{Sphere[{50, 50, 50}, .05]};
Manipulate[
 Graphics3D[
  balls,
  ViewPoint -> {1.3*10^-zoom, -2.4*10^-zoom, 2*10^-zoom}, 
  SphericalRegion -> True, 
  PlotRange -> {{0, 100}, {0, 100}, {0, 100}}],
 {zoom, 0, 3}]

enter image description here

The slider controls the zoom, which in this function zooms in all the way until you hit a very small sphere. For this to work though, the sphere needs to be precisely at the centre of the plot. To ensure this precision, I manually entered the size of the plot (PlotRange -> {{0, 100}, {0, 100}, {0, 100}}), as well as manually control the placement of the middle sphere (Sphere[{50, 50, 50}, .05]).

Is there a way to make sure that this sphere is placed right at the very centre of the box without needing to manually specify either the dimensions of the PlotRange or the coordinates of the centre sphere?

I hope this clarifies what I'm trying to get at.

$\endgroup$
4
  • 3
    $\begingroup$ Possible duplicate of this. Short answer is ViewCenter->{0.5,0.5,0.5} might be what you are after. $\endgroup$ Commented Apr 17, 2014 at 20:59
  • $\begingroup$ OK, I will try that. But then how can you place an object at the centre? $\endgroup$
    – Aron
    Commented Apr 17, 2014 at 21:16
  • $\begingroup$ The edit helps but I'm confused by the restraint: make sure that this sphere is placed right at the very center of the box without needing to manually specify ... the coordinates of the center sphere. Don't you need to know where the sphere is in order to zoom in to it? $\endgroup$ Commented Apr 17, 2014 at 21:20
  • $\begingroup$ True, but I want to place it at the centre without me needing to find out the total dimensions of the box. E.g. something like {BoxSize/2, BoxSize/2, BoxSize/2}. Alternatively I don't really mind specifying the coordinates of the Sphere, but in order to do that I need to know what the dimensions of the box are. Is there a way to discover what these are without my specifying what they are? I.e. I just want to let Mma decide what size the box should be, but then I want to find out what it is so that I can specify that I want the sphere to be placed at half the width, height and depth $\endgroup$
    – Aron
    Commented Apr 17, 2014 at 21:26

1 Answer 1

4
$\begingroup$

Like bobthechemist says you have to use ViewCenter to specify what point you want to zoom in on without specifying any absolute coordinates. For example in ViewCenter->{0.5,0.5,0.5} the 0.5 means 50 percent of the plot range in the x, y and z direction respectively.

Similarly when you position objects you have to give their position in a number of percent of the plot range. This way, again, you don't have to know what the plot range is. This is realized with Scaled, like this:

balls = Join[
   Table[Sphere[RandomReal[{0, 100}, 3], 1], {50}],
   {Green, Sphere[Scaled[{0.5, 0.5, 0.5}], .05]}
   ];
Manipulate[
 Graphics3D[balls, 
  ViewPoint -> {1.3*10^-zoom, -2.4*10^-zoom, 2*10^-zoom},
  ViewCenter -> {0.5, 0.5, 0.5},
  SphericalRegion -> True
  ], {zoom, 0, 3}
 ]

zoomedin

$\endgroup$
2
  • $\begingroup$ Thank you, @bobthechemist and @Pickett. On a related note, is it possible to specify for one particular sphere to be placed at the centre of the plot? So that for example even if the sphere had originally been placed randomly? For example if my code is: balls = Join[Table[Sphere[RandomReal[{0, 100}, 3], 1], {50}]]; Manipulate[ Graphics3D[ balls, ViewPoint -> {1.3*10^-zoom, -2.4*10^-zoom, 2*10^-zoom}, ViewCenter -> {0.5, 0.5, 0.5}, SphericalRegion -> True], {zoom, 0, 3}], is it possible to make it so that the plot rescales and centres on for example balls[[1]]? $\endgroup$
    – Aron
    Commented Apr 18, 2014 at 0:45
  • $\begingroup$ @Aron If you want that level of control I think you should set the plot range yourself. First create a list of coordinates for the spheres, then find out what the leftmost sphere is, the rightmost sphere etc. Now you have your boundaries. Pick the sphere that's supposed to be in the middle and pad the plot range appropriately. $\endgroup$
    – C. E.
    Commented Apr 18, 2014 at 10:03

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.