 # 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…

–Mitch

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.

thanks

Arash

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])
srf = planarBrep.Faces
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,domU)
v = random.uniform(domV,domV)
if (srf.IsPointOnFace(u,v) == inside) or (srf.IsPointOnFace(u,v) == border):
pts.append(srf.PointAt(u,v))
thank you very much guys , i did what i wanted to do and in a very efficient way 