Writing Code for Surface Plots? 81
MySchizoBuddy asks: "In what ways can you code plots of surface charts using a z(x,y) function or a cloud of points? I'm looking for a tutorial that explains this that doesn't use DirectX or OpenGL libraries (the language I'm using cannot use either framework anyway). How is the 3D mess generated and how can the 2D contour plots be generated as well? I'm assuming once I know that I can also use it to make torus plots as well. Remember, I'm asking for the explanation of the underlying math and an example code that does that. The GNUPlot gallery has some examples that I find helpful, but are there similar examples out there? (Remember, I am writing the plotting code as well)? Can anyone help or point me in the right direction?"
first plot (Score:2, Funny)
hum
Graph3 (Score:2, Informative)
http://www.detachedsolutions.com/graph3/ [detachedsolutions.com]
This is software that does what you need, written for the TI-83 (should work fine on an 84 too). Since it's open source, you can peek at the source code to get an idea of how its 3d rendering works.
Unfortunately, most TI calculator apps are coded in assembly...
Re:I just hacked something up myself (Score:2)
Another view (Score:4, Interesting)
Looky here [navarro.mus.br], here [navarro.mus.br] and here [navarro.mus.br].
Re:Another view (Score:2)
I actually didn't even use the mod function in my code. I produced the images in PPM format, and color 257 = color 1 in that format. It was an accidental "invention". (I wonder if it's been done properly before me).
Re:Another view (Score:1, Insightful)
Okay, so I don't mean to be that harsh - we still need people like you with that sort of talent
Re:Another view (Score:2)
Pff. According to the U.S. courts, the VFAT long filename shortening algorithm is non-obvious to the world of programming. Programmers must all be idiots.
Re:Another view (Score:2)
Yes, actually. Peanut butter, if possible.
Re:Another view (Score:2)
reminds me of mathematica (Score:2)
You sure you want to do the math? (Score:4, Informative)
I looked at 3D rendering quite a while back (about 15 years ago). Believe me -- you probably don't really want to built this up from scratch.
It sounds like you just need some sample code that doesn't rely on OpenGL or DirectX. To that end, the following (open-source) projects may be useful:
Re:You sure you want to do the math? (Score:1)
Simple 3D rendering like this is easy. It's just stringing a bunch of math end to end.
Now, doing it fast...
-:sigma.SB
Re:You sure you want to do the math? (Score:2)
Nested for loops are your friend (Score:1)
So what is you're API that lets you turn pixels on and off? Its not that hard to go from a cloud of points in openGL to a cloud of points in another api.
Contour lines can be harder. you need to connect points on the same elevation without drawing a line through higher/lower elevation. I'd say don't bother connecting lines. just draw a lot of points (of certain colors for certain heights) with a fine enough granularity that they bleed into
Re:Nested for loops are your friend (Score:2)
Though I don't think that would be much harder.
Re:Nested for loops are your friend (Score:1)
Reading the quote as "3d mesh" sounds plausable, I've never heard of a 3d mess (although some games come close).
MATLAB (Score:4, Interesting)
Re:MATLAB (Score:2)
2D contouring (Score:5, Informative)
Good 2D contour plotting is not easy (I know!!!).
If you are not afraid of coding, then the best reference that I know of is:
by Albrecht Preusser, ACM TOMS V10#4
The code (in FORTRAN, unfortunately) is also in TOMS as:
Algorithm 626 TRICP: A Contour Plot Program Triangular Meshes
You can get ACM TOMS (Transaction on Mathematical Software) from any reasonable college library.
This might be a bit of overkill, as it is designed for irregular data sets (versus regular datasets - grids). However, the code can be separated into a separate Delaunay Triangulation step. If you have a grid (or in your case, a function f(x,y)), it is easy to generate fixed triangles.
If anybody knows of a better algorithm, I'm all ears!!!
Re:2D contouring (Score:3, Informative)
Pardon me, I'd like to make a correction. Good 2D contour plotting from scattered point data is not easy. For contour plotting from a regular grid, the marching squares algorithm is simple, easy to implement, and works pretty well. Run it several times with different reference values to get all the contours you want.
Uhhh... I said good 2D contouring is hard. Marching squares is by NO stretch of the imagination good, even with regular data. Easy, yes. Good, no.
It has discontinuities. Good contourin
Why not OpenGL? (Score:3, Interesting)
Re:Why not OpenGL? (Score:1)
Re:Why not OpenGL? (Score:2)
R-project (Score:3, Insightful)
Re:R-project (Score:3, Informative)
I'm out of mod points, but this is insightful. The R programming language [r-project.org] is GPL'ed and works on lin/win/osx (packages for major distros). It is an interpreted language (except for a few internal commands), and so the source code for the several different 3D plotters is included with the program. Some you might have to install yourself, but this can be done by the install.packages command.
You might want to have a quick look at output from different 3D commands [ucl.ac.be] (persp, scatterplot3d and wireframe).
The in
Been there, done that. (Score:2)
I wrote a program to do that in 1968, for a UNIVAC 1108 with a pen plotter. Even did hidden surface elimination. We even ran it on a display a few times during really slow periods. But I don't have the ALGOL code any more.
The algorithm was in JACM around 1967, but you wouldn't do it that way today.
Basic Rundown (Score:5, Informative)
1) Take each 3d point of interest and apply some set of rotations around the X, Y, and Z axis to get the view angle right. You'll probably want to just do one rotation around each axis. e.g.:
x2 = x * cos( z_angle ) + y * sin( z_angle );
y2 = x * -sin( z_angle ) + y * cos( z_angle );
x3 = x2 * cos( y_angle ) + z * sin( y_angle );
z2 = x2 * -sin( y_angle ) + z * cos( y_angle );
y3 = y2 * cos( x_angle ) + z2 * sin( x_angle );
z3 = y2 * -sin( x_angle ) + z2 * cos( x_angle );
At this point, you'll have rotated your original coordinate (x,y,z) by some angle around some axis to get (x3,y3,z3)
(To my fellow graphics geeks: yes, yes, I know full well that matrices and quaternions are better -- I'm trying keep this simple.)
2) Maybe add some distance to the z value, to move ("translate") it away from the "camera" at the origin. You'll need to add enough to make sure that all of your z-values are positive and non-zero before step 3:
z4 = z3 + z_translate
3) Do what's called the "perspective divide" to find the two dimensional coordinate of this point. e.g.:
px = x3 / z4 * scale + center_x;
py = y3 / z4 * scale + center_y;
4) Either plot a point at the (px,py) that you just got, or compute steps 1 and 2 twice for the end points of lines and then use your system's graphics primitives to draw lines between (px1,py1) and (px2,py2). If you're not using a language or a library that can do simple 2d graphics, then I'd suggest writing out a file with the lines in SVG format (and then rasterize to bitmap with a program like Batik, Inkscape, Illustrator, etc.) e.g.:
<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd
<svg width="4cm" height="4cm" viewBox="0 0 400 400" xmlns="http://www.w3.org/2000/svg" version="1.1">
<path d="M px1 py1 L px2 py2" fill="none" stroke="black" stroke-width="1"
<path d="M px1 py1 L px2 py2" fill="none" stroke="black" stroke-width="1"
<path d="M px1 py1 L px2 py2" fill="none" stroke="black" stroke-width="1"
</svg>
5) If you want more than wireframe, things will be a lot more complicated. At the very least, you'll need to be able to fill polygons and sort back to front by the z-value after the rotation so that everything gets drawn in the correct order. Z-buffers would be even better.
Well, I hope all that helps. Again, if anyone's really interested, I'd suggest getting a real book on the topic. But I hope this's enough to get the curious started.
Re:Basic Rundown (Score:4, Interesting)
Also, if you're rotating the graph in real-time, don't rotate the points a degree, plot, and rotate another degree and plot, and so on.
Save the original set of points. Rotate them one degree and plot. Throw those results away, start with the original set and rotate them two degrees and plot. Repeat, rotating only the original set each time. You'll save a lot of trouble from accumulating floating point errors.
Re:Basic Rundown (Score:2)
If you're willing to have a wire-frame look... (Score:2, Interesting)
For xtemp = xlower to xupper step xincr
for ytemp = ylower to yupper step yincr
compute z=f(xtemp,ytemp)
(x,y)=transform(xtemp,ytemp,z)
if yymax(x) then plot x,y : ymax(x) = y
next ytemp, xtemp
Adjust your viewpoint for your 3D to 2D matrix so it plots from front to back.
Algorithm comes from an old book on doing 3D on an Apple ][ - it wasn't too hard to convert it to work on a TRS-80 Model I or a CoCo.
Shouldn't be too tough... (Score:2, Informative)
Here's what you need to do. First, figure out what a unit in the X
Read a book (Score:5, Informative)
Hate to disappoint you, but you're probably not going to find a tutorial and code that conveniently meets your requirements. For starters, eliminating OpenGL and D3D is going to remove a large number of tutorials, simply because it doesn't make sense for most people to roll their own rendering code.
Your best bet would be to look through "Computer Graphics: Principles and Practice" by Foley, van Dam, Feiner and Hughes. It covers a large number of topics, in enough detail to get a good idea of how to get started and where to look for more information. Also "Real-Time Rendering" by Tomas Möller and Eric Haines. Despite the name, a large portion of the book is applicable to almost all computer graphics, not just real-time. Also, a book on linear algebra would probably be helpful.
Re:Read a book (Score:2, Informative)
Mathematical Elements for Computer Graphics
http://www.nar-associates.com/nar-publishing/mecg
Drawing. (Score:2)
In times of Amiga, it would be like typing "ScreenOpen 320,256,32", then a line defining the palette in equally simple terms, then you can start the drawing stuff. On PCs there are dozens of graphics libraries and each one more difficult to use than another. Microsoft Visual someshit have this pretty
Re: Drawing. (Score:2)
There's a GTK+ add-on called "GTK Extra", which directly supports 2D and 3D plots. It was created as the infrastructure for SciGraphica.
Re: Drawing. (Score:2)
Drawingarea is horrible. No idea about this GTK Extra but I seriously doubt if it'
Java (Score:1)
algorithms for surface plots (Score:2)
There are a couple of things to watch out for. First, all polygons should be convex. This is usually not a problem, but you ca
Re:algorithms for surface plots (Score:2)
See what you can come up with (Score:2, Insightful)
Unfortunately, even if you have an API, you still have to understand what it does and how to interact with it. Many people believe that modular programming
Statistics and Data Analysis in Geology by Davis (Score:1, Insightful)
A relatively simple way (Score:2)
Another way was to use vector math, make a triangle between three terrain points and try to see if it intersects with your Z plane. Maybe ten years ago I made an algorithm that returns a line segment if there was such
Marching cubes (Score:2, Informative)
see: http://www.exaflop.org/docs/marchcubes/index.html [exaflop.org]
Generic Mapping Tool (Score:3, Interesting)
It's a combination of two simple issues... (Score:2)
The 3d to 2d mapping is actually pretty easy, with a little Linear Algebra. If you don't know any, grab a Linear algebra textbook and read up on vectors, dot products, cross products, etc.
You pick a viewpoint V = (x_v, y_v, z_v) and a direction vector D = (d_x, d_y, d_z) ( D is a unit vector, it's length should be 1). and an "up" vector U (which way is up, also a unit vector, perpendicular to D). Next we
Graphing Calculator (Score:2)
Marching Cubes Algorithmhttp://www.exaflop.org/doc (Score:2)
what about PLOT10? (Score:1)
Re:what about PLOT10? (Score:1)
If you are looking at creating a 3d Renderer... (Score:1)
Octave (Score:1)