-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScale_Object_in_2D.cpp
70 lines (57 loc) · 1.57 KB
/
Scale_Object_in_2D.cpp
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
60
61
62
63
64
65
66
67
68
69
70
// Write a Program to scale an Object in 2D plane.
#include<stdio.h>
#include<graphics.h>
// Matrix Multiplication to find new Coordinates.
// s[][] is scaling matrix. p[][] is to store
// points that needs to be scaled.
// p[0][0] is x coordinate of point.
// p[1][0] is y coordinate of given point.
void findNewCoordinate(int s[][2], int p[][1])
{
int temp[2][1] = { 0 };
for (int i = 0; i < 2; i++)
for (int j = 0; j < 1; j++)
for (int k = 0; k < 2; k++)
temp[i][j] += (s[i][k] * p[k][j]);
p[0][0] = temp[0][0];
p[1][0] = temp[1][0];
}
// Scaling the Polygon
void scale(int x[], int y[], int sx, int sy)
{
// Triangle before Scaling
line(x[0], y[0], x[1], y[1]);
line(x[1], y[1], x[2], y[2]);
line(x[2], y[2], x[0], y[0]);
// Initializing the Scaling Matrix.
int s[2][2] = { sx, 0, 0, sy };
int p[2][1];
// Scaling the triangle
for (int i = 0; i < 3; i++)
{
p[0][0] = x[i];
p[1][0] = y[i];
findNewCoordinate(s, p);
x[i] = p[0][0];
y[i] = p[1][0];
}
// Triangle after Scaling
line(x[0], y[0], x[1], y[1]);
line(x[1], y[1], x[2], y[2]);
line(x[2], y[2], x[0], y[0]);
}
// Driven Program
int main()
{
int x[] = { 100, 200, 300 };
int y[] = { 200, 100, 200 };
int sx = 2, sy = 2;
int gd, gm;
detectgraph(&gd, &gm);
initgraph(&gd, &gm," ");
scale(x, y, sx,sy);
int temp;
scanf("%d", temp);
getch();
return 0;
}