【问题标题】:What is causing this edge condition with CGContextAddArc?是什么导致 CGContextAddArc 出现这种边缘条件?
【发布时间】:2015-02-07 04:13:14
【问题描述】:

我通过使用 iOS Core Graphics 在圆的周边绘制圆点弧来创建视图。点中心被计算,存储在一个数组中,并在渲染到屏幕之前使用 CGContextAddArc 检索。我对用于绘制周边点的方法充满信心,这些点可能是 [A] 勾勒出轮廓,[B] 填充一种颜色,[C] 交替填充和勾勒轮廓,以及 [D] 填充一系列五种颜色,如图所示。

但是,如果将中心点添加到数组中,则在周长上绘制的最后一个点的属性会发生变化。

演示这种情况的最简单方法是绘制外围点,并加上 [E] 小中心点 [F] 大中心点和 [G] 两个中心点;在每种情况下,中心的小点和大点都应该被填充为绿色。使用 [H] 时,两个居中的点都被勾勒出未填充的轮廓时也会出现问题。

我已经学习 Core Graphics 一个月了,需要帮助来确定导致这种情况的边缘条件。我真的很欢迎一位更有经验的核心图形程序员的见解。谢谢。

这里是实现代码;首先,初始化视图设置的方法。

- (void)ViewSettings_WaitView {
    sectors         = 80;   // number of dots on perimeter
    limit           = sectors;

    uberRadius      = 52;   // radius of large circle perimeter
    dotRadius       = 4;    // radius of dots on large circle perimeter
    dotsFilled      = FALSE;    // fill every with colour or outline
    oneColour       = FALSE;    // every colour or one colour
    alternateDots   = FALSE;    // alternately filled and outlined

    ringDot         = 64;   // 64:show 0:hide
    ringDotFilled   = TRUE; // fill or outlined    
    centreDot       = 26;       // 26:show 0:hide
    centreDotFilled = FALSE;    // fill or outlined

    [self centreReference];     // set up arc drawing to start from 12 o'clock position
    [self selectZone];          // emulate 1-of-5 colours selected in GlobalView
} 

这是绘制这些图像的代码

- (void)drawCircle                  {
    context          = UIGraphicsGetCurrentContext();          // Get the Graphics Context
    CGContextSetLineWidth(context, 0.5);                       // Set the circle outerline-width

    dotPosition                    = CGPointMake(uberX,uberY); // centre point for ring dot and centre dot

    // create ring dot (larger centre dot)

    if (ringDot     != 0) {
        iOSCircle *newCircle       = [[iOSCircle alloc] init]; // Create a new iOSCircle Object
        newCircle.circleRadius     = ringDot;                  // ringDot radius
        newCircle.circleCentre     = dotPosition;              // place ringDot on the frame
        [totalCircles addObject:newCircle];                    // add to the circle Array
        [self setNeedsDisplay];                                // update the view

        NSLog(@"ringDot added:%@ radius: %f", NSStringFromCGPoint(dotPosition), ringDot);
    }

    // create centre dot (smaller centre dot)

    if (centreDot    != 0) {
        iOSCircle *newCircle       = [[iOSCircle alloc] init]; // Create a new iOSCircle Object
        newCircle.circleRadius     = centreDot;                // ringDot radius
        newCircle.circleCentre     = dotPosition;              // place ringDot on the frame
        [totalCircles addObject:newCircle];                    // add to the circle Array
        [self setNeedsDisplay];                                // update the view

        NSLog(@"centreDot added:%@ radius: %f", NSStringFromCGPoint(dotPosition), centreDot);
    }

    // create sector dots (on perimeter of the arc)

    for (dotCount   = 1; dotCount < limit+1; dotCount++)
    {
        iOSCircle *newCircle    = [[iOSCircle alloc] init];    // Create a new iOSCircle Object
        newCircle.circleRadius  = dotRadius;

            [self newCentre];                                  // create a new x and y point for each sector dot

        dotPosition             = CGPointMake(x,y);            // create each sector dot
        newCircle.circleCentre  = dotPosition;                 // place each dot on the frame
            [totalCircles addObject:newCircle];                // add to the circle Array
            [self setNeedsDisplay];                            // update the view

        NSLog(@"Dot %i %@", dotCount, NSStringFromCGPoint(dotPosition));
    }

    dotCount = 1;
    for (iOSCircle *circle in totalCircles) {                  // Loop through array and retrieve dot dimensions
        CGContextAddArc(context, circle.circleCentre.x, circle.circleCentre.y, circle.circleRadius, 0.0, M_PI * 2.0, YES);

            [self renderSectorDots];                           // render dots to view

    dotCount++;

    }    
    if (ringDot    != 0) {
        NSLog(@"add ringDot %@ radius: %f", NSStringFromCGPoint(dotPosition), ringDot);

        [self renderRingDot];
    }
    if (centreDot    != 0) {
        NSLog(@"add centreDot %@ radius: %f", NSStringFromCGPoint(dotPosition), centreDot);

        [self renderCentreDot];
    }
} 

