Drawing triangles

This is a discussion on Drawing triangles within the C++ Programming forums, part of the General Programming Boards category; I know how to draw a square or a rectangle, but what's the code to draw a triangle? Does anyone ...

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    11

    Drawing triangles

    I know how to draw a square or a rectangle, but what's the code to draw a triangle? Does anyone have any idea.

    Lin

  2. #2
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,267
    what language, graphics library, OS, compiler?

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    11
    C++

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    11
    Something that would look like this

    *
    ***
    *****
    ******

    with two variables, base and height...

    thanks, Lin

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    11
    I mean like this...

    Code:
        *
      ***
     ****
    *****
    Sorry

    Lin

  6. #6
    Registered User
    Join Date
    Apr 2002
    Posts
    11
    That's not it either...

    I'm looking for something with the top in the middle of the base and the two sides angling down.

    Lin

  7. #7
    Registered User Aran's Avatar
    Join Date
    Aug 2001
    Posts
    1,301
    possibly something like this

    Code:
    for (int numrows=1; numrows<height; numrows++)
    {
     for (int c=1; c<=base; c++)
     {
      if (c < ((base/2)-(c-1)) || c > ((base/2)+(c-1))
      {
       cout << " ";
      }
      else 
      {
       cout << "*";
      }
     }
    }

  8. #8
    Unregistered
    Guest
    let me guess your using a book by lawrenceville press
    Code:
    #include <iostream.h> 
    
    //------------------------------------------------------------------------------ 
    void Spaces(int spaces) 
    /* Set leading spaces before the '*' */ 
    { 
    	for(int count=0; count<=spaces; count++) 
    	{
    		cout<<" "; 
    	}
    } 
    //------------------------------------------------------------------------------ 
    void DrawBar(int sizeofrow,int spaces) 
    /* Display a bar of '*' of a set increasing lenght */ 
    { 
    	Spaces(spaces); 
    	for(int count=0; count<sizeofrow; count++) 
    	{
    		cout<<"*"; 
    	}
    	cout<<endl;
    } 
    //------------------------------------------------------------------------------ 
    void Isotriangle(int columns) 
    /* Build the triangle and set size of bars */ 
    { 
    	int count=0; 
    	int sizeofrow=1;
    	int stospaces;
    	
    	stospaces=columns;
    
    	while(count!=columns) 
    	{	 
    		count++; 
    		
    		if (count>1) 
    		{ 
    			sizeofrow=sizeofrow+2; 
    		} 
    		DrawBar(sizeofrow, stospaces); 
    		stospaces--;
    	} 
    } 
    //------------------------------------------------------------------------------ 
    int main() 
    
    { 
    	int columns; 
    
    	cout<<"Enter the number of rows you want for your triangle: "; 
    	cin>>columns; 
    
    	Isotriangle(columns); 
    	cout<<endl; 
    
    return (0); 
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Drawing HBITMAP into CWnd, Acquired from "screenshot"
    By DeusAduro in forum Windows Programming
    Replies: 6
    Last Post: 07-02-2009, 03:41 PM
  2. Slow drawing code
    By tjpanda in forum Windows Programming
    Replies: 5
    Last Post: 05-09-2008, 05:09 PM
  3. Drawing triangles in C#
    By John_L in forum C# Programming
    Replies: 2
    Last Post: 03-15-2008, 08:48 AM
  4. How to do double buffing with win32 drawing?
    By Josh Kasten in forum Windows Programming
    Replies: 2
    Last Post: 03-26-2004, 11:02 PM
  5. Need help drawing triangles (C)
    By Belvedeer84 in forum C Programming
    Replies: 6
    Last Post: 10-02-2003, 02:00 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21