File: lines_intersect.py

package info (click to toggle)
vedo 2025.5.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 10,404 kB
  • sloc: python: 64,792; javascript: 1,932; xml: 437; sh: 139; makefile: 6
file content (25 lines) | stat: -rw-r--r-- 904 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
"""Find the intersection points of two coplanar lines"""
import numpy as np
from vedo import *

p1, p2 = (-1,-1,0), (10,2,0)

x = np.linspace(0,10, 50)
y = np.sin(x)*4
pts = np.c_[x,y]

# create 2 lines and assign some arbitrary rotations
line1 = Spline(pts).lw(5).c('black').rotate_y(10).rotate_x(15)
line2 = Line(p1,p2).lw(5).c('green').rotate_y(10).rotate_x(15)

# make a small extrusion of line1 and intersect it with line2:
ds = line1.diagonal_size()*0.02  # 1% tolerance
pint = line1.extrude(ds).shift(0,0,-ds/2).intersect_with_line(line2)
ps = Points(pint, r=15).c('red')

# lets fill the convex area between the first 2 hits:
id0 = line1.closest_point(pint[0], return_point_id=True)
id1 = line1.closest_point(pint[1], return_point_id=True)
msh = Line(line1.points[id0:id1]).triangulate().lw(0).shift(0,0,-0.01)

show(line1, line2, ps, msh, __doc__+f"\narea = {msh.area()} cm:^2", axes=1).close()