lines_intersect.py 904 B

12345678910111213141516171819202122232425
  1. """Find the intersection points of two coplanar lines"""
  2. import numpy as np
  3. from vedo import *
  4. p1, p2 = (-1,-1,0), (10,2,0)
  5. x = np.linspace(0,10, 50)
  6. y = np.sin(x)*4
  7. pts = np.c_[x,y]
  8. # create 2 lines and assign some arbitrary rotations
  9. line1 = Spline(pts).lw(5).c('black').rotate_y(10).rotate_x(15)
  10. line2 = Line(p1,p2).lw(5).c('green').rotate_y(10).rotate_x(15)
  11. # make a small extrusion of line1 and intersect it with line2:
  12. ds = line1.diagonal_size()*0.02 # 1% tolerance
  13. pint = line1.extrude(ds).shift(0,0,-ds/2).intersect_with_line(line2)
  14. ps = Points(pint, r=15).c('red')
  15. # lets fill the convex area between the first 2 hits:
  16. id0 = line1.closest_point(pint[0], return_point_id=True)
  17. id1 = line1.closest_point(pint[1], return_point_id=True)
  18. msh = Line(line1.points[id0:id1]).triangulate().lw(0).shift(0,0,-0.01)
  19. show(line1, line2, ps, msh, __doc__+f"\narea = {msh.area()} cm:^2", axes=1).close()