I am in my 6th week learning C and the teacher gave a new assignment today. We have to write a program that prints geometric shapes (triangle, square, rectangle, circle and trapeze) using the '*' character. I started with the triangle, so let's focus on this.
We have to enter 3 values. The length of the base , the height of the triangle and the distance ( x_dist) of the top angle from the vertical axis that passes through the left angle of the base. (So if this distance is 0, the triangle will be a right triangle). We are given a few guidelines, like, first we have to find the coordinates of the 3 vertices and then find the points that belong on each edge of the triangle. However, we can't do this using double variables because the coordinates of the points represent the j and i on the table char board[61][61], which will then be printed on the console window. For example, if board[10][12] is an angle of the triangle, its value will be set to '*'.
I have calculated the coordinates of the angles, so the shape can be centered on the board, like this
// Point A (left angle)
Ax = (BOARD_LENGTH/2 + 1) - base/2;
Ay = (BOARD_HEIGHT/2 + 1) + height/2;
// Point B (top angle)
Bx = Ax + x_dist; // if x_dist is negative, Bx < Ax
By = Ay - height;
// PointC (right angle)
Cx = Ax + base - 1;
Cy = Ay;
My problem now is how to find the other points on each edge. At first I thought I should create an equation "y = a*x + b" that passes through points A and B and then find the x for each By < y < Ay, but this doesn't work well when I use integers. Then I thought the best course of action would be to find the element of each row of the table that is closer to the triangle's AB edge ** than any other point of that row, but I am unsure of how to do it.
Perhaps this is more of a math problem than a programming one, but I would appreciate any help you can give me. I hope you make sense of what I wrote because English is not my native language.
** Edit: Obviously, just for the other points that belond on the AB edge.