<cfoutput>
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
<html>
<head>
<meta http-equiv='expires' content='0'>
<title>Shapes</title>
<cfscript>
function getX(this) { return this.x; }
function getY(this) { return this.y; }
function setX(this, x) { this.x = x; }
function setY(this, y) { this.y = y; }
function moveTo(this, x, y) { setX(this, x); setY(this, y); }
function rMoveTo(this, dx, dy) { moveTo(this, this.x + dx, this.y + dy); }
function makeShape(x, y) {
// declare and initialize x & y coordinates
this = StructNew();
this.x = x;
this.y = y;
// accessors for x & y coordinates
this.getX = getX;
this.getY = getY;
this.setX = setX;
this.setY = setY;
// modify the shape coordinates
this.moveTo = moveTo;
this.rMoveTo = rMoveTo;
return this;
}
function getRadius(this) { return this.radius; }
function setRadius(this, radius) { this.radius = radius; }
function drawCircle(this) {
WriteOutput("Drawing a Circle at:(" & this.getX(this) & "," & this.getY(this) &
"), radius " & this.getRadius(this) & "<br>");
}
function makeCircle(x, y, radius) {
// inherit attributes and methods from shape class
this = StructCopy(makeShape(x, y));
// declare and initialize radius
this.radius = radius;
// accessors for radius
this.getRadius = getRadius;
this.setRadius = setRadius;
// draw the circle (set to defined function)
this.draw = drawCircle;
return this;
}
function getWidth(this) { return this.width; }
function getHeight(this) { return this.height; }
function setWidth(this, width) { this.width = width; }
function setHeight(this, height) { this.height = height; }
function drawRectangle(this) {
WriteOutput("Drawing a Rectangle at:(" & this.getX(this) & "," & this.getY(this) &
"), width " & this.getWidth(this) & ", height " & this.getHeight(this) & "<br>");
}
function makeRectangle(x, y, width, height) {
// inherit attributes and methods from shape class
this = StructCopy(makeShape(x, y));
// declare and initialize width & height
this.width = width;
this.height = height;
// accessors for width & height
this.getWidth = getWidth;
this.getHeight = getHeight;
this.setWidth = setWidth;
this.setHeight = setHeight;
// draw the rectangle (set to defined function)
this.draw = drawRectangle;
return this;
}
</cfscript>
</head>
<body>
<cfscript>
// set up array with some shape instances
scribble = ArrayNew(1);
ArrayAppend(scribble, makeRectangle(10, 20, 5, 6));
ArrayAppend(scribble, makeCircle(15, 25, 8));
// iterate through the shapes
for (i = 1; i LTE ArrayLen(scribble); i = i + 1) {
each = scribble[i];
each.draw(each);
each.rMoveTo(each, 100, 100);
each.draw(each);
}
// access a rectangle specific function
arectangle = makeRectangle(0, 0, 15, 15);
arectangle.setWidth(arectangle, 30);
arectangle.draw(arectangle);
</cfscript>
</body>
</html>
</cfoutput>
|