【问题标题】:Draw a rectangle inside a circle with respect to its center相对于圆心在圆内画一个矩形
【发布时间】:2017-08-14 11:58:30
【问题描述】:

我正在尝试以给定角度将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);

图片应该能解释我想说的话。

【问题讨论】:

  • Start here 我相信你可以自己解决。

标签: ios geometry cgpath


【解决方案1】:

TranslateCTM 设置矩形左上角角的平移,而不是左侧的中点(您想要的)。为此,只需从y-offset 中删除 一半 矩形的高度:

context.TranslateCTM(center.X, center.Y - size.Height / 2);

【讨论】:

    【解决方案2】:

    您应该创建一个相对于旋转原点的矩形。

    例如矩形的角

    0, - width / 2      // top left
    size , - width / 2  // top right
    size, width /2,     // bottom right
    0, width / 2        // bottom left
    

    您要转换的任何形状都应相对于预期的旋转和缩放原点创建。

    【讨论】:

      猜你喜欢
      • 2020-04-14
      • 1970-01-01
      • 1970-01-01
      • 2013-09-20
      • 1970-01-01
      • 2020-09-14
      • 2018-10-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多