以圆心为中心在圆内画一个矩形
Draw a rectangle inside a circle with respect to its center
我正在尝试以给定的角度在圆上放置一个 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-offset
:
中减去 half 矩形的高度
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
您打算变换的任何形状都应该相对于预期的旋转和缩放原点创建。
我正在尝试以给定的角度在圆上放置一个 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-offset
:
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
您打算变换的任何形状都应该相对于预期的旋转和缩放原点创建。