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

M

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++.
 
E

Erik Wikström

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.
 
J

Juha Nieminen

mlt said:
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?
 
J

Jerry Coffin

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.
 
M

mlt

Jerry Coffin said:
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++?
 
S

Sleipnir

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 !!!!!!!!!
 
J

Jerry Coffin

[ ... ]
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
}
}
}
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top