How to resolve the algorithm Greyscale bars/Display step by step in the Python programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Greyscale bars/Display step by step in the Python programming language

Table of Contents

Problem Statement

The task is to display a series of vertical greyscale bars (contrast bars) with a sufficient number of bars to span the entire width of the display. For the top quarter of the display, the left hand bar should be black, and we then incrementally step through six shades of grey until we have a white bar on the right hand side of the display. (This gives a total of 8 bars) For the second quarter down, we start with white and step down through 14 shades of gray, getting darker until we have black on the right hand side of the display. (This gives a total of 16 bars). Halfway down the display, we start with black, and produce 32 bars, ending in white, and for the last quarter, we start with white and step through 62 shades of grey, before finally arriving at black in the bottom right hand corner, producing a total of 64 bars for the bottom quarter.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Greyscale bars/Display step by step in the Python programming language

The given Python 2.7.1 code generates a vertical image of four gray-scaled stripes, each with different widths.

The code begins by importing the livewires module and setting up the graphics window with a black background.

The ty_pruhy function is defined to draw a single stripe. It takes three arguments: an index representing the stripe's position, the stripe's width, and a boolean value indicating whether the stripe should be drawn from left to right.

Within the ty_pruhy function, the stripe's height and lower y-coordinate are calculated. Then, the stripe's width is divided into equal intervals, and a color is assigned to each interval based on the stripe's width. The stripe is drawn using a loop, where each iteration draws a horizontal rectangle with the appropriate color and coordinates.

The main section of the code creates a list of four sublists, each representing one stripe. The first element in each sublist is an index representing the stripe's position, the second element is the stripe's width, and the third element is a boolean value indicating whether the stripe should be drawn from left to right.

The loop in the main section iterates through each stripe in the list, calling the ty_pruhy function to draw the stripe.

Finally, the code enters a loop that waits until the spacebar is pressed to close the graphics window.

Source code in the python programming language

#!/usr/bin/env python
#four gray scaled stripes 8:16:32:64 in Python 2.7.1

from livewires import *

horiz=640; vert=480; pruh=vert/4; dpp=255.0
begin_graphics(width=horiz,height=vert,title="Gray stripes",background=Colour.black)

def ty_pruhy(each):
	hiy=each[0]*pruh; loy=hiy-pruh
	krok=horiz/each[1]; piecol=255.0/(each[1]-1)
	for x in xrange(0,each[1]):
		barva=Colour(piecol*x/dpp,piecol*x/dpp,piecol*x/dpp ); set_colour(barva)
		if each[2]:
			box(x*krok,hiy,x*krok+krok,loy,filled=1)
		else:
			box(horiz-x*krok,hiy,horiz-((x+1)*krok),loy,filled=1)

# main
source=[[4,8,True],[3,16,False],[2,32,True],[1,64,False]]
for each in source:
	ty_pruhy(each)

while keys_pressed() != [' ']: # press spacebar to close window
	pass


  

You may also check:How to resolve the algorithm Modular exponentiation step by step in the Fortran programming language
You may also check:How to resolve the algorithm Narcissistic decimal number step by step in the Oforth programming language
You may also check:How to resolve the algorithm Primality by trial division step by step in the Eiffel programming language
You may also check:How to resolve the algorithm Sorting algorithms/Heapsort step by step in the Wren programming language
You may also check:How to resolve the algorithm Metronome step by step in the F# programming language