Transformation – Programmerbay https://programmerbay.com A Tech Bay for Tech Savvy Sat, 20 Aug 2022 18:25:07 +0000 en-US hourly 1 https://wordpress.org/?v=6.5.5 https://programmerbay.com/wp-content/uploads/2019/09/cropped-without-transparent-32x32.jpg Transformation – Programmerbay https://programmerbay.com 32 32 C Program for 2D Translation https://programmerbay.com/c-program-for-2d-translation/ https://programmerbay.com/c-program-for-2d-translation/#respond Fri, 19 Aug 2022 09:55:19 +0000 https://www.programmerbay.com/?p=1837 2D Translation can be defined as a way of shifting an object from one point to another in a straight path. Here is the C program for demonstrating 2D translation

Must Read: What is 2D Translation?

Program to show the translation of a line

Program:

#include<conio.h>
#include<graphics.h>
#include<stdio.h>
void main()
{
int gd=DETECT,gm;
// declaring two array
// Translation vector already initialized
int l[2][2],v[2]={10,15},i=0,j;
clrscr();
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
printf("Enter the initial and final coordinates of a line ");

// Getting input from user, having 2D array where 1st row represents initial point
// And Second row represents final coordinate
while(i<2)
{
printf("x%d and y%d = ",i,i);
j=0;
scanf("%d",&l[i][j]);
scanf("%d",&l[i][j+1]);
i++;
}
// Line before translation
line(l[0][0],l[0][1],l[1][0],l[1][1]);
setcolor(BLUE);
// Line after translation
line(l[0][0]+v[0],l[0][1]+v[1],l[1][0]+v[0],l[1][1]+v[1]); // Adding Translation vector in it to change the position
getch();
closegraph();
}

 

Output:

translation using array

 

 

Related post with Example:

2D Translation: Positioning of an Object

]]>
https://programmerbay.com/c-program-for-2d-translation/feed/ 0
C Program to Show a Kite is Flying https://programmerbay.com/c-program-to-show-a-kite-is-flying/ https://programmerbay.com/c-program-to-show-a-kite-is-flying/#respond Wed, 03 Aug 2022 08:46:20 +0000 https://www.programmerbay.com/?p=1969 The program uses translation transformation to implement flying kite in C. In computer graphics, translation allows reposition of an object. We can create an illusion of flying kite by frequently changing the object’s position.

C Program to show a kite is flying

Program:

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<stdlib.h>
void main()
{
int gm,gd=DETECT;
int i= 0,j=0,rnd_x=0,rnd_y,stop_me=0;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
srand(time());
while(stop_me<=1000)
{

if(i>=180 &&j>=100 ) // controlling kite, so that it wouldn't disappear from screen
{
rnd_x=rand()%4 -3;
rnd_y=rand()%5 -4;
}
else{
rnd_x=rand()%3;
rnd_y=rand()%3;
}
line(200+i,200-j,250+i,250-j);
line(200+i,200-j,150+i,250-j);
line(150+i,250-j,200+i,350-j);
line(200+i,350-j,250+i,250-j);
line(200+i,200-j,200+i,350-j);
arc(200+i,275-j,25,155,50);
line(0,500,200+i,225-j);
i=i+rnd_x;
j=j+rnd_y;
stop_me=5+stop_me;
delay(100);
clearviewport(); // clearing image which would make illusion of flying kite

}
getch();
closegraph();

}

Output:

]]>
https://programmerbay.com/c-program-to-show-a-kite-is-flying/feed/ 0
C Program to Perform Shearing on Triangle https://programmerbay.com/c-program-to-perform-shearing-on-a-triangle/ https://programmerbay.com/c-program-to-perform-shearing-on-a-triangle/#respond Mon, 01 Aug 2022 11:55:53 +0000 https://www.programmerbay.com/?p=1928 In this article, we’ll be implementing shearing transformation program with output. In Transformation, shearing is the process of slanting the shape of an object.

C Program to perform shearing of a triangle along the x axis with output

Program:

#include<stdio.h>
#include<graphics.h>
#include<conio.h>
void main()
{
int gd=DETECT,gm;
int x,y,x1,y1,x2,y2,shear_f;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
printf("\n please enter first coordinate = ");
scanf("%d %d",&x,&y);
printf("\n please enter second coordinate = ");
scanf("%d %d",&x1,&y1);
printf("\n please enter third coordinate = ");
scanf("%d %d",&x2,&y2);
printf("\n please enter shearing factor x = ");
scanf("%d",&shear_f);
cleardevice();
line(x,y,x1,y1);
line(x1,y1,x2,y2);
line(x2,y2,x,y);

setcolor(RED);
x=x+ y*shear_f;
x1=x1+ y1*shear_f;
x2=x2+ y2*shear_f;

line(x,y,x1,y1);
line(x1,y1,x2,y2);
line(x2,y2,x,y);
getch();
closegraph();
}

 

