Set Sided Polygon in C++
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10

Thread: Set Sided Polygon in C++

  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 online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,469

    Re: Set Sided Polygon in C++

    What does not work as you expected?
    Victor Nijegorodov

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

    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 03: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,591

    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,614

    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 )

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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center