2d Shearing Program in C
Shearing modifies a 2D object's size and shape along the x and y axes. It's like dragging each layer in one way to reshape a two-dimensional object. It is the perfect method for altering an object's shape in a two-dimensional space. The X and Y directions can alter an object's size in a two-dimensional plane.
- Shear x-ray:
The x coordinates shift in x shear, but the coordinates of y stay the same. If the point is P(x, y), the new points are P'(x', y'), which are indicated as follows:
y'=y; x'=x+Sh_x*y
- Y-Shear:
In y Shear, the y coordinates shift while the x coordinates stay the same. If P(x, y) is the point, then P'(x', y') will be the new points, with the formula
x'=x; y'=y+Sh_y*x.
- x-y Shear:
Both the x and y coordinates change in an x-y shear. The new points, P'(x', y'), will be presented as follows if P(x, y) is the point:
x'= x+Sh_x*y; y'= y+Sh_y*x.
Two primary forms of two-dimensional shearing exist:
Vertical (y-axis) and horizontal (x-axis) shearing.
- X-Axis horizontal shearing:
Each point on a horizontal line is displaced by a distance proportional to its separation from a stationary vertical line in a process known as horizontal shearing.
- Vertical Shearing on the Y-Axis:
Every point on a vertical line is moved by a distance proportional to its separation from a stationary horizontal line in a process known as vertical shearing.
C code for doing out 2D shearing along the x-axis:
Example code:
#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 enter the first coordinate = ");
scanf("%d %d",&x,&y);
printf("\n enter the second coordinate = ");
scanf("%d %d",&x1,&y1);
printf("\n enter the third coordinate = ");
scanf("%d %d",&x2,&y2);
printf("\n enter the last coordinate = ");
scanf("%d %d",&x3,&y3);
printf("\n enter the 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:
C code for doing out 2D shearing along the y-axis:
Example Code:
#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 enter the first coordinate = ");
scanf("%d %d",&x,&y);
printf("\n enter the second coordinate = ");
scanf("%d %d",&x1,&y1);
printf("\n enter the third coordinate = ");
scanf("%d %d",&x2,&y2);
printf("\n enter the last coordinate = ");
scanf("%d %d",&x3,&y3);
printf("\n enter the 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:
Uses for two-dimensional shearing
- Replicate Motion:
In a 2D scene, shearing might mimic the look of motion. Shearing an object vertically, for instance, might create the appearance that it is leaning or slipping in one way.
- Text Italics:
Shearing is a common typographic and graphics design technique to produce slanted or italic text. It offers a method of producing text at a certain angle without modifying the typeface.
- Slanted Shape Creation:
Shearing is a tool that may be used to skew or slant shapes. This helps to create ensembles that are dynamic and visually appealing in design and visuals.
- Picture Distortion:
A picture can be carefully distorted by using shearing. You can use this distortion to make interesting effects or for artistic purposes.
- Computer-Aided Design, or CAD:
Shearing is a technique used in CAD systems to modify object forms. This can help change how components are oriented inside a design.
- Computer Games:
In-game graphics, shearing can produce dynamic effects like tilting or inclined game objects to represent motion or a dynamic environment.
- Animation:
In movement, shearing is a helpful transition to produce eye-catching effects. It might be applied to certain frames or items to evoke a sensation of distortion or motion.
- Designing a user interface with graphics (GUI):
Shearing might be used to add perspective or depth to graphical objects in user interfaces. Both the user experience and visual attractiveness may benefit from this.
- Augmented reality (AR) and virtual reality (VR):
Shearing is useful for changing how items seem in augmented and virtual reality settings. It facilitates the development of realistic attitudes and viewpoints.
- Digital Illustration and Art:
Shearing is a tool artists use to reshape and distort objects in digital artwork. It allows artists to express their creativity and lets them try out various visual aesthetics.