Rubber Band Technique in Computer Graphics

Rubber Band Technique:

This technique is employed extensively in graphics packages for drawing graphics entities using a mouse only. In this technique, when the user clicks the mouse button the starting position is established. As the user moves the mouse, the cursor moves from the first point to the second, the program displays a line from the first endpoint to the current or a circle centred at the first endpoint and passing through the current point or a rectangle with one corner at the first point and the other corner at the current point.

In this method, the user generally has to drag the mouse pointer, to move the mouse while cursor while the button is down. The entity shape or size is dynamically changed with every mouse movement while the button is down. When the button is released, the endpoint is frozen and the figure is there at its final shape, size and position.

Rubber Band Algorithm:

[c]
do
GetMousePosition(&button, &x, &y)
while(NOT (button=1 and MousePointer in WorkArea)

x1=previousx2=x
y1=previousy2=y
do
GetMousePosition(&button, &x, &y)
if MousePosition not equal to x1 & y1 then
previousx2=x2
previousy2=y2
x2=x
y2=y
if the mouse moved from the previous position,
MousePosition is not equal to x2 & y2 then
if previously a shape had been drawn
if (RestoreFlag=1) then
It is stored in Workarea from the stack
by calling the function RestoreFlag().

The corresponding shape is drawn with the corresponding colour
with appropriate parameters:

Line: x1, y1 as starting point
x2, y2 as an endpoint

Rectangle: x1, y1 as left-top corner
x2, y2 as right-bottom corner

Square: x1, y1 as left-top corner
Side = Minimum(x2-x1, y2-y1)

Circle: x1, y1 and x2, y2 are two ends of the diameter of the circle.
Centrex = (x1+x2)/2
Centrey = (y1+y2)/2
Radius = SquareRoot((x2-centrex) + (y2-centrey))

Ellipse: x1, y1 and x2, y2 are two ends of the diameter of the ellipse.
Centrex = (x1+x2)/2
Centrey = (y1+y2)/2
Radiusx = Absolute((x2-x1)/2)
Radiusy = Absolute((y2-y1)/2)

RestoreFlag=1
end if
end if
[/c]