#pragma TextEncoding = "UTF-8" #pragma rtGlobals=3 #pragma version=1.02 #include // by tony.withers@uwo.ca // Description // draws a polgon around a cloud of data points on a plot. // Usage // Right click on a data point to invoke the trace menu // and select "Encircle points". // If you reshape the graph window you can repeat this procedure // to update the shape of the polygon. // Selecting "Remove Encircled Region" deletes the shaded region. // Polygons are drawn in the user back layer and can be edited // with the drawing tools. // Details // Polygon surrounds all of the data in the wave. // It makes most sense to apply this to x-y data. // Version history // 1.02 deal with offsets // 1.01 bug fix: remove gratuitous print statement Menu "TracePopup" "Encircle points", /Q, DrawCatmullRomBoundingSpline() "Remove encircled region", /Q, RemoveBoundingSpline() end function DrawCatmullRomBoundingSpline() // initialize (or reinitialize) the package data folder NewDataFolder /O root:Packages NewDataFolder /O root:Packages:Encircle // Create a data folder reference variable DFREF dfr = root:Packages:Encircle // figure out graph and trace names GetLastUserMenuInfo string s_info=traceinfo(S_graphName, S_traceName, 0) //print s_info if(numtype(NumberByKey("AXISZ",s_info))==0) doalert 1, "you've selected a contour trace. Continue?" if (v_flag==2) return 0 endif endif if(strlen(stringbykey("XWAVE",s_info))==0) duplicate /free tracenametowaveref(S_graphName, S_traceName), xdata xdata=x else wave xdata=XWaveRefFromTrace(S_graphName, S_traceName) endif wave ydata=tracenametowaveref(S_graphName, S_traceName) // number of points to calculate between nodes variable nPoints=20 variable alpha=0.5 // alpha=0 for standard (uniform) Catmull-Rom spline // alpha=0.5 for centripetal Catmull-Rom spline // alpha=1 for chordal Catmull-Rom spline if (numpnts(ydata)<3) doalert 0, "must have more than two data points" return 0 endif Convexhull xdata,ydata wave W_XHull=W_XHull, W_YHull=W_YHull // deal with offsets W_XHull+=GetNumFromModifyStr(s_info,"offset","{",0) W_YHull+=GetNumFromModifyStr(s_info,"offset","{",1) variable vertices=numpnts(W_XHull) make /free/n=(vertices+3) x_hull, y_hull x_hull=W_XHull[p-(vertices)*(p>(vertices-1))] y_hull=W_YHull[p-(vertices)*(p>(vertices-1))] // find the centroid of the hull variable x0=0, y0=0, varA=0 variable i, factor for(i=0;i(np-1))] y_temp=ydata[p-(np-1)*(p>(np-1))] make /free /n=(segPoints) x_out, y_out make /free /n=4 x_in, y_in variable i for(i=1;i