How can I draw a straight line, and know how long the line is in pixels?
I am working on an iPad app, and one of the feat开发者_JAVA百科ures that has been requested is the ability to make measurements of an image. With the knowledge that the iPad screen has a 132ppi resolution, it seems as though it will be quite simple to implement this.
But how can I draw a straight line on the iPad? Is there a library that is best? Is core animation, open gl, or quartz what I need? I don't have any experience drawing anything, so if someone can just be like "do this", I'll go figure out how to do it. I want to make it so the user can't draw anything but a straight line, and then when they are done, I need to know how many pixels long the line is.
Please help. Thanks
EDIT I forgot to make this clear, I would like to be able to make it so the line is drawn as the user goes. So they put the finger down, and then maybe a little dot appears, then as they drag, the line gets linger, and when they stop, the line is done.
Please see this answer:
How do I draw a line on the iPhone?
Also, to get the length, implement touchesBegan and touchesEnded, record both CGPoints and calculate the delta.
The distance formula
In code:
CGFloat dx = point2.x - point1.x;
CGFloat dy = point2.y - point1.y;
CGFloat distance = sqrt(dx*dx + dy*dy);
精彩评论