CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Apr 2001
    Posts
    1,029

    how to calculate the points of a pentagon given the center?

    Hello,

    I have one XY Point that should be the center of a pentagon.
    I want the length of each side to be 20.
    This is a Cartesian Coordinate System.
    I also want the tip on the pentagon to be facing South. So basically I want to draw an upside-down homeplate with the XY point the center. How can I calculate the points?


    Thanks!

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: how to calculate the points of a pentagon given the center?

    Think of the points are being on a circle, separated by angles of 2pi/5. You know the position of one point is straight down, which you can consider a zero angle (although more typically zero angle is the positive x axis), so you should be able to compute the angles for the other points from that. Once you have an angle and a distance you can convert those polar coordinates to Cartesian coordinates using the standard equations.

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

    Re: how to calculate the points of a pentagon given the center?

    I had a few minutes and decided I needed the programming exercise, so...
    Code:
    #include <iostream>
    #include <vector>
    #include <cmath>
    
    using namespace std;
     
    const double PI = 4 * atan(1.0);
    
    inline double RadiansToDegrees(double radians) { return ( 360 * radians ) / ( 2 * PI ); }
    inline double DegreesToRadians(double degrees) { return ( 2 * PI * degrees ) / 360; }
    
    class Pentagon
    {
    	protected:
    		double xCenter;
    		double yCenter;
    		double radiansOffset;
    		double lengthSide;
    		vector<double> x;
    		vector<double> y;
    
    	public:
    		Pentagon()
    			: xCenter(0), yCenter(0), radiansOffset(0), lengthSide(1), x(5), y(5)
    		{
    			CreatePoints();
    		}
    
    		Pentagon(double xCenter, double yCenter, double radiansOffset, double lengthSide)
    			: xCenter(xCenter), yCenter(yCenter), radiansOffset(radiansOffset), lengthSide(lengthSide), x(5), y(5)
    		{
    			CreatePoints();
    		}
    
    		~Pentagon()
    		{
    		}
    
    		void CreatePoints()
    		{
    			const double radius = Radius();
    
    			for( int i=0; i<5; ++i )
    			{
    				const double angle = radiansOffset + i * 2*PI/5;
    				x[i] = xCenter + radius * cos(angle);
    				y[i] = yCenter + radius * sin(angle);
    			}
    		}
    
    		double Radius() const { return lengthSide / ( 2.0 * sin(2*PI/10) ); }
    		double GetX(int index) const { return index>=0 && index<x.size() ? x[index] : 0; }
    		double GetY(int index) const { return index>=0 && index<y.size() ? y[index] : 0; }
    };
    
    int main()
    {
    	const double xCenter = 0;
    	const double yCenter = 0;
    	const double radiansOffset = DegreesToRadians(270);
    	const double lengthSide = 20;
    
    	const Pentagon p(xCenter, yCenter, radiansOffset, lengthSide);
    
    	for( int i=0; i<5; ++i )
    		cout << "[" << i << "] " << "x=" << p.GetX(i) << ", y=" << p.GetY(i) << endl;
    
      return 0;
    }

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