BezierPath issues

  • I'm trying to adapt a part of the sketch example in to a program for
    light drawing functions. Unfortunately not being very good with
    graphics I'm having a great deal of trouble. I was hoping that it was
    possible to create the free form drawing within a bezierpath but if it
    is I don't see how. Below is the bezier path creation code I am using
    to try and create the freeform drawing.. This code produces a single
    angled line (Always the exact same angle) about 5px long and nothing
    more no matter how much I move the mouse, then when I release the
    mouse button the application crashes with a bad access and I'll mark
    the line where it crashes.

    - (NSBezierPath *)bezierPath {

    NSRect bounds = [self bounds];

    if(![self strokeLineWidth]){
      NSLog(@"Zero stroke w3idth");
      return penPath;
      }

    [penPath setLineWidth:[self strokeLineWidth]];
    if(!lastPointSet){
      lastPoint = [self bounds].origin;
      lastPointSet= YES;
      }
    NSRect dot = NSMakeRect(bounds.size.width,bounds.size.height,[self
    strokeLineWidth],[self strokeLineWidth]);
    NSPoint dotPoint = NSMakePoint(dot.origin.x,dot.origin.y);

    if(!penPath){
      NSLog(@"returning NULL");
      return NULL;
      }
    [penPath moveToPoint:dotPoint]; //This is where it crashes on mouse up.
    [penPath lineToPoint:lastPoint];
    lastPoint = dotPoint;
    [penPath appendBezierPathWithRect:dot];
        return penPath;
    }

    I'm sure this is a simple problem but I cannot seem to figure it out.
  • On Feb 24, 2008, at 3:53 PM, Development wrote:

    > I'm trying to adapt a part of the sketch example in to a program for
    > light drawing functions. Unfortunately not being very good with
    > graphics I'm having a great deal of trouble. I was hoping that it
    > was possible to create the free form drawing within a bezierpath but
    > if it is I don't see how. Below is the bezier path creation code I
    > am using to try and create the freeform drawing.. This code produces
    > a single angled line (Always the exact same angle) about 5px long
    > and nothing more no matter how much I move the mouse, then when I
    > release the mouse button the application crashes with a bad access
    > and I'll mark the line where it crashes.
    >
    > - (NSBezierPath *)bezierPath {
    >
    >
    > NSRect bounds = [self bounds];
    >
    > if(![self strokeLineWidth]){
    > NSLog(@"Zero stroke w3idth");
    > return penPath;
    > }
    >
    > [penPath setLineWidth:[self strokeLineWidth]];
    > if(!lastPointSet){
    > lastPoint = [self bounds].origin;
    > lastPointSet= YES;
    > }
    > NSRect dot = NSMakeRect(bounds.size.width,bounds.size.height,[self
    > strokeLineWidth],[self strokeLineWidth]);
    > NSPoint dotPoint = NSMakePoint(dot.origin.x,dot.origin.y);

    Not sure why you're constructing a rect here.  It will always be for a
    region outside your bounds.
    Your dotPoint variable is just being set to the same point over and
    over again.

    > if(!penPath){
    > NSLog(@"returning NULL");
    > return NULL;
    > }
    > [penPath moveToPoint:dotPoint]; //This is where it crashes on mouse
    > up.

    You're going to have to post more code.  For example, how are you
    dealing with penPath?  Are you properly retaining it?

    > [penPath lineToPoint:lastPoint];

    Shouldn't this be [penPath lineToPoint:dotPoint]; ?

    >
    > lastPoint = dotPoint;
    > [penPath appendBezierPathWithRect:dot];

    Why are you appending the rect here?  Note that it's always a point
    physically outside your actual bounds.

    > return penPath;
    > }
    >
    > I'm sure this is a simple problem but I cannot seem to figure it out.

    Aside from the crash, you're going to have to change your code to work
    with capturing points from mouse movement.  And you'll then need to
    also deal with very fast movement that will tend to give you straight
    lines between points (maybe that's what you want?)

    ___________________________________________________________
    Ricky A. Sharp        mailto:<rsharp...>
    Instant Interactive(tm)  http://www.instantinteractive.com
  • Check out the lasso style crop maker here:

    http://developer.apple.com/samplecode/Cropped_Image/index.html

    -jcr
  • On 25/02/2008, at 8:53 AM, Development wrote:

    > I'm sure this is a simple problem

    Actually, it's not.

    Creating interactive drawing successfully has many pitfalls.

    One avenue you might consider is using an already written library.
    I'm working on one written in Cocoa called DrawKit: http://
    apptree.net/drawkitmain.html  - it's currently only at beta 1 but
    works. Beta 2 due soon.

    --------
    S.O.S.
previous month february 2008 next month
MTWTFSS
        1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29    
Go to today
MindNode
MindNode offered a free license !