gpt4 book ai didi

ios - 手动绘制贝塞尔曲线

转载 作者:行者123 更新时间:2023-11-29 03:24:18 25 4
gpt4 key购买 nike

我正在尝试以这种方式手动创建贝塞尔曲线:

- (void)drawBezierFrom:(CGPoint)from to:(CGPoint)to controlA:(CGPoint)a controlB:(CGPoint)b sections:(NSUInteger)cnt color:(NSUInteger)color
{
float qx, qy;
float q1, q2, q3, q4;
int lastx = - 1, lasty;
int plotx, ploty;
float t = 0.0;

while (t <= 1)
{
q1 = t*t*t*-1 + t*t*3 + t*-3 + 1;
q2 = t*t*t*3 + t*t*-6 + t*3;
q3 = t*t*t*-3 + t*t*3;
q4 = t*t*t;

qx = q1*from.x + q2*a.x + q3*to.x + q4*b.x;
qy = q1*from.y + q2*a.y + q3*to.y + q4*b.y;

plotx = round(qx);
ploty = round(qy);

/*if (lastx != -1)
[self drawLineFrom:NSMakePoint(lastx, lasty) to:NSMakePoint(plotx, ploty) color:color];
else
[self drawLineFrom:NSMakePoint(from.x, from.y) to:NSMakePoint(plotx, ploty) color:color];*/

lastx = plotx;
lasty = ploty;
t = t + (1.0/(cnt + 0.0f));
}
//[self drawLineFrom:NSMakePoint(lastx, lasty) to:NSMakePoint(to.x, to.y) color:color];
[self drawLineFromCoordX:lastx andY:lasty toPointWithCoordsX:to.x andY:to.y];
}

-(void)drawLineFromCoordX:(int)x andY:(int)y toPointWithCoordsX: (int)fx andY: (int)fy{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextMoveToPoint(context, x,y);
CGContextAddLineToPoint(context, fx,fy);
CGContextStrokePath(context);
}

调用viewDidLoad中的方法:

CGPoint fromPoint = CGPointMake(10, 10);
CGPoint toPoint = CGPointMake(100, 10);
CGPoint controlA = CGPointMake(50, 50);
CGPoint controlB = CGPointMake(60, 60);

[self drawBezierFrom:fromPoint to:toPoint controlA:controlA controlB:controlB sections:10 color:4];

编辑:

我做了你所做的事情:

-(void)drawRect:(CGRect)rect{
CGPoint fromPoint = CGPointMake(10, 10);
CGPoint toPoint = CGPointMake(100, 10);
CGPoint controlA = CGPointMake(50, 50);
CGPoint controlB = CGPointMake(60, 60);

[self drawBezierFrom:fromPoint to:toPoint controlA:controlA controlB:controlB sections:10 color:4];
}

保持其他一切不变,但它仍然不起作用

最佳答案

将你在 viewDidLoad 中的代码([super viewDidLoad] 除外)放入 drawRect 方法中。需要在 drawRect 方法中调用 UIGraphicsGetCurrentContext() 才能知道当前上下文是哪个。

编辑:

还放:

[self drawLineFromCoordX:lastx andY:lasty toPointWithCoordsX:to.x andY:to.y];

在 while 循环中。在 while 循环之前设置描边颜色:

[[UIColor redColor] setStroke];

关于ios - 手动绘制贝塞尔曲线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20658983/

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