Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

3D Point Plotting

by Anonymous Monk
on May 27, 2002 at 22:58 UTC ( [id://169662] : perlquestion . print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi, just a quick question: does anyone know of any modules that will allow points to be plotted in 3 dimensions? I've already tried searching CPAN, and the closest thing seems to be Chart::Graph::Xrt3d, however that requires xrt3d, which appears to be commercial software. Are there any alternatives here?

Replies are listed 'Best First'.
Re: 3D Point Plotting
by Abigail-II (Bishop) on May 28, 2002 at 16:29 UTC
    I do all my (2D) plotting by just opening a pipe to gnuplot. That free available program (which has nothing to do with the GNU project(!)) runs on a wide range of platforms, and is able to do 3D plots as well.

    It's worth to check it out and to find out whether it satisfies your needs.


Re: 3D Point Plotting
by FoxtrotUniform (Prior) on May 28, 2002 at 01:43 UTC
      does anyone know of any modules that will allow points to be plotted in 3 dimensions?

    You might try the OpenGL set of modules, even though they're massive overkill for simply plotting 3d points.

    Update: If you're interested, the definitive OpenGL site is

    The hell with paco, vote for Erudil!

      Yeah, OpenGL would be crazy for use in CGI (looking to export the graph as png or such)... heh. And I tried looking at GD::Graph3d... it doesn't allow point plotting, just "3d" bar graph, "3d" pie graph, and a line graph with depth... not useful, unfortunately.
Re: 3D Point Plotting
by Merlin42 (Friar) on May 28, 2002 at 16:12 UTC
    I know you asked for a module, but this can be done very simply .. as long as you only want to do simple stuff :).
    If you are only interested in doing 3d points and have a static projective camera pointed along the Z axis then you can use the old 'divide by Z' trick. This is what is used in the classic 'flying stars/windows' screen saver. Lets say you are interested in veiwing some points inside a 3d cube of dimensions x1 to x2 by y1 to y2 by z1 to inf
    1. throw out points outside this box (cull/clip)
    2. divide by Z (projection)
    3. scale to viewport


    for each point x,y,z
    x2d=x/z; y2d=y/z
    plot Xviewport,Yviewport into the bitmap

    NOTE: This is off the top of my head so it might be utter jiberish

    NOTE: You could add one step b4 the cull/clip to transform your points so that they can be viewed along the z axis. This can be done w/ a 3x3 matrix multiply and a vector add(or with a 4x4 matrix mult ala OpenGL).

    NOTE: If you prescale Z by some factor you can get different "zooms"

    NOTE: If you want an orthographic projection, more like engineering diagrams/bluerpints then just skip all the divides and just thow away Z.
Re: 3D Point Plotting
by cjf (Parson) on May 28, 2002 at 00:21 UTC
      If I remember correctally, GD::Graph3d really only gives charts a '3d' appearance, I might be wrong though, as I haven't ever actually tried to use it. (I do use GD::Graph and GD though.)

      "Weird things happen, get used to it."

      Flame ~ Lead Programmer: GMS

Re: 3D Point Plotting
by rbc (Curate) on May 28, 2002 at 17:08 UTC
    You might want to see if this is what you are looking for

    simple 3D graphics

    The script in this link demonstrates how to draw a cube
    and perform rotation and translations on it.
      rbc, thanks, but that script works with Tk... which isn't really applicable in the context of CGI, as far as I know... gnuplot looks like it might do the trick, but I'll have to play around with it a bit more. And as to your suggestion, Merlin42, very interesting but a bit beyond me, for now (still interested though). I figure I might as well explain the point of all this anyways: I'm going to be having as data a large number of RGB values, and am trying to plot them all in a 3d cubic space, so one can visually see clustering of the colors. For now I'll try gnuplot some more, but any other suggestions would be great. Thanks.
        Ok first I want to apoligize for my previous post ... the formulas are fscked up. Rather than try to fix them for the general case Ill give you a simple version that will work for your specific case.
        for each point R,G,B contained in (0,1)x(0,1)x(0,1):
        X=(R-0.5)/(B+1)*IMAGE_WIDTH  + IMAGE_WIDTH/2
        Y=(G-0.5)/(B+1)*IMAGE_HEIGHT + IMAGE_HEIGHT/2
        You can change the 1 to 'dolly' the camera back and forth. Just swap the variables to view along a different color axis.
        To view not along a color axis you need to rotate the points before hand. eg
        R'= R*cos(theta)+G*sin(theta)
        B'= B
        to rotate about the B axis
        R'= R
        G'= G*cos(theta)+B*sin(theta)
        to rotate about the R axis, and
        to rotate about the G axis

        Geez I can't get anything right...maybe this really isnt as simple as I said :). The formulas should now map the entire RGB space into the image ... if you rotate the space then the corners will not be visible ... fix this by increasing the value added to the denomiator.