繁体   English   中英

贝塞尔曲线的参数表达式

[英]Parametric Expression of a Bezier Curve

我已经使用贝塞尔曲线的参数表达式来沿着曲线定位点,并且该点应能正常工作。 问题是我将t值设置为y轴的百分比,不幸的是(很明显),它不相关,因为我的曲线比我的Y轴长。 因此,在此程序中,如果我将Y值设置为75,我想返回直线上Y值为25的点(相反,因为在iOS中,(0,0)位于左上角而不是底部留在我的图表中)。 当前设置我的Y值会将曲线上的点重新调整为75%,Y值为15.62。

任何人都建议如何在曲线上获得Y而不是75%的点?

这是上一个问题的后续问题, 在路径上找到了一个要点 ,但是我觉得它与众不同,足以保证它自己的线程。

#import "GraphView.h"

@interface GraphView ()
{
    float yVal;
}

@end

@implementation GraphView

@synthesize myLabel, yValue;

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self) {
        yVal = 50;
    }
    return self;
}

- (IBAction)yValueTextField:(id)sender
{
    yVal = yValue.text.intValue;
    [self resignFirstResponder];
    [self setNeedsDisplay];
}

- (void)drawRect:(CGRect)rect
{
    float t = yVal / 100;

    // Starting point
    float p1x = 0;
    float p1y = 100;

    // Control point 1
    float c1x = 50;
    float c1y = 100;

    // Control point 2
    float c2x = 50;
    float c2y = 0;

    // End Point
    float p2x = 100;
    float p2y = 0;

    CGPoint p1 = CGPointMake(p1x, p1y);
    CGPoint c1 = CGPointMake(c1x, c1y);
    CGPoint c2 = CGPointMake(c2x, c2y);
    CGPoint p2 = CGPointMake(p2x, p2y);

    // Cubic Bezier Curver Parmetic Expression
    float X = pow((1 - t), 3) * p1x + 3 * pow((1 - t), 2) * t * c1x + 3 * (1 - t) * pow(t, 2) * c2x + pow(t, 3) * p2x;
    float Y = pow((1 - t), 3) * p1y + 3 * pow((1 - t), 2) * t * c1y + 3 * (1 - t) * pow(t, 2) * c2y + pow(t, 3) * p2y;

    myLabel.text = [NSString stringWithFormat:@"Coord = %.2f, %.2f", X, Y];

    UIBezierPath *circle = [UIBezierPath bezierPathWithOvalInRect:CGRectMake((X - 2), (Y - 2), 4, 4)];
    [[UIColor blackColor] setFill];
    [circle fill];

    UIBezierPath *curve = [[UIBezierPath alloc] init];
    [curve moveToPoint:p1];
    [curve addCurveToPoint:p2 controlPoint1:c1 controlPoint2:c2];
    [curve setLineWidth:1];
    [[UIColor blueColor] setStroke];
    [curve stroke];
}

@end

这是在贝塞尔曲线上找到我的点的解决方案。 有关此背景的更多信息,请参阅我的另一篇相关文章-> 在路径上找到一个点

#import "Calculation.h"

@implementation Calculation

@synthesize a, b, c, d, xy;

- (float) calc
{

    float squareRootCalc =
    sqrt(
    6*pow(xy,2)*b*d
    +4*a*pow(c,3)
    -3*pow(b,2)*pow(c,2)
    +9*pow(xy,2)*pow(c,2)
    -6*a*c*b*d
    +6*a*xy*c*b
    -18*pow(xy,2)*b*c
    +6*a*pow(xy,2)*c
    -12*a*xy*pow(c,2)
    -2*pow(a,2)*xy*d
    +pow(a,2)*pow(d,2)
    +4*pow(b,3)*d
    +pow(xy,2)*pow(d,2)
    -4*pow(b,3)*xy
    -4*pow(c,3)*xy
    +pow(a,2)*pow(xy,2)
    +6*c*b*d*xy
    +6*a*c*d*xy
    +6*a*b*d*xy
    -12*pow(b,2)*d*xy
    +6*xy*c*pow(b,2)
    +6*xy*b*pow(c,2)
    -2*a*pow(xy,2)*d
    -2*a*xy*pow(d,2)
    -6*c*d*pow(xy,2)
    +9*pow(xy,2)*pow(b,2)
    -6*a*pow(xy,2)*b)
    ;

    float aCalc = 24*c*d*xy + 24*a*pow(c,2) - 36*xy*pow(c,2) + 4 * squareRootCalc * a;

    float bCalc = -12 * squareRootCalc * b;

    float cCalc = 12 * squareRootCalc * c;

    float dCalc = -4 * squareRootCalc * d;


    float xyCalc =
    24*xy*a*b
    -24*xy*b*d
    -12*b*a*d
    -12*c*a*d
    -12*c*b*d
    +8*xy*a*d
    +8*pow(b,3)
    +8*pow(c,3)
    +4*pow(a,2)*d
    +24*pow(b,2)*d
    -4*xy*pow(a,2)
    -4*xy*pow(d,2)
    +4*a*pow(d,2)
    -12*c*pow(b,2)
    -12*b*pow(c,2)
    -12*a*b*c
    -24*xy*a*c
    +72*xy*c*b
    -36*xy*pow(b,2)
    ;

    float cubeRootCalc = cbrt(aCalc + bCalc + cCalc + dCalc + xyCalc);

    float denomCalc = (a-3*b+3*c-d);

    float secOneCalc = 0.5 * cubeRootCalc / denomCalc;

    float secTwoCalc = -2 * ((a*c - a*d - pow(b,2) + c*b + b*d - pow(c,2)) / (denomCalc * cubeRootCalc));

    float secThreeCalc = (a - 2*b + c) / denomCalc;

    return secOneCalc + secTwoCalc + secThreeCalc;


}

- (Calculation *) initWithA:(float)p0 andB:(float)p1 andC:(float)p2 andD:(float)p3 andXy:(float)xyValue
{
    self = [super init];

    if (self) {
        [self setA:p0];
        [self setB:p1];
        [self setC:p2];
        [self setD:p3];
        [self setXy:xyValue];
    }
    return self;
}

- (void) setA:(float)p0 andB:(float)p1 andC:(float)p2 andD:(float)p3 andXy:(float)xyValue
{
    [self setA:p0];
    [self setB:p1];
    [self setC:p2];
    [self setD:p3];
    [self setXy:xyValue];
}

@end

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM