Build functions in the console. Part 2 (Graph)

3r33333. 3r3-31. Getting started r3r368. 3r33333. 3r33333. 3r33333. 3r33333. 3r33333. 3r33333. 3r33333. 3r33333. Last time I stopped at the construction of a table of function values. It is time to go to the construction of the graph itself, for which all this, in fact, began. 3r33333. 3r33333. 3r33333. 3r33333. So, the basic idea is as follows. Rotate the coordinate axis 90 degrees clockwise. This is necessary in order to simplify the construction, without storing data about each point in any sheet. 3r33333. 3r33333. 3r33333. 3r33333. Next, we restrict the coordinate axis to 82 characters for better readability of the graphics. It is clear that at the same time we lose accuracy and the graph will be more schematic (too compressed), especially for “cool” functions, but still. 3r33333. 3r33333. 3r33333. 3r33333. After that, we calculate the position of the x axis relative to the axis of the game, that is, we are looking for where we will have a point (x, 0). Well, and then line by line we will assign the value of the function y1 to x at this point. 3r33333. 3r33333. 3r33333. 3r33333. Let's go

3r33333. 3r3331. 3r33333. 3r33333. To begin with, we need the following formula:

3r33333.

3r3-3160.

3r3-3160. 3r33333. 3r33333. 3r33333. 3r33333. We will assign 80 positions to the graphics, the two remaining places will be purely decorative. With this formula, we find which range of values corresponds to one position in our graph. Then we can correctly mark the current point on it. 3r33333. 3r33333. 3r33333. 3r33333. The main ideas are marked, so let's move on to code 3r33353 itself. 3r33333.` dial_length = 12`

label = "Graph of the function y1 = x ** 3 - 2 * x ** 2 + 4 * x - 8"

print ("{1:> {0}}". format (len (label) + dial_length, label), 'n')

print ("{aux[1]:> {aux[0]}} n {aux[2]:> {aux[0]}}> n" .format (aux =

[dial_length + 82, 'y' , 82*'-'])); 3r33333. 3r33333. 3r33333. 3r33333. 3r33333. 3r33333. 3r33333. Including thanks to the comments to the first part, I learned about such a thing as 3r3673. format

. It seemed to me really more comfortable than my dances with a tambourine, so a huge piece of code with the calculation of indents turned into a couple of lines 3r33333. 3r33333. 3r33333. 3r33333.` print ("{1:> {0}}". format (len (label) + dial_length, label), 'n')`

3r33333. 3r33333. 3r33333. 3r33333. 3r33333. 3r33333. The unit is responsible for the number of the element passed as an argument to the format function, that is, it is a “link” (not literally) to the label variable, which we actually display on the screen. The numbering is exactly the same as in the sheets - from scratch. 3r33333. 3r33333. A pair of characters 3r34040. :>[/b] used to align the displayed text on the right side. Well, ** {0} 3r33241. after symbol 3r3240. >** needed to determine the number of line positions that you need. 3r33333. 3r33333. ** That is, in this case, we reserve for the label line len (label) + dial_length positions, and the label itself occupies only len (label), and aligns the text on the right side within the collection of these positions. 3r33333. 3r33333. 3r33333. 3r33333. print ("{1:> {0}}". format (len (label) + dial_length, label), 'n')print (dial_length * '' + label, 'n')3r33333. 3r33333. 3r33333. 3r33333. 3r3105. these lines are equivalent to [/i] 3r33333. 3r33333. **

` 3r33333. 3r33333. 3r33333. 3r33333. Yes, for lines, it is probably easier to use the second option, but applying the first one for general development does not hurt) 3r333335. 3r33333. 3r33333. 3r33333.`

` print ("{aux[1]:> {aux[0]}} n {aux[2]:> {aux[0]}}> n" .format (aux =`

[dial_length + 82, 'y' , 82*'-'])); 3r33333. 3r33333. 3r33333. 3r33333. 3r33333. In format, you can even push arrays of the r_value type (in C ++), that is, they were created directly by passing the argument 3r33353. 3r33333. 3r33333. 3r33333. We fix variables that are constant, that is, they do not depend on the current value of the function. 3r33333. 3r33333. In python there is no conditional ** const ** for designation of constants, therefore it is accepted to call such variables in capital letters and simply not to change them. 3r33333. 3r33333.

` MAX_Y1_VALUE_DIFFERENCE = (max_y1_value - min_y1_value) +`

(max_y1_value == min_y1_value)

RATIO = MAX_Y1_VALUE_DIFFERENCE /80

