如何绘制一条穿过点或到达点的线,其中线的长度始终相同(iPhone应用程序开发)
我有一个带有径向光标的圆形图表,光标移动到您在 iPhone 屏幕上触摸的位置,但我需要光标保持相同的长度,即使触摸发生在图表原点附近或外部图表。我不知道该怎么做。任何帮助将不胜感激
I have a circular graph with a radial cursor on it, and the cursor moves to where you touch on the iPhone screen, but I need the cursor to remain the same length, even if the touch occurs close to the origin of the graph or outside the graph. I have no idea how to do this. Any help would be greatly appreciated
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
确定您想要线的起点和终点之间的角度,然后从起点开始画一条您喜欢的任意长度的线,直到您想要的长度为止?或者我误解了这个问题?
反正切( (y2-y1)/(x2-x1) )
Determine the angle between where you want the line to start and the end point, then draw a line of whatever length you like starting from the start point and going only as far as you want the length? Or did I misunderstand the question?
arctangent( (y2-y1)/(x2-x1) )
我不熟悉这些方法,但是如果您根据角度找到合适的点,
CGContextAddLineToPoint
应该与@eruciform的技术一起使用。angle = arctan((y2 - y1) / (x2 - x1))
x = cos(angle) * length
y = sin(angle) * length
并绘制从 (x1, y1) 到 (x, y) 的线。
或者,您可以仅利用您所绘制的线与原始点形成的线成比例这一事实,并执行一些勾股定理:
原始长度 = sqrt((y2 - y1)^2 + (x2 - x1) ^2)
比率 = 所需长度 / 原始长度
x = ((x2 - x1) * 比率) + x1
y = ((y2 - y1) *ratio) + y1
再次绘制从 (x1, y1) 到 (x, y) 的直线。
I'm not familiar with these methods, but
CGContextAddLineToPoint
should work with @eruciform's technique if you find the appropriate point based on the angle.angle = arctan((y2 - y1) / (x2 - x1))
x = cos(angle) * length
y = sin(angle) * length
And draw the line from (x1, y1) to (x, y).
Alternatively, you could just use the fact that the line you're drawing is proportional to the one formed by the original point, and do some Pythagorean stuff:
original length = sqrt((y2 - y1)^2 + (x2 - x1)^2)
ratio = desired length / original length
x = ((x2 - x1) * ratio) + x1
y = ((y2 - y1) * ratio) + y1
Again, draw the line from (x1, y1) to (x, y).