In this program, we will draw a rectangle and a bar on screen. We will use rectangle and bar functions of graphics.h header file to draw rectangle and bar on screen. Below is the detailed descriptions if these two functions.
C program to draw a rectangle using Graphics. COMPUTER GRAPHICS - BASIC 2D. 2D Transformation. We can have various types of transformations such as translation, scaling up or down, rotation, shearing, etc. When a transformation takes place on a 2D plane, it is called 2D transformation. Transformations play an important role in computer graphics to reposition the graphics on the screen and change their size or orientation.
rectangle function draws a rectangle on screen. It takes the coordinates of top left and bottom right corners.
bar function draws a rectangle and fill it with current fill pattern and color.
Function Argument | Description |
---|---|
xTopLeft | X coordinate of top left corner. |
yTopLeft | Y coordinate of top left corner. |
xBottomRight | X coordinate of bottom right corner. |
yBottomRight | Y coordinate of bottom right corner. |
C program to draw rectangle and bar using graphics
Program OutputRelated Topics
C graphics program to draw an eclipse |
C graphics program to draw stars in night sky |
C graphics program to draw concentric circles |
C graphics program for moving car animation |
C program to draw 3D bar graph |
C program to draw pie chart using graphics |
C program for bouncing ball animation |
List of all C Graphics Programs |
A scaling transformation alters size of an object. In the scaling process, we either compress or expand the dimension of the object.
Scaling operation can be achieved by multiplying each vertex coordinate (x, y) of the polygon by scaling factor sx and sy to produce the transformed coordinates as (x’, y’).
So, x’ = x * sx and y’ = y * sy.
The scaling factor sx, sy scales the object in X and Y direction respectively. So, the above equation can be represented in matrix form:
Or P’ = S . P
Scaling process:
Note: If the scaling factor S is less than 1, then we reduce the size of the object. If the scaling factor S is greater than 1, then we increase size of the object.
Algorithm:
![Program Program](http://2.bp.blogspot.com/-WvCsPg0BLwU/UtEDHq2wCHI/AAAAAAAAAY4/mA3rgUP1sCU/s1600/transformation2.png)
Below is C implementation:
#include<stdio.h> // Matrix Multiplication to find new Coordinates. // points that needs to be scaled. // p[1][0] is y coordinate of given point. { for ( int j = 0; j < 1; j++) temp[i][j] += (s[i][k] * p[k][j]); p[0][0] = temp[0][0]; } // Scaling the Polygon { line(x[0], y[0], x[1], y[1]); line(x[2], y[2], x[0], y[0]); // Initializing the Scaling Matrix. int p[2][1]; // Scaling the triangle { p[1][0] = y[i]; findNewCoordinate(s, p); x[i] = p[0][0]; } // Triangle after Scaling line(x[1], y[1], x[2], y[2]); } // Driven Program { int y[] = { 200, 100, 200 }; detectgraph(&gd, &gm); getch(); return 0; |
Output:
This article is contributed by Anuj Chauhan. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to [email protected]. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.