How to resolve the algorithm Colour pinstripe/Display step by step in the C programming language
Published on 7 June 2024 03:52 AM
How to resolve the algorithm Colour pinstripe/Display step by step in the C programming language
Table of Contents
Problem Statement
The task is to create 1 pixel wide coloured vertical pinstripes with a sufficient number of pinstripes to span the entire width of the graphics display.
The pinstripes should either follow the system palette sequence, or a sequence that includes: black, red, green, blue, magenta, cyan, yellow, and white:
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Colour pinstripe/Display step by step in the C programming language
The provided C code uses the graphics.h
and conio.h
libraries to create a graphical interface and draw colored bars across the screen. Here's a detailed breakdown of the code:
-
Header Files:
#include<graphics.h>
: This header file is included to access the graphics functions of the Borland Graphics Interface (BGI) library.#include<conio.h>
: This header file is included to use thegetch()
function for getting a character from the keyboard.
-
Constants:
#define sections 4
: This defines a constant namedsections
with the value 4, which is used to divide the screen into four sections vertically.
-
Main Function:
int d=DETECT,m;
: This declares an integerd
and initializes it withDETECT
, which tells the graphics system to auto-detect the graphics adapter.m
is another integer variable.initgraph(&d,&m,"c:/turboc3/bgi");
: This initializes the graphics system using theinitgraph()
function. It sets the graphics mode based on the detected adapter and loads the graphics driver from the specified path (in this case,c:/turboc3/bgi
).maxX = getmaxx();
: This gets the maximum x-coordinate of the screen.maxY = getmaxy();
: This gets the maximum y-coordinate of the screen.
-
Drawing the Bars:
- The code enters a nested loop to draw colored bars across the screen:
- The outer loop iterates through the
y
coordinates, dividing the screen into four sections vertically based onsections
. - The inner loop iterates through the
x
coordinates, drawing bars of increasing width across each section. setfillstyle(SOLID_FILL,(colour++)%16);
: This sets the fill style to solid and assigns a color to the bar. Thecolour
variable is incremented after each bar to cycle through the 16 available colors.bar(x,y,x+increment,y+maxY/sections);
: This draws a filled rectangle (bar) with the specified coordinates and height equal to the section height (maxY/sections
).- After drawing each section, the
increment
variable is incremented to gradually increase the width of the bars. Thecolour
variable is reset to 0 to restart the color cycle.
- The outer loop iterates through the
- The code enters a nested loop to draw colored bars across the screen:
-
User Input and Exit:
getch();
: This waits for the user to press any key.closegraph();
: This closes the graphics system and releases the allocated memory.
-
Return:
return 0;
: This indicates a successful termination of the program.
Source code in the c programming language
#include<graphics.h>
#include<conio.h>
#define sections 4
int main()
{
int d=DETECT,m,maxX,maxY,x,y,colour=0,increment=1;
initgraph(&d,&m,"c:/turboc3/bgi");
maxX = getmaxx();
maxY = getmaxy();
for(y=0;y<maxY;y+=maxY/sections)
{
for(x=0;x<maxX;x+=increment)
{
setfillstyle(SOLID_FILL,(colour++)%16);
bar(x,y,x+increment,y+maxY/sections);
}
increment++;
colour = 0;
}
getch();
closegraph();
return 0;
}
You may also check:How to resolve the algorithm Naming conventions step by step in the PicoLisp programming language
You may also check:How to resolve the algorithm Walk a directory/Non-recursively step by step in the Haskell programming language
You may also check:How to resolve the algorithm Vector products step by step in the Scala programming language
You may also check:How to resolve the algorithm Leap year step by step in the GAP programming language
You may also check:How to resolve the algorithm Day of the week step by step in the Ursala programming language