核心图形 - 以一定角度绘制线条

use*_*069 11 iphone core-graphics objective-c

我一直在寻找挣扎这一段时间,需要一些帮助.我只想从我的显示器中心(160,200)以14.4度的间隔绘制25条线.我一直在for循环中使用这行代码,其中x是14.4度乘数 -

    UIImage*backgroundImage = [UIImage imageNamed:@"Primate Background Only.png"];
[backgroundImage drawInRect:CGRectMake(0, 0, 320, 480)];
Run Code Online (Sandbox Code Playgroud)

//画出外圈

rect = CGRectMake(0.0, 35.0, 320.0, 320.0);     
CGContextRef contextRef = UIGraphicsGetCurrentContext();                // Get the contextRef
CGContextSetLineWidth       (contextRef, 0.5);                  // Set the border width
CGContextSetRGBFillColor    (contextRef, (219.0f/255.0f), (219.0f/255.0f), (219.0f/255.0f), 0.05f);             // Set the circle fill color to GREEN
CGContextSetRGBStrokeColor  (contextRef, 0.0, 0.0, 0.0, 0.2);           // Set the circle border color to BLACK     
CGContextFillEllipseInRect      (contextRef, rect);                 // Fill the circle with the fill color
CGContextStrokeEllipseInRect    (contextRef, rect);                 // Draw the circle border
Run Code Online (Sandbox Code Playgroud)

//绘制内圈

rect = CGRectMake(25.0, 60.0, 270.0, 270.0);                        // Get the contextRef
CGContextSetLineWidth       (contextRef, 0.5);                  // Set the border width
CGContextSetRGBFillColor    (contextRef, (219.0f/255.0f), (219.0f/255.0f), (219.0f/255.0f), 0.25f);
CGContextSetRGBStrokeColor  (contextRef, 0.0, 0.0, 0.0, 0.2);           // Set the circle border color to BLACK     
CGContextFillEllipseInRect      (contextRef, rect);                 // Fill the circle with the fill color
CGContextStrokeEllipseInRect    (contextRef, rect);     
Run Code Online (Sandbox Code Playgroud)

//绘制细分

CGContextRef context = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(context, 0.0, 0.0);           
    for (x=1; x<26; x++) {  

        CGContextSetLineWidth       (context, 0.5);
        CGContextSetRGBStrokeColor  (context, 0.0, 0.0, 0.0, 0.25);
        CGContextMoveToPoint        (context, 160, 200);                
        CGContextAddLineToPoint     (context, (160.0 * (cos((x*14.4)*(M_PI/180)))), (160.0 * (sin((x*14.4)*(M_PI/180)))));                      //  The angle is in degrees
        CGContextAddLineToPoint     (context, 200, 65);
        CGContextAddLineToPoint     (context, 160, 200);            
        CGContextStrokePath(context);                           // Why is this not showing both line color and infill?
        CGContextSetFillColorWithColor   (context, [UIColor whiteColor].CGColor);       
        CGContextFillPath           (context);
        CGContextRef context = UIGraphicsGetCurrentContext();
    }           
Run Code Online (Sandbox Code Playgroud)

(我打算在这里发布一张图片,但不允许我这样做!)

有人可以纠正我的触发功能.这意味着从12:00点到24点顺时针画25条线.相反,它向后抽取只有90度然后返回,所有线条的长度也都很长.

非常感谢

以上是用于根据要求绘制两个外圆和内部区段的代码的更大样本.我会尝试下一步上传图片.

在此输入图像描述

idz*_*idz 17

你的主要问题是这行代码:

CGContextAddLineToPoint(context, (160.0 * (cos((x*14.4)*(M_PI/180)))), 
                                (160.0 * (sin((x*14.4)*(M_PI/180)))));
Run Code Online (Sandbox Code Playgroud)

它应该是:

CGContextAddLineToPoint(context, 160 + (160.0 * (cos((x*14.4)*(M_PI/180)))), 
                                200 + (160.0 * (sin((x*14.4)*(M_PI/180)))));
Run Code Online (Sandbox Code Playgroud)

正如你最初写的那样,线条是相对于0,0绘制的,但是你想要相对于你的中心点(160,200)绘制.

接下来的两行也有点怀疑:

        CGContextAddLineToPoint     (context, 200, 65);
        CGContextAddLineToPoint     (context, 160, 200);
Run Code Online (Sandbox Code Playgroud)

我不清楚你在这里尝试做什么,但这最终会从每个手臂末端画一条线到一个固定点(200,65)并从那里回到你的中心点,这几乎可以肯定不是你想要的!尝试评论这些线条,确认正确绘制"手臂"并从那里拿走它......

希望这很清楚

如果您进行这些更正,您的屏幕将如下所示: 截图