You are viewing this question as it will appear in the manual grading interface. Return to the normal view when you are done.

Autograder demo: Draw a triangle in C

In this question, we'll assume that there's a graphics library available that will allow us to draw lines on the screen. Such a library is not available with the system that we’re using but similar libraries are available on other systems, so the problems posed here are not unreasonable.

Assume the graphics library function is available to you:

1
2
/* draws a line from (x_start, y_start) to (x_end, y_end) */
void drawLine(int x_start, int y_start, int x_end, int y_end);

Your task is to write a function drawTri that draws right-angled, isosceles triangle on the screen where the parameters are:

  • side_length: the equal length of the two shorter sides,
  • x and y: the (coordinate) location of the upper vertex

Sample output are as follows (in red only):

Sample Output

Function call: drawTri(2, 0, 4);


Function call: drawTri(3, 0, 0);


Function call: drawTri(1, 1, 1);

Note: Your function is not expected to draw the axes and the dashed supporting-lines!

drawTri.c

Correct answer

Click to reveal
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/*
 * Draws a right-angled, isosceles triangle on the screen
 *    where:  (i) the length of the equal/shorter sides
 * (i.e., not the hypotenuse), and (ii) the (x, y) location
 * of the upper vertex are passed as parameters.
 */
void drawTri(int side_length, int x, int y)
{
    /*
     * We know that if we can find the coordinates of the 3 vertices,
     * then we can use the drawLine function to draw the triangle.
     * The coordinates of the first vertex are given as parameters of
     * the function. The coordinates of the other two vertices need to
     * be computed.
     */
    int v1x = x;
    int v1y = y;
    int v2x = v1x;
    int v2y = v1y - side_length;
    int v3x = v2x + side_length;
    int v3y = v2y;

    // call drawLine to draw the triangle on the screen
    drawLine(v1x, v1y, v2x, v2y);
    drawLine(v2x, v2y, v3x, v3y);
    drawLine(v3x, v3y, v1x, v1y);
}

Student view placeholder

In student views this area is used for assessment and score info.
Tools

Staff information

Question

Title:
Autograder demo: Draw a triangle in C

Variant

Started at:
2026-09-19 07:59:25 (CDT)
Duration:
0s
Show/Hide answer
{}
History
dec frac
rad deg