By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,931 Members | 1,565 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,931 IT Pros & Developers. It's quick & easy.

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

P: n/a
mlt
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++.
Sep 2 '08 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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
Sep 2 '08 #2

P: n/a
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?
Sep 2 '08 #3

P: n/a
On Sep 2, 2:18*pm, Erik Wikström <Erik-wikst...@telia.comwrote:
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 '08 #4

P: n/a
In article <48***********************@news.sunsite.dk>, af*@asd.com
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.
Sep 3 '08 #5

P: n/a
mlt

"Jerry Coffin" <jc*****@taeus.comskrev i en meddelelse
news:MP************************@news.sunsite.dk...
In article <48***********************@news.sunsite.dk>, af*@asd.com
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++?
Sep 3 '08 #6

P: n/a
In article <48***********************@news.sunsite.dk>, af*@asd.com
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.
Sep 5 '08 #7

This discussion thread is closed

Replies have been disabled for this discussion.