Advertisement
khatta_cornetto

c g

Oct 25th, 2024 (edited)
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.67 KB | None | 0 0
  1. #include <graphics.h>
  2. #include <stdio.h>
  3. #include <conio.h>
  4.  
  5. int main() {
  6.     int gd = DETECT, gm;
  7.     initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
  8.    
  9.     // Drawing a House
  10.     rectangle(100, 200, 300, 350);    // Main body
  11.     line(100, 200, 200, 100);         // Left roof line
  12.     line(200, 100, 300, 200);         // Right roof line
  13.     rectangle(170, 250, 230, 350);    // Door
  14.     rectangle(120, 230, 160, 270);    // Window
  15.    
  16.     // Drawing a Bottle
  17.     rectangle(400, 200, 450, 350);    // Body
  18.     arc(425, 200, 0, 180, 25);       // Top curve
  19.     rectangle(415, 150, 435, 200);    // Neck
  20.    
  21.     // Drawing a Cup
  22.     rectangle(500, 250, 550, 300);    // Body
  23.     arc(525, 250, 0, 180, 25);       // Top curve
  24.     line(550, 250, 570, 270);        // Handle top
  25.     line(550, 300, 570, 280);        // Handle bottom
  26.     line(570, 270, 570, 280);        // Handle vertical
  27.    
  28.     // Drawing a Fan
  29.     circle(300, 450, 30);            // Center circle
  30.     line(300, 450, 350, 400);        // Blade 1
  31.     line(300, 450, 250, 400);        // Blade 2
  32.     line(300, 450, 350, 500);        // Blade 3
  33.     line(300, 450, 250, 500);        // Blade 4
  34.    
  35.     getch();
  36.     closegraph();
  37.     return 0;
  38. }
  39.  
  40.  
  41.  
  42.  
  43.  
  44.  
  45. Here are the most commonly used graphics functions:
  46.  
  47. 1. Basic Initialization Functions:
  48. ```c
  49. initgraph(&gd, &gm, "path");  // Initialize graphics mode
  50. closegraph();                  // Close graphics mode
  51. ```
  52.  
  53. 1.2
  54.  
  55. int points[] = {100, 100, 200, 100, 150, 50, 100, 100};  // A triangle (last point connects back to start)
  56. drawpoly(4, points);  // 4 points including the closing point
  57.  
  58.  
  59. 2. Drawing Basic Shapes:
  60. ```c
  61. // Draw a pixel
  62. putpixel(int x, int y, int color);
  63.  
  64. // Draw a line
  65. line(int x1, int y1, int x2, int y2);
  66.  
  67. // Draw a rectangle
  68. rectangle(int left, int top, int right, int bottom);
  69.  
  70. // Draw a circle
  71. circle(int x, int y, int radius);
  72.  
  73. // Draw an ellipse
  74. ellipse(int x, int y, int start_angle, int end_angle, int x_radius, int y_radius);
  75.  
  76. // Draw an arc
  77. arc(int x, int y, int start_angle, int end_angle, int radius);
  78. ```
  79.  
  80. 3. Color and Fill Functions:
  81. ```c
  82. // Set current drawing color
  83. setcolor(int color);
  84.  
  85. // Fill an enclosed area
  86. floodfill(int x, int y, int border_color);
  87.  
  88. // Set fill pattern and color
  89. setfillstyle(int pattern, int color);
  90. ```
  91.  
  92. 4. Text Functions:
  93. ```c
  94. // Output text at specified position
  95. outtextxy(int x, int y, "text");
  96.  
  97. // Set text style
  98. settextstyle(int font, int direction, int size);
  99.  
  100. // Set text justification
  101. settextjustify(int horiz, int vert);
  102. ```
  103.  
  104. 5. Screen and Window Functions:
  105. ```c
  106. // Clear screen
  107. cleardevice();
  108.  
  109. // Clear viewport
  110. clearviewport();
  111.  
  112. // Set viewport
  113. setviewport(int left, int top, int right, int bottom, int clip);
  114. ```
  115.  
  116. Common Color Constants:
  117. ```c
  118. BLACK       // 0
  119. BLUE        // 1
  120. GREEN       // 2
  121. CYAN        // 3
  122. RED         // 4
  123. MAGENTA     // 5
  124. BROWN       // 6
  125. LIGHTGRAY   // 7
  126. DARKGRAY    // 8
  127. LIGHTBLUE   // 9
  128. LIGHTGREEN  // 10
  129. LIGHTCYAN   // 11
  130. LIGHTRED    // 12
  131. LIGHTMAGENTA// 13
  132. YELLOW      // 14
  133. WHITE       // 15
  134. ```
  135.  
  136. Here's a simple example of how to use these functions together:
  137.  
  138. ```c
  139. #include <graphics.h>
  140.  
  141. int main() {
  142.    int gd = DETECT, gm;
  143.    initgraph(&gd, &gm, "");
  144.    
  145.    // Draw a house
  146.    setcolor(BLUE);
  147.    rectangle(100, 200, 300, 350);    // House body
  148.    
  149.    setcolor(RED);
  150.    line(100, 200, 200, 100);        // Roof left
  151.    line(200, 100, 300, 200);        // Roof right
  152.    
  153.    setcolor(BROWN);
  154.    rectangle(175, 250, 225, 350);    // Door
  155.    
  156.    setcolor(WHITE);
  157.    circle(190, 300, 5);             // Door handle
  158.    
  159.    getch();
  160.    closegraph();
  161.    return 0;
  162. }
  163. ```
  164.  
  165.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement