Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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.

share|improve this question
2  
Possible duplicate of this. Short answer is ViewCenter->{0.5,0.5,0.5} might be what you are after. –  bobthechemist Apr 17 at 20:59
    
OK, I will try that. But then how can you place an object at the centre? –  Aron Apr 17 at 21:16
    
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? –  bobthechemist Apr 17 at 21:20
    
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 –  Aron Apr 17 at 21:26
add comment

1 Answer 1

up vote 3 down vote accepted

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

share|improve this answer
    
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]]? –  Aron Apr 18 at 0:45
    
@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. –  Pickett Apr 18 at 10:03
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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