views:

79

answers:

2

I have a class Grid which produces a graph paper like grid on in the drawing area. I then have 5 other classes for different shapes to draw with; Line, Polygon, Ellipse, Curve, Arc

Now, these 5 classes use an instance of Grid because Grid has a resolution and a scale. Inside Grid I have:

public function set resolution(x:Number):void {
    _gap = (modBy10(x) / 10);
    _scale = (modBy10(x) / (this.resolution * _scale));

    draw();
}

public function get resolution():Number {
    return (_gap * 10);
}

public function set scale(x:Number):void {
    _scale = (this.resolution / x);
}

public function get scale():Number {
    return _scale;
}

/**/

public function scaleLength(x:Number):Number {
    return (x * this.scale);
}

public function scaleLengthDown(x:Number):Number {
    return (x / this.scale);
}

public function scaleArea(x:Number):Number {
    return (x / Math.pow(this.scale, 2));
}

I'm just lost for a solution on how to update every instance of my 5 drawing classes when Grid is changed.

For instance, Polygon is made up of multiple instances of Line, Line(length, angle) where "length" is in either in, ft, cm, or m. If the user wishes to change the scale from say 10ft per 100px resolution to 20ft per 80px.. Is there an easier way than re-drawing every Line inside Polygon?

+1  A: 

a way to handle this is the composite pattern and dependency inversion.

alternatively, you could stick the all drawn objects into one container and just scale the container accordingly.

greetz
back2dos

back2dos
+1  A: 

I wouldn't have all the other objects depend on the Grid for scale.

Rather I would have a container called something like ScaledDrawingSurface that contains all of the shape objects and the Grid object.

The container would then have a scale propety that could be changed.

You then have two options for dealing with a change in the scale property:

  1. Have an event that is dispatched by the ScaledDrawingSurface when the scale changes. All of the shapes and the grid would listen for this event and update themselves accordingly. If you do this, you will want to make a custom ScaleChangedEvent that includes a property for the new scale.

  2. Have the ScaledDrawingSurface iterate through all of its children setting their scales appropriately. If you do this, you will want to have an IScalable interface with a SetScale method that the shapes and the grid implement.

In either case, you can make composite objects (e.g. polygon) responsible for updating their child objects, using either event dispatching or iterative processing.

One nice bonus of this approach is it would be very easy to have multiple overlayed grids (like graph paper) that update to the scale of the surface - you would just need a property on the Grid class to set the spacing of each grid relative to the global scale (and probably colour and thickness properties)

DanK