Ios 在圆内相对于圆心绘制一个矩形
我试图以给定的角度在圆上放置一个Ios 在圆内相对于圆心绘制一个矩形,ios,geometry,cgpath,Ios,Geometry,Cgpath,我试图以给定的角度在圆上放置一个CGPath。路径看起来更像一个矩形。创建完路径后,我移动到圆的中心并从中绘制路径。但我想将矩形的左中心与圆的中心对齐,而不是左上角。我想,我应该用一些公式来计算矩形相对于给定角度的原点,我不知道怎么做 //My logic to draw the circle goes here .... //My custom rect drawing logic is inside the CreatePath method CGPath customRe
CGPath
。路径看起来更像一个矩形。创建完路径后,我移动到圆的中心并从中绘制路径。但我想将矩形的左中心与圆的中心对齐,而不是左上角。我想,我应该用一些公式来计算矩形相对于给定角度的原点,我不知道怎么做
//My logic to draw the circle goes here
....
//My custom rect drawing logic is inside the CreatePath method
CGPath customRectPath = CreatePath(size);
context.TranslateCTM(center.X, center.Y);//I am sure this is where I am doing it wrong
context.RotateCTM((float)(angle));
context.AddPath(customRectPath);
context.DrawPath(CGPathDrawingMode.EOFill);
这张图片应该能解释我想说的话
TranslateCTM
设置矩形左上角的平移,而不是左侧(所需)的中点的平移。要执行此操作,只需将矩形的高度从y偏移量中减去一半即可:
context.TranslateCTM(center.X,center.Y-size.Height/2)代码>您应该创建矩形,使其相对于旋转原点
矩形的拐角处
0, - width / 2 // top left
size , - width / 2 // top right
size, width /2, // bottom right
0, width / 2 // bottom left
您想要变换的任何形状都应该相对于预期的旋转和缩放原点创建。开始我相信您可以自己计算出来。