Output:

triangle shearing x

C Program to perform shearing of a triangle along the y axis with output

Program:

#include<stdio.h>
#include<graphics.h>
#include<conio.h>
void main()
{
int gd=DETECT,gm;
int x,y,x1,y1,x2,y2,shear_f;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
printf("\n please enter first coordinate = ");
scanf("%d %d",&x,&y);
printf("\n please enter second coordinate = ");
scanf("%d %d",&x1,&y1);
printf("\n please enter third coordinate = ");
scanf("%d %d",&x2,&y2);
printf("\n please enter shearing factor y = ");
scanf("%d",&shear_f);
cleardevice();
line(x,y,x1,y1);
line(x1,y1,x2,y2);
line(x2,y2,x,y);

setcolor(RED);
y=y+ x*shear_f;
y1=y1+ x1*shear_f;
y2=y2+ x2*shear_f;

line(x,y,x1,y1);
line(x1,y1,x2,y2);
line(x2,y2,x,y);
getch();
closegraph();
}

Output:

triangle shearing y

]]>
https://programmerbay.com/c-program-to-perform-shearing-on-a-triangle/feed/ 0
C Program to Perform Shearing on a Rectangle https://programmerbay.com/c-program-to-perform-shearing-on-a-rectangle/ https://programmerbay.com/c-program-to-perform-shearing-on-a-rectangle/#respond Mon, 01 Aug 2022 10:54:48 +0000 https://www.programmerbay.com/?p=1919 In this article, we’ll be implementing a program for shearing transformation on a rectangle with output. In in computer graphics, shearing can be defined as process of changing shape of an object along x axis and y axis.

Program for shearing a rectangle along the x-axis in C with output

program:

#include<stdio.h>
#include<graphics.h>
#include<conio.h>
void main()
{
int gd=DETECT,gm;
int x,y,x1,y1,x2,y2,x3,y3,shear_f;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
printf("\n please enter first coordinate = ");
scanf("%d %d",&x,&y);
printf("\n please enter second coordinate = ");
scanf("%d %d",&x1,&y1);
printf("\n please enter third coordinate = ");
scanf("%d %d",&x2,&y2);
printf("\n please enter last coordinate = ");
scanf("%d %d",&x3,&y3);
printf("\n please enter shearing factor x = ");
scanf("%d",&shear_f);
cleardevice();
line(x,y,x1,y1);
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x,y);

setcolor(RED);
x=x+ y*shear_f;
x1=x1+ y1*shear_f;
x2=x2+ y2*shear_f;
x3=x3+ y3*shear_f;

line(x,y,x1,y1);
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x,y);
getch();
closegraph();
}

 

Output:

shearing x rectangle

Program for shearing a rectangle along the y axis in C with output

Program:

#include<stdio.h>
#include<graphics.h>
#include<conio.h>
void main()
{
int gd=DETECT,gm;
int x,y,x1,y1,x2,y2,x3,y3,shear_f;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
printf("\n please enter first coordinate = ");
scanf("%d %d",&x,&y);
printf("\n please enter second coordinate = ");
scanf("%d %d",&x1,&y1);
printf("\n please enter third coordinate = ");
scanf("%d %d",&x2,&y2);
printf("\n please enter last coordinate = ");
scanf("%d %d",&x3,&y3);
printf("\n please enter shearing factor y = ");
scanf("%d",&shear_f);
cleardevice();
line(x,y,x1,y1);
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x,y);

setcolor(RED);
y=y+ x*shear_f;
y1=y1+ x1*shear_f;
y2=y2+ x2*shear_f;
y3=y3+ x3*shear_f;

line(x,y,x1,y1);
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x,y);
getch();
closegraph();
}

 

 

Output:

rectangle y axis

]]>
https://programmerbay.com/c-program-to-perform-shearing-on-a-rectangle/feed/ 0
Program To Perform Scaling on a Triangle in C https://programmerbay.com/program-to-perform-scaling-on-a-triangle/ https://programmerbay.com/program-to-perform-scaling-on-a-triangle/#respond Wed, 27 Jul 2022 17:19:15 +0000 https://www.programmerbay.com/?p=1868 In this article, we’ll be implementing a program to demonstrate 2D scaling of a triangle in computer graphics using C.

Scaling can be defined as a process of changing or altering the size of an object. It can be used to increase or decrease the size of the respective object. Below is the program:

Program to Scale a Triangle in C

