核心图形 - 以角度绘制直线

13

我已经苦苦搜索了一段时间,需要帮助。我只想从我的显示器中心(160,200)画出25条线,间隔为14.4度。我一直在使用这行代码,在for循环中使用x作为14.4度的乘数 -

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

// 绘制外圆

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

// 画出内圆

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);     

// 绘制线段

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();
    }           

(我本来想在这里发布一张图片,但它不允许我!)

请问有人能够纠正我的三角函数吗?我想要从12点钟开始顺时针绘制25条线,直到24:00。但它只会逆时针绘制90度然后返回,而且所有的线都超长了。

非常感激。

以上是用于绘制两个外圆和内部段的更大代码示例,如所请求的。接下来我将尝试上传图片。

输入图像描述


1
三角函数看起来不错,你能给我们展示更多的代码吗?迭代 x 的循环等。 - Gustav Larsson
Gustav是正确的。此外,如果您可以将图像上传到其他地方,并提供链接,那么拥有足够声望的人就可以编辑您的帖子,以便将其嵌入行内。 - Tommy
我想重新格式化你的代码,但是太多了...你能否重新格式化一下,让它更易读?除非你有意地混淆它 :) - Nick Weaver
你搞定了吗?如果没有,请发布更多信息,以便有人可以帮助你。如果成功了,请接受一个答案。 - idz
1个回答

19

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

CGContextAddLineToPoint(context, (160.0 * (cos((x*14.4)*(M_PI/180)))), 
                                (160.0 * (sin((x*14.4)*(M_PI/180)))));

应该是这样:

CGContextAddLineToPoint(context, 160 + (160.0 * (cos((x*14.4)*(M_PI/180)))), 
                                200 + (160.0 * (sin((x*14.4)*(M_PI/180)))));

最初的代码会相对于0,0绘制线条,但你希望相对于屏幕中心点(160,200)进行绘制。

接下来的两行代码也有些可疑:

        CGContextAddLineToPoint     (context, 200, 65);
        CGContextAddLineToPoint     (context, 160, 200);

我不太清楚你在这里要做什么,但是这会导致从每个臂的末端绘制一条线到固定点(200,65),然后再返回到中心点,这几乎肯定不是你想要的!尝试注释掉这些行,确认“arms”被正确绘制,然后再进行下一步操作...

希望这很清楚

如果你进行这些更正,你的屏幕将看起来像这样:Screenshot


@Frank,欢迎来到stackoverflow!你提出的编辑建议可能只需要作为对这个答案的评论,或者是对你的问题的评论。:) 编辑其他人的问题和答案是可以的,但如果它们是澄清要点、纠正拼写错误等方面,那就更好了。通过编辑进行交流很快就会导致无法阅读的内容。 (曾经尝试过阅读世界上第一个维基吗?难以理解。 :) - sarnold

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接