I solve the problem with Data: you need to draw a right triangle using the * symbol and the for loop. How to implement this idea is not yet. Please push on the idea. Code, please do not throw. The triangle must be filled completely.
- 3And what language? - Nicolas Chabanovsky ♦
9 answers
see the Bresenham algorithm for drawing lines, similarly for triangles
An interesting challenge. :) Now there are such tasks on the EGE on computer science in part C.;)
I just combine the already written answers. In the general case, when the triangle occupies a common position (i.e., somehow rotated in space), everything is simple: we determine the tangent of the angle of each side these are the sides), the smallest and largest X, and the smallest and largest Y (that is, we define the square to which our triangle is inscribed). Then we run a cycle from the smallest X to the largest X, and inside it a cycle from the smallest Y to the largest Y. And inside the second we check whether the current point belongs to the given triangle. Those. For the current point (a point, these are X and Y coordinates), the conditions are met: to the right or on the left face, to the left or on the right face, above or on the bottom face. (in general, left / right / bottom is an example ... it depends on how you rotate it.) The principle is important: the point should be inside the triangle drawn by three lines according to the equation y = kx + b. And when checking you substitute your X and Y instead of the corresponding variables into the inequality (since it is necessary that the triangle is not just drawn, but also painted over, then inequalities are necessary) ... And we calculated the coefficient k for each straight line at the beginning ( tangent angle of the sides).
If the triangle occupies a particular position (ie, one of the faces is parallel to one of the axes), then the algorithm is simplified: you need to use only two equations (and the third: Y = const; for example). Those. we also take two cycles, one inside the other, and check whether the point belongs to a triangle or not.
If it is still not clear, then: take a piece of paper, draw a coordinate system, on it is a right-angled triangle and estimate that yes how ... the principle is that we iterate over all points and determine which ones need to be painted and which are not (and your case, put an asterisk).
If a triangle with sides a and b, suppose a is vertically and b horizontally, the increment of the horizontal quantity * changes from 0 to bc in steps of b / a.
- I add that b / a needs to be rounded, to a larger or smaller value. - Alex Silaev
- 2The step does not need to be rounded, as well as the actual amount received, otherwise an error will accumulate. You only need to round up the number of displayed . Either it is necessary to do this, or each time to calculate the number * rounded (x b / a), where x is the number of points passed on the side a - Georgy
- Well, I actually meant it :) - Alex Silaev
If the console output. You can build on how many characters * print in the current line. To do this, we learn the tangent of the desired angle http://www.neive.by.ru/trigonometrija/trfun.html Next, we print the necessary number of characters based on the formula: N-current line multiplied by the tangent of the angle. Example if the sides are A = 4 and B = 4, then the tangent = 1. Then follow the steps:
one@
2 @@
3 @@@
four@@@@
Interesting task =)
I can offer several options. I will write simplified:
- Let there be a variable bounding the current side - the width of the current side. As the current loop iterator will be equal to this variable, we derive the line break, we increase the width (for the next row) and the loop iterator is zeroed. After this verification, we output asterisks =) The cycle will end as soon as the width of the triangle is greater than the limit for the cycle.
- The cycle will be large - the number of iterations as the characters in the square. (n * n) While the remainder of iterator division by width is less than the result of the same division, we draw an asterisk. If the remainder is 0, a line break.
- It is possible with flags by the state machine method =) Infinite for loop, a line drawing flag, a line break flag, a completion flag. And switching them: draw a string, move the string-draw the string -...- complete the loop. Here you can turn around well. If you add trigonometry, then at least three points draw triangles.
If the desired non-isosceles triangles, then either the coefficients, or several stars at a time =) I think there are other options.
This is solved by restricting the passage of the cycle to the left and below the line y = kx + b, if the right angle is at the lower left and two more lines that are the legs of the triangle.
You can also use the matrix n * m, where m and n are the sides of the triangle) Below the main diagonal, including its, will be "*", and above "") just the for loop is involved)) well, this is an option for the word))
Well, draw on paper a few triangles and look.
2 * ** 3 * ** *** 4 * ** *** ****
In the i'y line i asterisks. Well also we do 2 nested loops for - the triangle turns out.
function triangle(n) { var res=""; for (var q=1; q<=n; ++q) { for (var w=1; w<=q; ++w) { res += "*"; } res += "\n"; } return res; }
- "Please do not throw the code." - Pavel Mayorov
var lines = 4; var star, space; for(var curLine = 1; curLine<=lines; curLine++) { space = lines - curLine; star = curLine * 2 - 1; drawLine(space, star, " ", "*"); } function drawLine(cntTabs, cntSymbols, tab, symbol) { var tabs = symbols = ""; for(var i = 0; i<cntTabs; i++) { tabs += tab; } for(var i = 0; i<cntSymbols; i++) { symbols += symbol; } console.log(tabs + symbols); }
- "Please do not throw the code." - Pavel Mayorov