CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    May 2014
    Posts
    72

    Making an octagon out of right triangles

    Hello all,

    One exercise says that, "Define a right triangle class. Make an octagonal shape out of eight right triangles of different colors."

    Making such a class isn't difficult. I wrote it as follows:

    Code:
    #include "Simple_window.h"
    
    class right_triangle : public Shape {
    public:
    	
    	right_triangle(Point p, int l, int sh): c(p), _long(l), _short(sh) 
    	 { add(Point (p));}
    
    	void draw_lines () const
    	{
    		fl_line(point(0).x, point(0).y, point(0).x, point(0).y+_long, point(0).x+_short, point(0).y+_long);
    		fl_line(point(0).x+_short, point(0).y+_long, point(0).x, point(0).y);
    	}
    
    private:
    	Point c;
    	int _long, _short;
    };
    
    //*********************************
    
    int main()
    {
        Simple_window win(Point(100,100), 700,500, "Octagon"); 
    
    	Point p(200,200);
    	int l = 80, sh = 60;
    	right_triangle rt(p,l,sh);
    
    	win.attach(rt);
    	win.wait_for_button();
    }
    But it sounds that making an octagon using eight right triangles isn't possible!
    What's your thoughts please?

  2. #2
    Join Date
    Jul 2013
    Posts
    576

    Re: Making an octagon out of right triangles

    Quote Originally Posted by abbassi View Post
    But it sounds that making an octagon using eight right triangles isn't possible!
    What's your thoughts please?
    It's easier with a picture but I'll try in words.

    To get an octagon you inscribe a circle with 8 isosceles triangles. The angle between the equal sides of the triangles will be 45 degrees (8 time 45 degrees is 360 which is a full circle).

    Now you inscribe two right triangles in each isosceles triangle. This splits the isosceles triangles in the middle giving them one 90 degree angle each making them right triangles.

    So all in all you need 16 right triangles to build an octagon.
    Last edited by razzle; June 28th, 2014 at 04:02 AM.

  3. #3
    Join Date
    May 2014
    Posts
    72

    Re: Making an octagon out of right triangles

    Yes, that's right. Thank you.

  4. #4
    Join Date
    Jul 2013
    Posts
    576

    Re: Making an octagon out of right triangles

    Quote Originally Posted by abbassi View Post
    Yes, that's right. Thank you.
    You're wellcome.

    I know it's an exercise but note that representing an octagon with 16 right triangles is very inefficient. All it takes are two points, the centre point and one perimeter corner point. The rest can be deducted from that.

  5. #5
    Join Date
    Jun 2002
    Location
    Stockholm, Sweden
    Posts
    1,641

    Re: Making an octagon out of right triangles

    You don't need 16 triangles if it is okay to overlap them. Use two triangles to make a rectangle that touches two opposite sides of the octaton.
    Make another such rectangle rotated 45 degrees to form the next pair of sides of the octagon. Repeat the procedure again two times...
    Nobody cares how it works as long as it works

  6. #6
    Join Date
    Jul 2013
    Posts
    576

    Re: Making an octagon out of right triangles

    ---
    Last edited by razzle; June 30th, 2014 at 08:03 AM.

  7. #7
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: Making an octagon out of right triangles

    if the octagon is symettric, then you can do it with as few as 10 right triangles of 3 different sizes.
    - take any 2 opposing edges, and connect them to make a rectangle. split the rectangle diagonally, these are the first 2 triangles
    - both sides of the rectangle now have a trapezoid. Cut of the 2 "pointy sides" (2 more triangles on each of the 2 trapezoids = 4)
    - split the remaining 2 rectangles diagonally to make 2 more for each (totalling 4 more)

    There might be a way to do any convex octagon with fewer than 10 right triangles, but I'm not really seeing how.

    a concave octagon can be done with as few as 4 right triangles. just make 2 rectangles (split diagonally) then put one short end of the rectangle next to the long end of the other.
    Code:
     +-----+
     |\    |
     | \   |+-+
     |  \  ||/|
     |   \ |+-+
     |    \|
     +-----+
    the outer shape is a concave octagon

  8. #8
    Join Date
    Jun 2014
    Posts
    6

    Re: Making an octagon out of right triangles

    Crudely drawn but this is how you can do it with 8 right angle triangles.
    Name:  triangles.png
Views: 1027
Size:  2.3 KB

  9. #9
    Join Date
    Jul 2013
    Posts
    576

    Re: Making an octagon out of right triangles

    Quote Originally Posted by David-I View Post
    Crudely drawn but this is how you can do it with 8 right angle triangles.
    I'm pretty certain this doesn't work with a regular octagon (fully symmetric and equally long sides L). I suspect your solution works only if the diagonal sides in your picture have length sgrt(2)*L and the other sides length L. When all sides are L your solution will produce two non-right triangles. They must be split to be become right triangles. So to me it looks like 10 is the true minimum number of right triangles to cover a regular octagon but I may be wrong.

    By all means if you have a solution I would be very interested. On the other hand I don't think the OP should worry much about it. People shouldn't be required to struggle with irrelevant secondary topics when the primary task is to learn how to program C++.
    Last edited by razzle; July 1st, 2014 at 12:28 AM.

  10. #10
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: Making an octagon out of right triangles

    interesting approach

    but yes, it only works for a very specific type of non-symmetric octagon.
    Using the orientation from #8, if the length of the edges of the vertal left/rightmost edge is X,
    then the length of the diagonal outer edges will be
    Code:
     _____
    V 2X²

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured