Drawing to code 5 point star function C++

This is a discussion on Drawing to code 5 point star function C++ within the C++ Programming forums, part of the General Programming Boards category; I'm trying to code a 5 point star function in C++, I am using the WinBGIm library. I am getting ...

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    1

    Drawing to code 5 point star function C++

    I'm trying to code a 5 point star function in C++, I am using the WinBGIm library. I am getting stuck on the trigonometry of the triangle. My function is given the centre x and y coordinates and the radius of the star all as integers.

    This is what I have s ofar.

    Code:
    // The main purpose of this program is to draw 7 stars
    // Using the WinBGIm graphics library.
    
    // Directives:
    #include <graphics.h>
    #include <cstdlib>
    #include <cmath>
    using namespace std;
    
    //Constants
    
    const int S = 700;
    
    // First Star(test)
    const int STARX1 = 0.2*S;
    const int STARY1 = 0.5*S;
    const int RADIUS1 = 0.07*S;
    
    
    //-----------------------------------------------------------------------------
    
    //-----------------------------------------------------------------------------
    // Function Prototypes.
    // This is a list of all the functions that I plan to write because the
    // author of the graphics library did not have them!
    void star (int cx, int cy, int radius);
    //-----------------------------------------------------------------------------
    
    //-----------------------------------------------------------------------------
    // Main program
    
    int main()
    {
        initwindow(S,S,"Dipper!");
        star(STARX1, STARY1, RADIUS1);
        delay(200000);
    
    }
    
    //-----------------------------------------------------------------------------
    
    //-----------------------------------------------------------------------------
    // This function draws a star. The center of the star is at (cx, cy),
    // and the radius of the star is given by radius.
    
    void star(int cx, int cy, int radius)
    {
        
        int farrightxcoord = cx + radius*cos(M_PI/10);
        int farrightycoord = cy - radius*sin(M_PI/10);
        int bottomrightxcoord = cx+ radius*cos(5*M_PI/10);
        int bottomrightycoord= cy - radius*sin(5*M_PI/10);
        int bottomleftxcoord = cx+radius*cos(9*M_PI/10);
        int bottomleftycoord=cx-radius*sin(9*M_PI/10);
        int farleftxcoord    = cx + radius*cos(13*M_PI/10);
        int farleftycoord    = cy - radius*sin(13*M_PI/10);
        int topxcoord = cx + radius*cos(17*M_PI/10);
        int topycoord = cy - radius*sin(17*M_PI/10);
        line(farrightxcoord,farrightycoord, farleftxcoord, farleftycoord);
        line(farrightxcoord,farrightycoord, bottomleftxcoord,bottomleftycoord);
        line(bottomleftxcoord,bottomleftycoord,topxcoord,topycoord);
        line(topxcoord,topycoord,bottomrightxcoord,bottomrightycoord);
        line(bottomrightxcoord,bottomrightycoord, farleftxcoord, farleftycoord);
    As you can see the star function is where I am really confused, can someone help me with this?
    Last edited by YellowJello; 01-18-2010 at 05:12 PM.

  2. #2
    Registered User
    Join Date
    Jan 2010
    Posts
    1
    this'll work -

    line(x, y - starsize,
    x + starsize*sin(M_PI/5), y + starsize*cos(M_PI/5));
    line(x + starsize*sin(M_PI/5), y + starsize*cos(M_PI/5),
    x - starsize*cos(M_PI/10), y - starsize*sin(M_PI/10));
    line(x - starsize*cos(M_PI/10), y - starsize*sin(M_PI/10),
    x + starsize*cos(M_PI/10), y - starsize*sin(M_PI/10));
    line(x + starsize*cos(M_PI/10), y - starsize*sin(M_PI/10),
    x - starsize*sin(M_PI/5), y + starsize*cos(M_PI/5));
    line(x - starsize*sin(M_PI/5), y + starsize*cos(M_PI/5),
    x, y - starsize);

    peace

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 12:49 PM
  2. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  3. C++ compilation issues
    By Rupan in forum C++ Programming
    Replies: 1
    Last Post: 08-22-2005, 05:45 AM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM

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