fitline.py 846 B

123456789101112131415161718192021222324252627
  1. """Draw a line in 3D that fits a cloud of 20 Points,
  2. Show the first set of 20 points and fit a plane to them."""
  3. from vedo import *
  4. # declare the class instance
  5. plt = Plotter(axes=1)
  6. # draw 500 fit lines superimposed and very transparent
  7. for i in range(500):
  8. x = np.linspace(-2, 5, 20) # generate every time 20 points
  9. y = np.linspace(1, 9, 20)
  10. z = np.linspace(-5, 3, 20)
  11. data = np.stack((x,y,z), axis=1)
  12. data+= np.random.normal(size=data.shape) * 0.8 # add gauss noise
  13. plt += fit_line(data).lw(4).alpha(0.04).c("violet") # fit a line
  14. # 'data' still contains the last iteration points
  15. plt += Points(data).color("yellow").ps(10)
  16. print("Line 0 Fit slope = ", plt.objects[0].slope)
  17. plane = fit_plane(data).c("green4") # fit a plane
  18. print("Plane Fit normal =", plane.normal)
  19. plt += plane, __doc__
  20. plt.show().close()