代码之家  ›  专栏  ›  技术社区  ›  Ramaraj T

在圆内相对于圆心绘制一个矩形

  •  0
  • Ramaraj T  · 技术社区  · 7 年前

    我正在尝试放置一个 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);
    

    enter image description here

    2 回复  |  直到 7 年前
        1
  •  1
  •   meowgoesthedog    7 年前

    TranslateCTM 左上角 y-offset :

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

        2
  •  0
  •   Blindman67    7 年前

    您应该创建矩形,使其相对于旋转原点。

    例如矩形的角落

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