plot a 3D sphere from C++ using gnuplot?

Discussion in 'C++' started by mlt, Sep 2, 2008.

  1. mlt

    mlt Guest

    Anyone has an idea to generate points for a solid sphere in C++ that can be
    plotted with gnuplot? I have tried to plot simple functions based on output
    from a function written in C++ which worked fine, but am not sure how to
    deal with solid object like a sphere from C++.
     
    mlt, Sep 2, 2008
    #1
    1. Advertising

  2. On 2008-09-02 19:46, mlt wrote:
    > Anyone has an idea to generate points for a solid sphere in C++ that can be
    > plotted with gnuplot? I have tried to plot simple functions based on output
    > from a function written in C++ which worked fine, but am not sure how to
    > deal with solid object like a sphere from C++.


    Just like you did with the function, you just have to add the third
    dimension in the output, and figure out the correct gnuplot format.

    --
    Erik Wikström
     
    Erik Wikström, Sep 2, 2008
    #2
    1. Advertising

  3. mlt wrote:
    > Anyone has an idea to generate points for a solid sphere in C++ that can be
    > plotted with gnuplot? I have tried to plot simple functions based on output
    > from a function written in C++ which worked fine, but am not sure how to
    > deal with solid object like a sphere from C++.


    Isn't this more of a gnuplot question than a C++ question?
     
    Juha Nieminen, Sep 2, 2008
    #3
  4. mlt

    Guest

    On Sep 2, 2:18 pm, Erik Wikström <> wrote:
    > On 2008-09-02 19:46, mlt wrote:
    >
    > > Anyone has an idea to generate points for a solid sphere in C++ that can  be
    > > plotted with gnuplot? I have tried to plot simple functions based on output
    > > from a function written in C++ which worked fine, but am not sure how to
    > > deal with solid object like a sphere from C++.

    >
    > Just like you did with the function, you just have to add the third
    > dimension in the output, and figure out the correct gnuplot format.
    >
    > --
    > Erik Wikström


    hi
     
    , Sep 2, 2008
    #4
  5. mlt

    Jerry Coffin Guest

    In article <48bd7bfb$0$90275$>,
    says...
    > Anyone has an idea to generate points for a solid sphere in C++ that can be
    > plotted with gnuplot? I have tried to plot simple functions based on output
    > from a function written in C++ which worked fine, but am not sure how to
    > deal with solid object like a sphere from C++.


    Generating the points isn't very hard, but by themselves, those will be
    pretty useless -- if you just plot the points, there won't be a visible
    difference between a sphere and a flat circle.

    If you want to generate something recognizably sphere-like in gnuplot,
    it's probably easier (and faster) to let gnuplot generate the points
    itself anyway, something like this:

    set nokey
    set parametric
    set hidden3d
    set view 60
    set isosamples 40, 30
    set xrange[-2 : 2]
    set yrange[-2 : 2]
    set zrange[-1 : 1]
    splot [-pi:pi][-pi/2:pi/2] cos(u)*cos(v), sin(u)*cos(v), sin(v)

    This will give a wireframe model. If you _really_ want something that
    looks like a solid surface, you'll need to do define lights and the
    characteristics of the surface being modeled, then calculate the color
    for each point on the surface. My immediate guess is that gnuplot won't
    be of much help for this job -- you'd be much better off using something
    like OpenGL that supports such things directly.

    --
    Later,
    Jerry.

    The universe is a figment of its own imagination.
     
    Jerry Coffin, Sep 3, 2008
    #5
  6. mlt

    mlt Guest

    "Jerry Coffin" <> skrev i en meddelelse
    news:...
    > In article <48bd7bfb$0$90275$>,
    > says...
    >> Anyone has an idea to generate points for a solid sphere in C++ that can
    >> be
    >> plotted with gnuplot? I have tried to plot simple functions based on
    >> output
    >> from a function written in C++ which worked fine, but am not sure how to
    >> deal with solid object like a sphere from C++.

    >
    > Generating the points isn't very hard, but by themselves, those will be
    > pretty useless -- if you just plot the points, there won't be a visible
    > difference between a sphere and a flat circle.
    >
    > If you want to generate something recognizably sphere-like in gnuplot,
    > it's probably easier (and faster) to let gnuplot generate the points
    > itself anyway, something like this:
    >
    > set nokey
    > set parametric
    > set hidden3d
    > set view 60
    > set isosamples 40, 30
    > set xrange[-2 : 2]
    > set yrange[-2 : 2]
    > set zrange[-1 : 1]
    > splot [-pi:pi][-pi/2:pi/2] cos(u)*cos(v), sin(u)*cos(v), sin(v)
    >
    > This will give a wireframe model. If you _really_ want something that
    > looks like a solid surface, you'll need to do define lights and the
    > characteristics of the surface being modeled, then calculate the color
    > for each point on the surface. My immediate guess is that gnuplot won't
    > be of much help for this job -- you'd be much better off using something
    > like OpenGL that supports such things directly.
    >

    '
    Ok but how would you generate the data for a wireframe model of a sphere
    from C++?
     
    mlt, Sep 3, 2008
    #6
  7. mlt

    Sleipnir Guest

    just do a stuff as

    h=1/(2*npts_per_line)

    for (i = -1.0; i<=1; i+=h)
    for (j = -1.0; j<=1; j+=h)
    for (k = -1.0; k<=1; k+=h)
    {
    tmp = i*i+j*j+k*k;
    if (tmp<=1)
    file << i << '\t' << j << '\t' << k << '\t' << sqrt(tmp) << '\n';
    }


    TADAAAAAA !!!!!!!!!


    "mlt" <> a écrit dans le message de
    news:48bd7bfb$0$90275$...
    > Anyone has an idea to generate points for a solid sphere in C++ that can
    > be plotted with gnuplot? I have tried to plot simple functions based on
    > output from a function written in C++ which worked fine, but am not sure
    > how to deal with solid object like a sphere from C++.
    >
     
    Sleipnir, Sep 3, 2008
    #7
  8. mlt

    Jerry Coffin Guest

    In article <48be9749$0$90275$>,
    says...

    [ ... ]

    > Ok but how would you generate the data for a wireframe model of a sphere
    > from C++?


    It depends on the exact sort of wire-frame you want. Obvious choices are
    latitude-like lines, longitude-like lines, or both. Here's a bit of code
    to generate some points that approximate a wire-frame (i.e. the points
    are close together, but not really connected).

    double d2r(double degrees) {
    const double conversion = 3.1416f/180.0f;
    return degrees * conversion;
    }

    void Sphere(double radius) {
    for (int latitude=-90; latitude<90; latitude++) {
    double current_radius = cos(d2r(latitude)) * radius;
    double z = sin(d2r(latitude)) * radius;

    // Every 10 degrees of latitude, draw a longitude line.
    // Otherwise, draw a point every 10 degrees of longitude.
    int increment = latitude % 10 ? 10 : 1;

    for (int longitude=0; longitude<360; longitude+=increment) {
    double x = cos(d2r(longitude))*current_radius;
    double y = sin(d2r(longitude))*current_radius;
    // (x,y,z) is a point in the wireframe
    }
    }
    }



    --
    Later,
    Jerry.

    The universe is a figment of its own imagination.
     
    Jerry Coffin, Sep 5, 2008
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Denebola

    Rotating sphere

    Denebola, Jan 19, 2006, in forum: HTML
    Replies:
    8
    Views:
    6,861
    dorayme
    Jan 21, 2006
  2. Ron Natalie

    Random numbers within a sphere?

    Ron Natalie, Feb 8, 2005, in forum: C++
    Replies:
    25
    Views:
    7,802
    Richard.Ayling
    Oct 23, 2007
  3. ¶}¾Ç¤F¶Ü?

    plot two data sets on gnuplot

    ¶}¾Ç¤F¶Ü?, May 10, 2004, in forum: Python
    Replies:
    2
    Views:
    980
    Fernando Perez
    May 15, 2004
  4. Dr. Colombes
    Replies:
    1
    Views:
    1,055
    John Hunter
    Feb 10, 2005
  5. Armin Armbruster
    Replies:
    0
    Views:
    239
    Armin Armbruster
    Nov 11, 2008
Loading...

Share This Page