你会如何更新 100+变量是否在不同的类中发生了变化?
我有一个 Grid 类,它在绘图区域中生成像网格一样的方格纸。然后我还有其他 5 个类用于绘制不同的形状; Line、Polygon、Ellipse、Curve、Arc
现在,这 5 个类使用 Grid 的实例,因为 Grid 有分辨率和比例。在网格内部我有:
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));
}
我只是迷失了如何在网格更改时更新我的 5 个绘图类的每个实例的解决方案。
例如,Polygon 由 Line、Line(length、angle) 的多个实例组成,其中“长度”的单位为 in、ft、cm 或 m。如果用户希望将比例从每 100 像素分辨率 10 英尺更改为每 80 像素 20 英尺。有没有比重新绘制多边形内的每条线更简单的方法?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不会让所有其他对象都依赖于网格来缩放。
相反,我会拥有一个名为 ScaledDrawingSurface 之类的容器,其中包含所有形状对象和网格对象。
然后容器将具有可以改变的尺度特性。
然后,您有两个选项来处理比例属性的更改:
当比例更改时,由 ScaledDrawingSurface 调度一个事件。所有形状和网格都会侦听此事件并相应地更新自身。如果这样做,您将需要创建一个自定义 ScaleChangedEvent,其中包含新比例的属性。
让 ScaledDrawingSurface 迭代其所有子项,并适当设置其比例。如果这样做,您将需要一个 IScalable 接口,其中包含形状和网格实现的 SetScale 方法。
在任何一种情况下,您都可以使用事件分派或迭代处理使复合对象(例如多边形)负责更新其子对象。
这种方法的一个好处是,可以很容易地拥有多个覆盖的网格(如方格纸),这些网格可以更新到表面的比例 - 您只需要 Grid 类上的一个属性来设置每个网格相对于全局尺度(可能还有颜色和厚度属性)
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:
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.
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)
处理此问题的一种方法是 复合模式 和 依赖倒置。
或者,您可以将所有绘制的对象粘贴到一个容器中,然后相应地缩放容器。
问候
后退2dos
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