AXIS_X_POS = abs (int ((- min_y1_value) /RATIO))

if (AXIS_X_POS> 80):

AXIS_X_POS = 81

3r33333. 3r33333. 3r33333. 3r33333. 3r33333. 3r33333. Since, for obvious reasons, RATIO cannot be equal to ? MAX_Y1_VALUE_DIFFERENCE must be a positive number. It is for this purpose that there is a second term on the right side of the assignment. 3r33333. 3r33333. We calculate the position of the X axis by the formula 3r33333. 3r33333.

3r3-3160.

3r3158.

3r3-3160. 3r33333. 3r33333. 3r33333. 3r33333. Where does this formula come from? We simply calculate the ratio of the segments (on the axis of the game) from the beginning of the axis (min (y1)) to the current value of the function (y1 (x)) and the segment from the beginning of the axis to its end (max (y1)). Well, we multiply this ratio by 80 in order to find such a distance from the beginning of the axis to the current value in the spaces (therefore, only whole numbers can be used) that reflect the formula-ratio on the graph. 3r33333. 3r33333. Since we are interested in the position when y1 (x) = ? then introduced into the formula required values and voila. 3r33333. 3r33333. 3r33333. 3r33333. Now you can go directly to printing values

3r33333.

` while (is_sequence_decreasing and from_x> = to_x) or`

. (Not is_sequence_decreasing and from_x <= to_x):

y1_cur_value = y1 (from_x)

cur_y1_value_and_min_difference = (y1_cur_value - Min_y1_value) +

(y1_cur_value == min_y1_value) *

((max_y1_value == min_y1_value))

pos_of_y = int ( cur_y1_value_and_min_difference * 80 /

MAX_Y1_VALUE_DIFFERENCE)

3r33333. 3rr3332. 3r3353. 3r33333. 3r33333. 3r33333. The cycle is already familiar to us. It is necessary to count each function value a second time in order not to store them in a sheet or something else. 3r33333. 3r33333. The position of the game is calculated by the above formula. 3r33333. 3r33333. We will have to fix the upper difference from the formula, considering the case when the formula will result in the uncertainty of the form zero to zero. If such uncertainty arises, it will mean that the current value of y1 (x) = max (y1), which means the current value of the game will coincide with the end of the y axis. 3r33333. 3r33333. 3r33333. 3r33333.

` print ("{1: ^ {0} .6g}". format (dial_length, from_x), end = '')`

3r33333. if (negative_value_exists):

if y1_cur_value <= 0 - RATIO /2:

req_aux = AXIS_X_POS - pos_of_y

if (req_aux! = 0): 3r3333366. print (pos_of_y * '' + '*' + (req_aux - 1) * '' + '|')

else:

print ((AXIS_X_POS - 1) * '' + '*' + '|')

elif y1_cur_value> = 0 + RATIO /2:

req_aux = pos_of_y - AXIS_X_POS

if (req_aux! = 0): 3r3333366. print (AXIS_X_POS * '' + '|' + (req_aux - 1) * '' + '*')

else:

print ((AXIS_X_POS) * '' + '| *')

else:

print (AXIS_X_POS * '' + '*')

else:

print ('|' + pos_of_y * '' + '*')

AXIS_X_POS = 0

from_x + = pace_x

print ((dial_length + AXIS_X_POS) * '' + '| n',

(dial_length + AXIS_X_POS - 3) * '' + 'x V')

3r33333. 3r33333. 3r33333. 3r33333. 3r33333. 3r33333. This part of the code is directly responsible for printing the

graphic itself. 3r33333.

` print ("{1: ^ {0} .6g}". format (dial_length, from_x), end = '')`

3r33333. 3r33333. 3r33333. 3r33333. Here, format was very useful and simplified the code. ** ^ ** allows us to align the number in the center of the selected area (in this case, 12 positions). ** g ** is responsible for the numbers - if they do not have the fractional part, then it will not be printed (the number is like int), otherwise - 6 decimal places

3r33333. 3r33333. 3r33333. Since our graph is limited to a space of 80 characters along the y axis, on our graph the value of the function at the point will coincide with the x axis not only in the case of y1 (x) = ? but also in the vicinity of[0 — RATIO/2, 0 + RATIO/2]. 3r33333. 3r33333. In total, we have three cases of the location of the asterisk (that is, the point) and the vertical stick (that is, the x axis): '* |' (y1 (x) <= 0 — RATIO/2), '*' (0 — RATIO/2 < y1(x) < 0 + RATIO/2), '|*' (y1(x) > = 0 + RATIO /2), we will consider these three cases. 3r33333. 3r33333.

