Sunday, 17 April 2016

Computer Graphics, Chapter 4: The Magic of 2D Geometrical Transformations

Computer Graphics, Chapter 4: The Magic of 2D Geometrical Transformations

By: Prof. Dr. Zeeshan Bhatti

2D Transformation in Computer Graphics | 2D Translate Rotate object in Computer Graphics 🎮09

Welcome back, digital creators! Professor Dr. Zeeshan Bhatti here from Zeeshan Academy. Today, we're unlocking one of the most fundamental and visually intuitive concepts in computer graphics: 2D Geometrical Transformations.

If you've ever dragged an icon on your desktop, rotated an image in a photo editor, or watched an animated character grow and shrink on screen, you've already witnessed transformations in action. In essence, these operations are the digital puppetry that brings static images to life. So, let's pull back the curtain and understand the core principles that make it all happen.


Prefer a visual walkthrough? Watch the full video lecture here: "2D Transformation in Computer Graphics" on the Zeeshan Academy YouTube channel.


What Are 2D Geometrical Transformations?

In many applications of computer graphics, we need to alter and manipulate displayed graphical objects. This can be done either interactively (like when you're using a design tool) or non-interactively (like in a pre-rendered animation).

Think about it: when you select an icon and move it from one location to another, you are performing a transformation. Similarly, resizing a window or spinning a 3D model in a viewer all fall under this umbrella.

Technically, geometrical transformations are the tools and techniques we use to modify an object's shape, size, position, and orientation on the screen. They are the mathematical rules that tell each pixel of an object where to go next.

The Fantastic Four: Core Types of 2D Transformations

While there are complex transformations, everything is built upon four fundamental types. Let's meet the team:

  1. Translation: This is a fancy term for movement. It involves shifting an object from one location to another without changing its shape, size, or orientation. Imagine sliding a coffee mug across your desk—that's translation.

  2. Rotation: As the name implies, this is all about turning. Rotation changes the object's orientation by spinning it around a fixed point, known as the pivot point. Picture the hands of a clock moving—each hour, they perform a rotation.

  3. Scaling: This transformation changes the size of an object. It can make an object larger (scaling up) or smaller (scaling down). Importantly, scaling can be uniform (changing height and width by the same factor) or non-uniform (stretching taller or wider). Zooming in on a picture is a form of scaling.

  4. Shear: This is the most dramatic of the four, as it distorts the shape of an object, slanting it along an axis. Think of it as pushing the top of a stack of books sideways while keeping the bottom fixed, creating a parallelogram shape. It's like the digital version of a funhouse mirror effect.

It's also worth mentioning Reflection, which is often considered a special case of scaling (specifically, negative scaling), as it creates a mirror image of an object.

Now, let's dive deeper into each one.


1. Translation: The Simple Slide

Translation is arguably the easiest transformation to understand. You simply move every point of the object by the same amount in the x and y directions.

  • The Logic: For a point (x, y), the new translated point (x', y') is found by:

    • x' = x + tx

    • y' = y + ty
      ...where tx is the translation in the x-direction and ty is the translation in the y-direction.

  • Real-World Analogy: Moving a piece on a chessboard. Each point on the piece moves the exact same number of squares.


2. Rotation: The Elegant Spin

Rotation is a bit more mathematically involved, as it requires trigonometry. We rotate an object by a specific angle (θ - theta) around a pivot point, typically the origin (0,0).

  • The Logic: The rotation formulas for a point (x, y) around the origin are:

    • x' = x * cos(θ) - y * sin(θ)

    • y' = x * sin(θ) + y * cos(θ)

  • Key Insight: Notice how the new x-coordinate depends on both the original x and y coordinates. This interdependence is what creates the circular motion. Furthermore, the angle θ is measured in radians in most programming languages, so don't forget to convert from degrees!


3. Scaling: Sizing Things Up (or Down)

Scaling multiplies the coordinates of an object by scaling factors to change its size.

  • The Logic: To scale a point (x, y):

    • x' = x * sx

    • y' = y * sy
      ...where sx is the scaling factor in the x-direction and sy is the scaling factor in the y-direction.

  • Important Consideration: If sx and sy are the same, the scaling is uniform, and the object's proportions are maintained. If they are different, the object will be stretched or squashed. Moreover, scaling is usually done relative to a fixed point. If you scale an object not centered at the origin, it will also appear to move—a crucial point for animators to remember!


4. Shear: The Art of Distortion

Shearing slants the object, transforming a rectangle into a parallelogram. It comes in two primary flavors: shear in the x-direction and shear in the y-direction.

  • The Logic (X-Shear): This slants the object along the x-axis.

    • x' = x + shx * y

    • y' = y
      ...where shx is the shear parameter. Notice that the new x-coordinate is influenced by the original y-coordinate, which is what causes the slant.


The Secret Sauce: Matrix Notation and Homogeneous Coordinates

Now, here's where the magic truly unfolds. While we can handle each transformation with separate equations, this becomes messy when we want to combine them. For instance, how do you efficiently rotate an object around a point other than the origin? The answer lies in matrices and homogeneous coordinates.

  • Why Matrices? Matrices allow us to represent each transformation (translation, rotation, scaling) as a compact, standard mathematical object. This is incredibly efficient for computers to process.

  • The Problem with Translation: Look at the translation equations again. They use addition (x + tx), while rotation and scaling use multiplication. This inconsistency makes it hard to combine them.

  • The Solution - Homogeneous Coordinates: To solve this, we introduce a clever trick: we add a third coordinate, w, to our 2D points. We represent a 2D point (x, y) as (x, y, 1) in homogeneous coordinates. This allows us to represent all transformations—including translation—as matrix multiplications!

    • Translation Matrix:

      text
      [ 1  0  tx ]
      [ 0  1  ty ]
      [ 0  0  1  ]
    • Rotation Matrix:

      text
      [ cos(θ)  -sin(θ)  0 ]
      [ sin(θ)   cos(θ)  0 ]
      [   0        0     1 ]
    • Scaling Matrix:

      text
      [ sx  0   0 ]
      [ 0   sy  0 ]
      [ 0   0   1 ]

The power of this approach is that to apply multiple transformations, you simply multiply the matrices together to form a single composition matrix. You then apply this one matrix to every point in your object. This is vastly more efficient than applying transformations one after the other.

Download Slides:


Why This Matters: The Power of Composition

Composition is the heart of complex animation and modeling. Let's say you want to rotate a spaceship around its own center and then move it across the screen.

  1. Without composition, you would first calculate new coordinates for every point after rotation, and then calculate the translation for all these new points.

  2. With composition, you multiply the rotation matrix and the translation matrix to create a single "rotate-and-move" matrix. You then apply this single matrix to the original coordinates of the spaceship. This cuts the computational workload in half!

This principle is used everywhere, from the game you're playing on your phone to the special effects in the latest blockbuster movie.


Here is the Summary of What we Have Learned today:



There are generally four basic types of transformations namely, translation, rotation, scaling, and Shear. Whereas, reflection being part of transformation is usually considered as oppsite or negative scale. 
  • Translation means change in position, location or displacement of object on screen.
  • Rotation means change in direction, orientation, or angle of object.
  • Scale means change in size of object.
  • Shear is an operation referring to change in proportion of an object, when the shape of object is modified or altered to a different unique form. 

2D Transformation

 Lecture Contents

 • Translation
 • Scaling
 • Rotation
 • Shear
 • Matrix notation
 • Compositions
 • Homogeneous coordinates

Conclusion: You Are Now a Transformation Wizard!

Congratulations! You've just navigated the core concepts that form the backbone of manipulation in computer graphics. You now understand that every movement, turn, and resize on your screen is a precise mathematical operation.

To recap, we've covered:

  • The definition and importance of 2D Geometrical Transformations.

  • The four fundamental types: Translation, Rotation, Scaling, and Shear.

  • The mathematical logic behind each one.

  • The powerful concept of Matrix Notation and Homogeneous Coordinates that unifies them all.

  • The efficiency of Composition for applying multiple transformations.

This knowledge is not just theoretical; it's the practical foundation you will use to start creating your own graphics programs.

Ready to put this theory into code? In our next blog post, we'll roll up our sleeves and implement every single one of these transformations in C++ and MATLAB. You'll see the formulas come to life on screen!

For now, if you want to solidify your understanding, download the full lecture slides and review the key points.

Keep exploring, and I'll see you in the next lecture!


Instructor: Prof. Dr. Zeeshan Bhatti
YouTube Channel: Zeeshan Academy

No comments:

Post a Comment

Featured post

Beginner? Get Your First Vibe Coding Project Done in 5 Minutes (Free Template Included!)

Beginner? Get Your First Vibe Coding Project Done in 5 Minutes (Free Template Included!) So, you’ve heard about Vibe Coding , right? Ma...