Thread: Printing geometrical shapes

  1. #1
    Registered User
    Join Date
    May 2015
    Posts
    2

    Printing geometrical shapes

    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.
    Last edited by kkostas; 05-28-2015 at 02:59 AM. Reason: clarification

  2. #2
    Tweaking master Aslaville's Avatar
    Join Date
    Sep 2012
    Location
    Rogueport
    Posts
    528
    Perhaps this is more of a math problem than a programming one,
    Indeed.

    May be this could be of help Midpoint circle algorithm - Wikipedia, the free encyclopedia

  3. #3
    Registered User
    Join Date
    May 2015
    Posts
    2
    Aslaville, thanks for the suggestion. It will certainly be very helpful when I get to the circle.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A different html shapes question...
    By DrPenguin in forum C Programming
    Replies: 4
    Last Post: 11-02-2014, 04:16 PM
  2. Printing Shapes
    By cpsestudent in forum C Programming
    Replies: 7
    Last Post: 02-16-2011, 05:05 PM
  3. Replies: 11
    Last Post: 10-07-2008, 06:19 PM
  4. Counting shapes
    By Tupcia in forum C++ Programming
    Replies: 1
    Last Post: 09-27-2008, 09:03 PM
  5. Draw Shapes.
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 08-19-2002, 09:22 AM

Tags for this Thread