Site Tools

Random Surface Points

Developer: RhinoScript
Summary: Generating random points on a surface using RhinoScript


I want to create a certain number of random points from a surface. How do I do that?


Follow these steps:

  1. Pick the surface - GetObject
  2. Get the surface domain in both the u and v directions - SurfaceDomain
  3. Generate random u,v parameters that fall within the surface domain - Rnd
  4. Evaluate the surface that these parameters - EvaluateSurface
  5. Verify the resulting points lie in the surface - IsPointOnSurface
  6. Add the points to the document - AddPoint

The following RhinoScript example demonstrates this.

 Sub RandomSurfacePoints()
   Dim srf, num, cnt
   Dim dom_u, dom_v, uv(1), pt
   srf = Rhino.GetObject("Select surface", 8, True, True)
   If IsNull(srf) Then Exit Sub
   num = Rhino.GetInteger("Number of points", 10, 1)
   If IsNull(num) Then Exit Sub
   Call Rhino.EnableRedraw(False)
   dom_u = Rhino.SurfaceDomain(srf, 0)
   dom_v = Rhino.SurfaceDomain(srf, 1)
   cnt = 0
   While (cnt < num)
     uv(0) = Rnd() * (dom_u(1) - dom_u(0)) + dom_u(0)
     uv(1) = Rnd() * (dom_v(1) - dom_v(0)) + dom_v(0)
     pt = Rhino.EvaluateSurface(srf, uv)
     If (Rhino.IsPointOnSurface(srf, pt) = True) Then
       Call Rhino.AddPoint(pt)
       cnt = cnt + 1
     End If
   Call Rhino.EnableRedraw(True)
 End Sub

developer/scriptsamples/randomsurfacepoints.txt ยท Last modified: 2020/08/14 (external edit)