CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 20
  1. #1
    Join Date
    Dec 2010
    Posts
    12

    Set Sided Polygon in C++

    Hello basically I've been working on this for 2 days now, I'm completely stuck. I've looked around the internet and can't really find anything in relation.

    I have a program that I've created, It makes shapes. I've managed to make a draw line function & a square function.

    Now it's sort of like a pixel plotter, with X & Y as the view ports.

    I'm struggling to create a polygon with a given number of sides i.e. the user inputs how many sides that want.

    Any help would be much appreciated, also I'm pretty rubbish at triangularly and that kinda stuff, so noobish explanations please!

    This is what I got so far, still hasn't managed to figure out what i need next :-(

    Code:
    // Draw a regular polygon with the given number of sides. The centre of the polygon will be at
    // (X, Y) and the points are on a circle of radius R
    void PixelPlotterForm::DrawPolygon( int Sides, int X, int Y, int R, Color PixelColour )
    {
    	int x = X;
    	int y = Y;
    	int r = R;
    	int sides = Sides;
    
    	float cir = PI * (r * 2);
    
    	float edgelength = cir / sides;
    	float tempsides = 0;
    
    	float angle = 360 / sides;
    
    
    
    	while(tempsides < sides)
    	{
                   //set view port pixel, this acually places the pixel on the program, X, Y and colour    (ignore colour for now, it works)
    		SetViewportPixel(x + r, y + r, PixelColour);
    		r = r + angle;
    		tempsides++;
    	}
    }

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,395

    Re: Set Sided Polygon in C++

    What does not work as you expected?
    Victor Nijegorodov

  3. #3
    Join Date
    May 2002
    Posts
    1,798

    Re: Set Sided Polygon in C++

    I do not understand your algorithm. Perhaps there is something I am missing. It seems to me that if one specifies a polygon with the number of sides greater than or equal to 3, there are an infinite number of possible such polygons. Thus one would need to specify the length of each side and some clever algorithm to determine the angles between each side (such algorithms exist - try google). For more details, see:
    http://www.mathleague.com/help/geome...ns.htm#polygon

    After further thought I realized you must by trying to inscribe a regular polygon ( all sides equal ) on a circle. You will need some algorithm to divide the circumference of the circle into N+1 segments, then draw lines from each segment point to the next going around the circle. The math shouldnt be to difficult. I would be interested in seeing how you solve the problem.

    Hope this is helpful
    Last edited by Mike Pliam; December 22nd, 2010 at 04:37 PM.
    mpliam

  4. #4
    Join Date
    Dec 2010
    Posts
    12

    Re: Set Sided Polygon in C++

    I've almost got it figured, I've got it too do one side of the polygon when a given side is given.

    Basically I'm struggling too create it with an infinite number of sides.

  5. #5
    Join Date
    Dec 2010
    Posts
    12

    Re: Set Sided Polygon in C++

    Anyone have any idea how I would do this, been trying to figure it out for ages!

  6. #6
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: Set Sided Polygon in C++

    Quote Originally Posted by Mike Pliam View Post
    It seems to me that if one specifies a polygon with the number of sides greater than or equal to 3, there are an infinite number of possible such polygons.
    A polygon with less than three sides is an interesting concept... Aside from that: The possibilities of any polygon (number of sides >= 3) are infinite unless you specify some parameters. Even if you postulate a regular polygon they're infinite unless you specify a center and a starting point (e.g.).

    Quote Originally Posted by abakiz View Post
    Basically I'm struggling too create it with an infinite number of sides.
    Hint: A regular polygon with an infinite number of sides is a circle.

    Aside from that: http://en.wikipedia.org/wiki/Polygon
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  7. #7
    Join Date
    Dec 2010
    Posts
    12

    Re: Set Sided Polygon in C++

    I see what your saying, but what sort of algorithm could I do this with?

  8. #8
    Join Date
    Dec 2010
    Posts
    12

    Re: Set Sided Polygon in C++

    What I got so far:

    Code:
    int x = X;
    	int y = Y;
    	//The radious, half of the circle length
    	int r = R;
    	int sides = Sides;
    
    	float cir = PI * (r * 2);
    
    	//basically saying, the edgelength and shave off the bit when you curve it straight, so you get the true length of each side.
    	float edgelength = (cir / sides) - ((cir / sides) / PI);
    
    	//splitting the angle of the sides
    	float angle = cir / sides;
    
    	//you need to rotate or move the line to the next angle / side or something.
    
    
    	float EndX_Y;

  9. #9
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,721

    Re: Set Sided Polygon in C++

    The point on a circle centered on (xc,yc) with radius r are given by

    x = xc + r*cos(angle)
    y = yc + r*sin(angle)

    Code:
    void PixelPlotterForm::DrawPolygon( int Sides, int X, int Y, int R )
    {
       double xc     = X;
       double yc     = Y;
       double radius = R;
    
       double angle_step = 2.0 * PI / static_cast<double>(Sides);
    
       for (int i=0; i<Sides; ++i)
       {
          double angle = angle_step * i;
    
          double x = xc + radius*cos(angle);
          double y = yc + radius*sin(angle);
       }
    }

  10. #10
    Join Date
    Jan 2014
    Posts
    1

    Re: Set Sided Polygon in C++

    abakiz, did you manage to fix this issue? Im working on something similar and as I am new to this struggling with:
    void PixelPlotterForm:rawRectangle( int X, int Y, int Width, int Height, Color PixelColour )

  11. #11
    Join Date
    Oct 2019
    Posts
    5

    Re: Set Sided Polygon in C++

    did you get the whole code? I am stuck with this problem

  12. #12
    Join Date
    Oct 2019
    Posts
    5

    Re: Set Sided Polygon in C++

    could anyone help me with this problem?

  13. #13
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Set Sided Polygon in C++

    Quote Originally Posted by basilmon View Post
    could anyone help me with this problem?
    What is the issue - you don't say??
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  14. #14
    Join Date
    Oct 2019
    Posts
    5

    Re: Set Sided Polygon in C++

    When I run the code I only getting single pixel , not even a side of polygon.

  15. #15
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,395

    Re: Set Sided Polygon in C++

    Quote Originally Posted by basilmon View Post
    When I run the code I only getting single pixel , not even a side of polygon.
    And what is the code you run?
    Could you post it?
    Victor Nijegorodov

Page 1 of 2 12 LastLast

Tags for this Thread

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