0

I'm trying to draw a power function curve in a 300x300 pixel rectangle using NSBezierPath as follows:

-(void)drawPowerCurve:(float)power points:(int)numbPoints{
    NSBezierPath * path = [NSBezierPath bezierPath];
    [path setLineWidth: 1.0];
    [[NSColor colorWithCalibratedRed:0.0 green:0.0 blue:1.0 alpha:1.0] set];

    NSPoint borderOrigin = {35.5,15.5};
    NSPoint endPoint;
    [path moveToPoint:borderOrigin];
    for(int i = 0; i < numbPoints; i++){
        endPoint.x = borderOrigin.x + (300.0/numbPoints)*(i+1);
        endPoint.y = borderOrigin.y + 300.0*pow(((i+1)/(float)numbPoints), power);
        [path lineToPoint:endPoint];
        [path stroke];
        [path moveToPoint:endPoint];
    }
}

However, the curve thins out at the top end compared to the bottom end. For example power = 1.8 and numbPoints = 50. Also the curve doesn't look as smooth as for example curves shown in Apple's ColorSync Utility. Of course I don't know how they are drawing the curves in ColorSync. Any ideas on how to improve the look of these curves (particularly getting rid of the thining out).

Edit -- Here is a screenshot: enter image description here

Jim Merkel
  • 352
  • 2
  • 9
  • Please post a screen shot. Also, what is the height of the view (or graphics context) into which you're drawing? – rob mayoff Apr 04 '19 at 02:03
  • Having a hard time pasting the screenshot on here -- it's a png file. However, I notice if I use many more points like 500 to 1000 rather than 50, the thining out is much less. Don't understand why that would be. Also, regarding graphics context, I'm not saving /restoring graphics context. – Jim Merkel Apr 04 '19 at 02:50
  • As a guess (sorry can't test at the moment) try moving the `stroke` out of the loop so you only draw the curve once instead of `numbPoints` times as it grows. You can also remove the redundant `moveToPoint` in the loop, `lineToPoint` leaves the current point at the end of the added segment. – CRD Apr 04 '19 at 04:57
  • @CRD That fixed it. The curve now looks identical to the curve in ColorSync. You could provide that as an answer, and I will accept it. – Jim Merkel Apr 04 '19 at 05:14

1 Answers1

3

Move the stroke out of the loop so you only draw the curve once instead of numbPoints times as it grows. Also remove the redundant moveToPoint in the loop, lineToPoint leaves the current point at the end of the added segment.

CRD
  • 52,522
  • 5
  • 70
  • 86