2D Transformations: Scaling

On August 4, 2012, in Algorithms, Drawing, Shapes, by , Views : 23792


Scaling is a linear transformation that enlarges or diminishes objects. When we talk about scaling we usually mean some amount of scaling along each dimension. That is, we must specify how much to change the size along each dimension. To know more on what scaling is and its matrix representation see 2D Transformations#Scaling.

Getting the inputs

Taking in the inputs

Scaling_Original_Figure

The Original Figure entered.

Scaling_Scaled_Figure

Scaled Figure.

Function:

1
2
3
4
5
6
7
8
void scale( int figure[], int edges, int dx, int dy, int cx, int cy )
{
	for(int i=0; i < edges; i++)
	{
		figure[2*i] = (figure[2*i] - cx) * dx + cx;
		figure[2*i+1] = (figure[2*i+1] - cy) * dy + cy;
	}
}

Example Usage:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include<graphics.h>
#include<stdio.h>
#include<conio.h>
 
 
void scale( int figure[], int edges, int dx, int dy, int cx, int cy )
{
	for(int i=0; i < edges; i++)
	{
		figure[2*i] = (figure[2*i] - cx) * dx + cx;
		figure[2*i+1] = (figure[2*i+1] - cy) * dy + cy;
	}
}
 
void main()
{
	int figure[20], edges; // A Figure with Max 10 edges.
	int dx, dy, cx=0, cy=0; 
	int gd = DETECT, gm;
	clrscr();
 
	printf( "Number of edges: " );
	scanf( "%d", &edges );
 
	for(int i=0; i < edges; i++)
	{
		printf( "Enter edge (x%d,y%d) : ", i , i );
		scanf( "%d %d", &figure[2*i], &figure[2*i+1] );
	}
	figure[2*i] = figure[0];
	figure[2*i+1] = figure[1];
	edges += 1;
 
	printf( "Enter dx: ");
	scanf( "%d", &dx);
	printf( "Enter dy: ");
	scanf( "%d", &dy);
 
	printf( "Enter the center of scaling: \n");
	printf( "cx: ");
	scanf( "%d", &cx);
	printf( "cy: ");
	scanf( "%d", &cy);
 
	initgraph( &gd, &gm, "" );
	cleardevice();
	setbkcolor(WHITE);
 
	setcolor(GREEN);
	setlinestyle(SOLID_LINE, 0, 3);
	drawpoly( edges, figure );
	getch();
 
	scale(figure,edges,dx,dy,cx,cy);
 
	setcolor(RED);
	drawpoly( edges, figure );
	getch();
}

Hope, it helps. 🙂



5 Responses to 2D Transformations: Scaling

  1. Sameer Bhatkar says:

    U guys provide outstanding solutions to all and any complicated programs for some of the tough aims…thanks to all of u….i just needed to ask U guys that…since I use a Windows 7 ultimate version of OS..turbo c aint compatible on it..it either doesnt install.else shows no outputs for even the simplest programs…can u guys please suggest me a way of installing and running turbo C on Win7 with proper utilisation of the services provided by the C software

  2. inzamam says:

    Sir transformation ke har program me Aap
    [2*i] & [2*i+1] kyo use karte hai please explain it

    • Hi,

      I am using ‘drawpoly’ to draw the shape.

      drawpoly(int numpoints, int *polypoints)

      The first parameter of drawpoly is the number of points, and the second one is an array with the coordinates of the points.

      eg: For the edge (x1, y1), x1 is stored in array[0] and y1 in array[1].

      Why array[2*i] and array[2*i+1]:

      i starts with 0.
      So, the 1st edge is stored in array[0] and array[1].
      For the second edge, i is 1, so its stored in array[2] and array[3].

      Hope it helps.

  3. Mohan Raj says:

    Awesome… Easily understanding prgms….

Leave a Reply

Your email address will not be published. Required fields are marked *


*

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Visit our friends!

A few highly recommended friends...

  • Dipin Krishna

Archives

All entries, chronologically...

Read more:
wheel
Moving Wheel using Mid-Point Circle algorithm and DDA Line algorithm

CIRCLE
Circle using Mid-Point Algorithm

Rotation
2D Transformations: Rotation

MinMaxClose