2
$\begingroup$

I've been trying to plot spheres and points, given the (c_x, c_y, c_z) centre coordinates of the sphere and (x, y, z) coordinates of the points, like this:

    Show[Graphics3D[
  {
   {PointSize[Medium], Point[{{-261.6645, 2.3550077, -2.069712}}]},
   {PointSize[Medium], Point[{{-263.8915, 0.4822703, -2.371802}}]},
   {Opacity[0.5], Red, Sphere[{-260.072, 3.06565, -1.28551}, 3.4/2]},
   {Opacity[0.5], Sphere[{-262.5363, 0.7417043, -2.528322}, 3.4/2]}
   }
  ],
 Axes -> True]

My goal now is to do the same but for more points and spheres and in a more automatic way: I have a file (points.txt) that has the (x,y,z) values of all points I want to plot, each coordinate has its column, and another file (sphere.txt) which has the coordinates of the centre of every sphere I also want to plot (again each coordinate is in a separate column)

Is there an easy way to indicate that Graphics3D (or another function?) must plot every point from points.txt and sphere in sphere.txt?

Points.txt:

ID  X   Y   Z
0   -457.7842   0   0
1   -394.908    0   0
2   -345.2658   0   0
3   -326.8692   0   0
4   -256.3814   0   0
5   -203.4865   0   0
6   -132.9539   0   0
7   -63.00377   0   0
8   185.97179   0   0
9   209.24666   0   0
10  438.78941   0   0
11  -62.70032   0.8898561   1.9836796
12  -57.64477   2.9849146   3.2990784
13  -256.9517   0.3147554   0.310139

Spheres.txt

SphereID  X   Y    Z
0   -263.7371   0.2212362   -1.553052
1   -393.6699   0.3578  -2.478324
2   -263.3724   -7.979184   -0.449041
3   -261.0845   3.0958525   1.9232374
4   -256.401    -1.259318   -0.081782
5   186.51078   1.0394365   -0.505206
$\endgroup$
8
  • $\begingroup$ Please post parts of your data point.txt and sphere.txt. $\endgroup$
    – cvgmt
    Aug 31, 2022 at 10:42
  • $\begingroup$ Hi @cvgmt, I edited the main post to include what you asked. Thank you. $\endgroup$
    – Joao
    Aug 31, 2022 at 11:22
  • 4
    $\begingroup$ points = Import["points.txt", "Data"][[2 ;; -1]][[;; , 2 ;; -1]]; spheres = Import["sphere.txt", "Data"][[2 ;; -1]][[;; , 2 ;; -1]]; Graphics3D[{{PointSize[Medium], Point@points}, {Opacity[0.5], Red, Sphere[#, 3.4/2]} & /@ spheres}, Axes -> True] $\endgroup$
    – cvgmt
    Aug 31, 2022 at 11:52
  • 1
    $\begingroup$ @Joao Read the Part function in document (use F1 key )can find such usage. For example, {x,a,b,c}[[2;;-1]] get {a,b,c}. $\endgroup$
    – cvgmt
    Aug 31, 2022 at 14:15
  • 1
    $\begingroup$ Welcome to the Mathematica Stack Exchange. The introductory book written by the inventor is a good learning resource. There is a fast intro for math students as well as a fast intro for programmers to choose from. $\endgroup$
    – Syed
    Aug 31, 2022 at 14:40

1 Answer 1

3
$\begingroup$

Since the data contain the firt row and first colum,we need to remove it,so we extract the data from the 2 row to the end -1 and from 2 colum to the end -1. For example

data = {{x, x, x, x}, {y, 1, 2, 3}, {y, 4, 5, 6}, {y, {7, 8, 9}}}
data[[2;;-1,2;;-1]]

{{1, 2, 3}, {4, 5, 6}, {{7, 8, 9}}}

points = Import["points.txt", "Data"]
points = points[[2 ;; -1, 2 ;; -1]]
spheres = 
 Import["sphere.txt", "Data"][[2 ;; -1, 2 ;; -1]]; 
Graphics3D[{{PointSize[Medium], 
   Point@points}, {Opacity[0.5], Red, Sphere[#, 3.4/2]} & /@ spheres},
  Axes -> True]
$\endgroup$
1
  • $\begingroup$ Thank you @cvgmt, your previous comment pointing to where to read also helped! $\endgroup$
    – Joao
    Sep 2, 2022 at 7:24

Your Answer

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

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