3r33333.

y1 (x) <= 0 — RATIO/2

3r33333. In this case, the point is located to the x axis, so we are looking for the distance from the point to the axis in spaces. Due to the rounding of numbers, it may happen that the values of the variables AXIS_X_POS and pos_of_y may coincide. But this can not be, because in this case we would fall into the third case. In our case, the point does not coincide with the x axis, so an additional condition is necessary, which will decrease by one the variable pos_of_y in the case of equality.

3r33333.

y (x)> = 0 + RATIO /2

3r33333. The case is identical to the first case, only the point will be located on the other side of the x-axis and all the above actions for this are corrected to 3r-?666. 3r33333.

the rest is 3r33353. 3r33333. The easiest case is to simply type an asterisk in place of the

axis. 3r33333.

3r33333. 3r33333. 3r33333. 3r33333. This is if we have negative values (y1 (x) < 0). Если нет, то просто печатаем '|' и определяем позицию точки.

3r33333. Well, we complete the program by drawing the x axis. 3r33333. 3r33333. 3r33333. 3r33333. So, the code that eventually turned out:

3r33333.

` dial_length = 12`

label = "Graph of the function y1 = x ** 3 - 2 * x ** 2 + 4 * x - 8"

print ("{1: ^ {0} .6f}". format (dial_length, x_copy))

3r33333. print ("{1:> {0}}". format (len (label) + dial_length, label), 'n')

print ("{aux[1]:> {aux[0]}} n {aux[2]:> {aux[0]}}> n" .format (aux =

[dial_length + 81, 'y' , 82*'-']), end = ''); 3r33333. 3r33333. MAX_Y1_VALUE_DIFFERENCE = (max_y1_value - min_y1_value) +

(max_y1_value == min_y1_value)

RATIO = MAX_Y1_VALUE_DIFFERENCE /80

AXIS_X_POS = abs (int ((- min_y1_value) /RATIO))

if (AXIS_X_POS> 80):

AXIS_X_POS = 81

3r33333. while (is_sequence_decreasing and from_x> = to_x) or

. (Not is_sequence_decreasing and from_x <= to_x):

y1_cur_value = y1 (from_x)

cur_y1_value_and_min_difference = (y1_cur_value - Min_y1_value) +

(y1_cur_value == min_y1_value) *

((max_y1_value == min_y1_value))

pos_of_y = int ( cur_y1_value_and_min_difference * 80 /

MAX_Y1_VALUE_DIFFERENCE)

print ("{1: ^ {0} .6g}". format (dial_length, from_x), end = ")

a. if y1_cur_value <= 0 - RATIO /2:

req_aux = AXIS_X_POS - pos_of_y

if (req_aux! = 0):

print (pos_of_y * '' + '*' + (req_aux - 1) * '' + '|')

r.a. . print ((AXIS_X_POS - 1) * '' + '*' + '|')

elif y1_cur_value> = 0 + RATIO /2:

req_aux = pos_of_y - AXIS_X_POS

if (req_aux! = 0): 3r3333366. print (AXIS_X_POS * '' + '|' + (req_aux - 1) * '' + '*')

else:

print ((AXIS_X_POS) * '' + '| *')

else:

print (AXIS_X_POS * '' + '*')

else:

print ('|' + pos_of_y * '' + '*')

AXIS_X_POS = 0

from_x + = pace_x

print ((dial_length + AXIS_X_POS) * '' + '| n',

(dial_length + AXIS_X_POS - 3) * '' + 'x V')

3r33333. 3r33333. 3r33333. 3r33333. 3r33333. 3r33333. Run the program on several tests 3r33353. 3r33333. 3r33333. 3r33333. 3r33333. 3r33333. 3r33333. 3r33333. 3r33333. 3r33333. 3r33333. 3r33333. 3r33333. 3r33333. It works) 3r33353. 3r33333. 3r33333. 3r33333. 3r33333. 3r33333.

3r33333. 3r33333. 3r33333. 3r33333. 3r33333. 3r33333.

It may be interesting

#### weber

Author**16-10-2018, 00:23**

Publication Date
#### Mathematics / Python / Graphical shells

Category- Comments: 0
- Views: 311

With deadlines getting tighter and closer together, and the constant onslaught of assignment after assignment, sometimes, it can feel like life is passing you by. That the world is moving on without you. You might find yourself sitting at your desk night after night trying to keep up with your workload, and wondering when you will ever have a life outside of your dorm room again.