Detect if drawing a path is a circle/rectangle in xcode

做~自己de王妃 提交于 2019-11-28 05:10:42

问题


I want to implement a auto complete feature for a drawing app. Once a free hand object is drawn, I want to detect the type of object (circle/rectangle/triangle) and based on the result would want to plot a corresponding object.

I have read up a bit about OpenCV but then I would need to convert the user drawing into an image at real time. I am recording the number of points plotted/traced by the touch and also generate a UIBeizerPath of the corresponding path. How do I go about to detecting the shape type?


回答1:


You need to segment the data points first. Google on "stroke segmentation" to find related articles. One simple and fast algorithm is to compute the forward slope and backward slope for each data point, then compute the turning angle between forward slope and backward slope. If the turning angle is greater than a certain angle threshold, then you can assume that your path takes a sharp turn there. From the number of sharp turns computed, you can infer whether the points represent a triangle (with 2 sharp turns), a quadrilateral (with 3 sharp turns) or something else. To infer that the data points represent a circle or a rectangle, you will need to do additional computation. For example, if there is no sharp turns at all, do a circle fitting to the data points to see if the maximum error to the fitted circle is smaller than a certain tolerance. To output a rectangle, you will have to fit straight lines to each segment of data points and check if the fitted lines are more or less orthogonal to each other.




回答2:


You can iterate through UIBezierPath points with CGPathApply(..) method. Look here for example.

But you should determine shape type somehow - it's mathematical task and approach depends on your input data.



来源:https://stackoverflow.com/questions/27622358/detect-if-drawing-a-path-is-a-circle-rectangle-in-xcode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!