program:

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main(){
int x,y,x1,y1,x2,y2;
int scl_fctr_x,scl_fctr_y;
int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
printf("\t\t\t********** Scaling ***********\n");
printf("\n\t\t\t Please enter first coordinate of Triangle = ");
scanf("%d %d",&x,&y);
printf("\n\t\t\t Please enter second coordinate of Triangle = ");
scanf("%d %d",&x1,&y1);
printf("\n\t\t\t Please enter third coordinate of Triangle = ");
scanf("%d %d",&x2,&y2);
line(x,y,x1,y1);
line(x1,y1,x2,y2);
line(x2,y2,x,y);
printf("\n\t\t\t Now Enter Scaling factor x and y = ");
scanf("%d %d",&scl_fctr_x,&scl_fctr_y);
x = x* scl_fctr_x;
x1 = x1* scl_fctr_x;
x2 = x2* scl_fctr_x;
y = y* scl_fctr_y;
y1 = y1* scl_fctr_y;
y2= y2 * scl_fctr_y ;

line(x,y,x1,y1);
line(x1,y1,x2,y2);
line(x2,y2,x,y);
getch();
closegraph();
}

Output:

scaling

 

 

]]>
https://programmerbay.com/program-to-perform-scaling-on-a-triangle/feed/ 0
C Program to Rotate a Line https://programmerbay.com/c-program-to-rotate-a-line/ https://programmerbay.com/c-program-to-rotate-a-line/#respond Wed, 27 Jul 2022 12:48:40 +0000 https://www.programmerbay.com/?p=1863 Rotation can be defined as moving an object in a circular path at a given angle theta.

If there is a positive angle, it would rotate in anticlockwise whereas if it appears to a negative angle, the object would rotate in clockwise. Here’s is C program to rotate a line in computer Graphics. The below program is rotation program in C.

Program to show rotation of a line in Computer Graphics

Program:

#include<graphics.h>
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int gd=DETECT,gm;
int pivot_x,pivot_y,x,y;
double degree,radian;
int rotated_point_x,rotated_point_y;
initgraph(&gd,&gm,"C://TURBOC3//BGI");
cleardevice();
printf("\t\t*********** ROTATION *********** \n");
printf("\n Enter an initial coordinates of the line = ");
scanf("%d %d",&pivot_x,&pivot_y);
printf("\n Enter a final coordinates of the line = ");
scanf("%d %d",&x,&y);
line(pivot_x,pivot_y,x,y);
printf("\n\n Now, Enter a degree = ");
scanf("%lf",&degree);
radian=degree*0.01745;
rotated_point_x=(int)(pivot_x +((x-pivot_x)*cos(radian)-(y-pivot_y)*sin(radian)));
rotated_point_y=(int)(pivot_y +((x-pivot_x)*sin(radian)+(y-pivot_y)*cos(radian)));
setcolor(RED);
line(pivot_x,pivot_y,rotated_point_x,rotated_point_y);
getch();
closegraph();
}

 

Output:

Rotation by 30

rotation 30

Rotation by 90

rotation 90

]]>
https://programmerbay.com/c-program-to-rotate-a-line/feed/ 0
Types of Transformations in Computer Graphics https://programmerbay.com/what-is-2d-transformation-in-c-graphics/ https://programmerbay.com/what-is-2d-transformation-in-c-graphics/#respond Thu, 14 Jul 2022 17:57:24 +0000 https://programmerbay.com/?p=5145 Transformation

Transformation can be defined as repositioning of coordinates, size or orientation of an object. There are various types of transformations namely translation, rotation scaling, etc. A transformation can be 2D and 3D. In this, We will study about 2D transformation that forms using (x,y) coordinates


Types of Transformations

In computer graphics, there are various types of transformation :

Translation
Rotation
Scaling
Reflection
Shearing

Translation

A translation is a repositioning of an object along a straight line by adding a translation factor to the original coordinates of the given object.
A translation factor is also known as the translation vector.
Suppose, (x,y) is a position of an object. (tx,ty) is a translation vector that to be added to the given coordinate (x,y). After adding them, the object would be placed at a new coordinate.

x’=x+tx ,

y’=y+ty

Rotation

Rotation can be defined as moving an object in a circular path at a given angle theta. if there is a positive angle, it would rotate in anticlockwise whereas if it appears to a negative angle, the object would rotate in clockwise. The point at which an object about to rotate is known as a pivot point.

x’= x cosθ – y sinθ

y’= x sinθ + y cosθ

Scaling

Scaling is a redefining the size of an object or changing the size of an object by multiplying scaling factor to it.
x’ = x*Sx ,

y’=y.Sy

Reflection

Reflection has nothing to do with size, it is a repositioning of an object relative to xy quadrants that produce a mirror image.

Shear

Shearing is a process of slanting the edges of an object. it can be of two types x shear and y shear
In x-shear, we don’t touch y-coordinates, but changes are made to x coordinate which produces a  right or left tilted object
In y shear, we don’t touch x coordinate, instead, changes are made to x coordinate which produces a right or left tilted object

]]>
https://programmerbay.com/what-is-2d-transformation-in-c-graphics/feed/ 0