gpt4 book ai didi

iphone - 如何将圆分成等份?

转载 作者:行者123 更新时间:2023-12-03 21:12:07 31 4
gpt4 key购买 nike

我是iphone的初学者。我想要将圆分成相等的部分。我有代码,

#import "MyView.h"
#define PI 3.14159265358979323846
static inline float radians(double degrees) { return degrees * PI / 180; }
@implementation MyView
- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
// Initialization code
}
return self;
}
- (void)drawRect:(CGRect)rect {

// circle
CGContextRef contextRef = UIGraphicsGetCurrentContext();
CGContextSetRGBFillColor(contextRef, 0, 0, 255, 0.1);
CGContextSetRGBStrokeColor(contextRef, 0, 0, 255, 0.5);
// Draw a circle (filled)
//CGContextFillEllipseInRect(contextRef, CGRectMake(100, 100, 150, 150));
// Draw a circle (border only)
CGContextStrokeEllipseInRect(contextRef, CGRectMake(100, 100, 150, 150));
//cgr


CGContextRef c5= UIGraphicsGetCurrentContext();
//CGFloat red4[4] = {1.0f, 0.0f, 0.0f, 1.0f};
CGContextSetLineWidth(c5, 2.0);
CGContextSetStrokeColorWithColor(c5, [UIColor brownColor].CGColor);
CGContextBeginPath(c5);
//CGContextMoveToPoint(c5, 101.0f, 156.0f);
CGContextMoveToPoint(c5, 175.0f, 175.0f);
CGContextAddLineToPoint(c5, 100.0f, 175.0f);
CGContextStrokePath(c5);

CGContextRef c6= UIGraphicsGetCurrentContext();
//CGFloat red4[4] = {1.0f, 0.0f, 0.0f, 1.0f};
CGContextSetLineWidth(c6, 2.0);
CGContextSetStrokeColorWithColor(c6, [UIColor blueColor].CGColor);
CGContextBeginPath(c6);
CGContextMoveToPoint(c6, 175.0f, 175.0f);
CGContextAddLineToPoint(c6, 175.0f, 250.0f);
//CGContextAddLineToPoint(c6, 175.0f, 245.0f);
CGContextStrokePath(c6);

CGContextRef c7= UIGraphicsGetCurrentContext();
//CGFloat red4[4] = {1.0f, 0.0f, 0.0f, 1.0f};
CGContextSetLineWidth(c7, 02.0);
CGContextSetStrokeColorWithColor(c7, [UIColor greenColor].CGColor);
CGContextBeginPath(c7);
CGContextMoveToPoint(c7, 175.0f, 175.0f);
CGContextAddLineToPoint(c7, 175.0f, 100.0f);
CGContextStrokePath(c7);

CGContextRef c8= UIGraphicsGetCurrentContext();
//CGFloat red4[4] = {1.0f, 0.0f, 0.0f, 1.0f};
CGContextSetLineWidth(c8, 2.0);
CGContextSetStrokeColorWithColor(c8, [UIColor redColor].CGColor);
CGContextBeginPath(c8);
//CGContextMoveToPoint(c5, 101.0f, 156.0f);
CGContextMoveToPoint(c8, 175.0f, 175.0f);
CGContextAddLineToPoint(c8, 250.0f, 175.0f);
CGContextStrokePath(c8);

CGContextRef c9= UIGraphicsGetCurrentContext();
//CGFloat red4[4] = {1.0f, 0.0f, 0.0f, 1.0f};
CGContextSetLineWidth(c9, 2.0);
CGContextSetStrokeColorWithColor(c9, [UIColor purpleColor].CGColor);
CGContextBeginPath(c9);
//CGContextMoveToPoint(c5, 101.0f, 156.0f);
CGContextMoveToPoint(c9, 175.0f, 175.0f);
CGContextAddLineToPoint(c9, 230.0f, 125.0f);
CGContextStrokePath(c9);

CGContextRef c10= UIGraphicsGetCurrentContext();
//CGFloat red4[4] = {1.0f, 0.0f, 0.0f, 1.0f};
CGContextSetLineWidth(c10, 2.0);
CGContextSetStrokeColorWithColor(c10, [UIColor orangeColor].CGColor);
CGContextBeginPath(c10);
//CGContextMoveToPoint(c5, 101.0f, 156.0f);
CGContextMoveToPoint(c10, 175.0f, 175.0f);
CGContextAddLineToPoint(c10, 120.0f, 125.0f);
CGContextStrokePath(c10);

CGContextRef c11= UIGraphicsGetCurrentContext();
//CGFloat red4[4] = {1.0f, 0.0f, 0.0f, 1.0f};
CGContextSetLineWidth(c11, 2.0);
CGContextSetStrokeColorWithColor(c11, [UIColor magentaColor].CGColor);
CGContextBeginPath(c11);
//CGContextMoveToPoint(c5, 101.0f, 156.0f);
CGContextMoveToPoint(c11, 175.0f, 175.0f);
CGContextAddLineToPoint(c11, 120.0f, 225.0f);
CGContextStrokePath(c11);

CGContextRef c12= UIGraphicsGetCurrentContext();
//CGFloat red4[4] = {1.0f, 0.0f, 0.0f, 1.0f};
CGContextSetLineWidth(c12, 2.0);
CGContextSetStrokeColorWithColor(c12, [UIColor yellowColor].CGColor);
CGContextBeginPath(c12);
//CGContextMoveToPoint(c5, 101.0f, 156.0f);
CGContextMoveToPoint(c12, 175.0f, 175.0f);
CGContextAddLineToPoint(c12, 231.0f, 226.0f);
CGContextStrokePath(c12);


}

- (void)makeCircleAt:(CGPoint)center withDiameter:(float)diameter withColor:(int)myColor
{
float radius = diameter * 0.5;
CGRect myOval = {center.x - radius, center.y - radius, diameter, diameter};
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetRGBFillColor(context, 1.0, 0.0, 0.0, 1.0);
CGContextAddEllipseInRect(context, myOval);
CGContextFillPath(context);
}

- (void)dealloc {
[super dealloc];
}
@end
.

我希望上面的代码动态地划分圆而不使用 c5,c6......c12 block ,只是我想使用一个循环,在其中输入数字并根据数字划分圆。

最佳答案

您可能想将圆分成一系列圆弧并绘制楔形。

使用 (x - h)^2 + (y - k)^2 = r^2 获取沿圆圆周的弧的起点和终点。然后画三条线:从中心开始和结束点各一条,然后从起点到结束点画一条弧线段。 (使用 CGContextAddArc)。然后填写路径。

一旦您在数组中获得了沿周界的点,您就可以使用相同的循环对任意除法进行循环遍历它们。

编辑:

Here is an old cocoa example that shows the math involved

Here's an iPhone pie chart example.

This thread has more links.

我认为,如果你用谷歌搜索“iPhone 和饼图”,你应该会找到许多解决相同基本问题的示例。

关于iphone - 如何将圆分成等份?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2500492/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com