和渲染中心点的方法(圆点类似)

- (void)renderCentreDot {
    switch (centreDotFilled) {
        case 1:
            colourIndex = selectedZone;
            [self whatColour];
            break;
        default:
            [self dotOutline];
            break;
    }
} 

- (void)whatColour {
    switch (colourIndex) {
        case 1:
            // Fill the circle with cyan
            [self paintCyan];
            break;
        case 2:
            // Fill the circle with red
            [self paintRed];
            break;
        case 3:
            // Fill the circle with yellow
            [self paintYellow];
            break;
        case 4:
            // Fill the circle with magenta
            [self paintMagenta];
            break;
        case 5:
            // Fill the circle with green
            [self paintGreen];
            break;
        default:
            break;
    }
}

- (void)dotOutline {
    CGContextStrokePath(context);                     // draw outerline only
} 

- (void)paintGreen {
    CGContextSetFillColorWithColor(context, [[UIColor greenColor] CGColor]);
    CGContextDrawPath(context, kCGPathFillStroke);    // fill with outerline-colour
    }

最后是计算扇形点新中心的方法

- (void)newCentre  {
    dotAngle    = dotAngle + uberAngle;
    x           = uberX + (uberRadius * 2 * cos(dotAngle));
    y           = uberY + (uberRadius * 2 * sin(dotAngle));    
//    NSLog(@"%i %f %f %f", dotCount, dotAngle, endAngle, uberAngle);
}

以及初始化中心参数并从12点位置开始绘制的方法

- (void)centreReference { 

    uberX     = 160; 
    uberY     = 240; 
    uberAngle = (2.0 * PI) / sectors; 

    dotAngle  = PI * -0.5;             // start drawing 0.5 PI radians before 3 o'clock
    endAngle  = PI * 1.5;              // stop drawing 1.5 PI radians after 3 o'clock 

    NSLog(@"%f %f %f %f", uberX, ringY, uberRadius, uberAngle); 

}

【问题讨论】:

    标签: ios objective-c core-graphics


    【解决方案1】:

    您有 80 个外圈和 0、1 或 2 个内圈。所有这些圆圈都存储在一个名为totalCircles 的数组中,内圈存储在数​​组的前面。所以如果你有两个内圈,totalCircles 有 82 个元素。

    问题是你只画了前 80 个圆:两个内圆和 78 个外圆。

    然后您尝试填充其中一个内圈,而是填写第 79 个外圈。

    换句话说,你不知道哪个圆圈是哪个圆圈。


    尝试按以下方式构建您的代码:

    您已经有一个“iOSCircle”对象,这很好。这个圆形对象也应该包含它的颜色并且能够自己绘制:

    @interface Circle : NSObject
    @property (nonatomic) CGPoint centre;
    @property (nonatomic) CGFloat radius;
    @property (nonatomic, strong) UIColor* color;
    - (void)draw;
    @end
    

    -draw 方法绘制圆,如果设置了color,则可选择填充:

    - (void)draw
    {
        CGContextRef ctx = UIGraphicsGetCurrentContext();
        CGContextAddArc(ctx, self.centre.x, self.centre.y, etc...)
        if (self.color)
        {
            [self.color setFill];
            CGContextDrawPath(ctx, kCGPathFillStroke);
        }
        else
        {
            CGContextStrokePath(ctx);
        }
    }
    

    你应该有一个方法来生成一个圆形对象数组。这将创建 80 个外圈,以及可选的内圈。这也会为每个圆圈分配一种颜色。

    那么你的主要绘制方法就这么简单:

    - (void)drawCircle
    {
        NSArray* circles = [self generateCircles];
        for (Circle* circle in circles)
        {
            [circle draw];
        }
    }
    

    【讨论】:

    • 哇。这是有道理的。我将尝试扩展 iOSCircle 对象。谢谢。最终,我想将周边的 80 个圆圈排列成 5 个集群,每组 16 个较小的圆圈围绕一个可选的中心圆圈,每个集群都有不同的颜色 - 就像 5 个不同的行星,每个行星都有 16 个卫星。我应该为每个集群使用单独的数组吗?您的 -draw 方法如何允许这样做?
    • 还是一个包含 5 个行星的阵列和 16 个卫星的 5 个独立阵列?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-11
    • 2019-07-18
    • 2011-02-26
    • 1970-01-01
    • 2020-04-15
    • 2022-11-02
    相关资源
    最近更新 更多