Random point inside a closed curve / python

Hello guys , i have a question : what is the best and most efficient way to generate some random point inside a closed planar curve !?

Dunno, just off the top of my head, I would get a bounding box in the plane of the curve and find the extents (domain) in x and y; then have a loop that generates random x and y values within that domain, create a point with the random values and check for containment in the curve with rs.PointInPlanarClosedCurve() or Rhino.Geometry.Curve.Contains(pt). If the point is inside, you’re done, break out of the loop. If not continue the loop until you do find an inside point…

There are probably more efficient methods, but this one is easy to script…


Hello Mitch , thank you for your comment , i was thinking to a similar solution , but i wanted to evaluate the surface which i generated with rs.addPlanarSrf() , and check if the generated point is inside boundry or not .
i wonder which one is more efficient and aint ther a function to this task !?
Mitch if found easier way to do this please let me know.



Well, if you know that the test point is in the same plane as the surface you can also use rs.IsPointOnSurface().

HTH, --Mitch

The following script would replicate the rs.IsPointOnSurface method Mitch mentioned, just using the native RhinoCommon method:

import rhinoscriptsyntax as rs
import random
import Rhino

randomPtsNum = rs.GetInteger("number of random pts")
curveId = rs.GetObject("pick up the curve")
curveObj = rs.coercegeometry(curveId)
planarBrep = Rhino.Geometry.Brep.CreatePlanarBreps([curveObj])[0]
srf = planarBrep.Faces[0]
domU = srf.Domain(0)
domV = srf.Domain(1)

pts = []
inside =  Rhino.Geometry.PointFaceRelation.Interior
border = Rhino.Geometry.PointFaceRelation.Boundary
while len(pts) < randomPtsNum:
    u = random.uniform(domU[0],domU[1])
    v = random.uniform(domV[0],domV[1])
    if (srf.IsPointOnFace(u,v) == inside) or (srf.IsPointOnFace(u,v) == border):

thank you very much guys , i did what i wanted to do and in a very efficient way :smile:

Mmm…I know that this is an old post but is this not better just to use “Rhino.Geometry.BrepTrim.Contains(crv,pt)” and check it towards Rhino.Geometry.PointContainment.“whatever”??

Just my 2 cents in the discussion.