Transformations


  Tutorial Home
Intro

It is really helpful to be able to create drawable things such as faces, dogs, piglets and spaceships. However, once we create them we want to move them around, make them bigger or smaller and also rotate them. The Group class has the ability to do all of these things. This makes it very useful when creating new pictures.

There are three basic transformations: scale, rotate and translate. No matter what order you set them in your program they are always performed in this order. Scaling changes the size of things. Rotation spins them around counter-clockwise. Translation moves them to a new location.

Transformations can only be applied to Group objects. If we want to transform a rectangle for example, we first put it into a Group.

Scaling

Scaling and object is the mathematical term for changing its size. We scale objects by multiplying each of their dimensions by a scale factor. Scale factors of 1.0 do nothing because multiplying by 1.0 yields the same value. Scaling by a number between 0.0 and 1.0 will make an object smaller. Scaling by a value greater than 1.0 will make it larger. The following code shows what will happen when scaling is applied to a rectangle.

	public static void main(String[] args) 
	{
		Win2D win = new Win2D("Tutorial",10,20,400,300);
		
		Group container = new Group();
		win.add(container);
		container.add(new Rect(0,0,5,5));
			// Adds a new square to the container
		container.setScale(0.5,2);
			// Make the square half as wide and twice as high.
		
		win.add(new Axes());

	}

The resulting view is :

Scaling

Rotation

Rotation is performed by specifying a counter-clockwise angle in degrees around the origin. Rotation on an object is performed after the scaling. If we wanted, we could take our rectangle object from the previous problem and tip it over using the following code.

public static void main(String[] args) 
	{
		Win2D win = new Win2D("Tutorial",10,20,400,300);
		
		Group container = new Group();
		win.add(container);
		container.add(new Rect(0,0,5,5));
			// Adds a new square to the container
		container.setScale(0.5,2);
			// Make the square half as wide and twice as high.
		container.setRotate(30);
			// rotate the rectangle 30 degrees
		
		win.add(new Axes());

	}

The new appearance is:

Transform 5

 

Translation

After an object is scaled and rotated, we can change its location. This is called translation. The location of the object is the origin. We can change the location of the object by setting its location as shown in the code below.

public static void main(String[] args) 
	{
		Win2D win = new Win2D("Tutorial",10,20,400,300);
		
		Group container = new Group();
		win.add(container);
		container.add(new Rect(0,0,5,5));
			// Adds a new square to the container
		container.setScale(0.5,2);
			// Make the square half as wide and twice as high.
		container.setRotate(30);
			// rotate the rectangle 30 degrees
		container.setTranslate(1, 3);
			// move the rectangle's origin to the point [1,3]
		win.add(new Axes());

	}

 

Transform 6