Search code examples
smalltalksqueakmorphic

Squeak Circle Section


I'm trying to draw a quarter of a circle with Morphic in Squeak-Smalltalk. How does it work?

Thanks in advance!


Solution

  • The best teacher is the the image itself. If you open a browser on CircleMorph, you'll see that its superclass, EllipseMorph, defines #drawOn:, which is how morphs draw themselves. From there, you can get all the information and inspiration to make your custom morph.

    Update: My first thought was to draw it by hand (totally override #drawOn:), but there weren't any obvious candidates in Canvas. By letting the circle draw itself while setting the clipping rectangle to a quarter of it, it became almost a one-liner.

    Update 2: The trick with a quarter-circle is getting CircleMorph to do most of the work for you! The best I came up with is:

    QuarterCircleMorph>>drawOn: aCanvas
    
        | realBounds |
        "Save the actual bounds of the morph"
        realBounds := bounds.
    
        "Pretend the bounds are 4x as big"
        bounds := bounds bottom: bounds bottom + bounds height.
        bounds := bounds right: bounds right + bounds width.
    
        "Let CircleMorph handle the drawing"
        super drawOn: aCanvas.
    
        "Restore the actual bounds"
        bounds := realBounds.
    

    Where QuarterCircleMorph is a subclass of CircleMorph. Because you can't draw past it's real bounds, everything works out. n.b. in actual code, the comments would be overkill (except maybe the 4x one, but then, that's a sign to maybe refactor :))