http://www.perlmonks.org?node_id=1043029

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

Hi monks, a while ago i was working on a cube that rotates along one of his axes. But i couldn't make it work and after a while i left it at that. Now i decided to rewrite, simplify and finish it. I did some other similar things since then that worked fine so i should be allright. My question has to do with adding depth to the picture. I use the same technique as they tought me in high school. So a 45 degree z-axis with the same length unit as on the other axes. So a 3-d coordinate like (x,y,z): @two = (440,360,80) would be converted like

$x2 =@two[0] + sqrt((@two[2]*@two[2])/2)

I hope you understand what I meen. What i would like to ask if there are better ways of handling this. Maybe a module that can support something like that, maybe some advice on how to better give depth to the image etc. I'm working with the GD module by the way. Any response would be appreciated. Thank you

Replies are listed 'Best First'.
Re: rotating cube.
by rjt (Curate) on Jul 08, 2013 at 04:04 UTC

    For an in-depth look at the math (with some other simple formulae you can experiment with), take a look at 3D Projection at Wikipedia.

    Back to Perl, if you want to simply plot a non-moving cube (or pre-render a series of video frames) without fancy shading, texture mapping, etc., GD or Image::Magick will do just fine. For more realistic 2D renders or any sort of "live" 3D animation of a rotating cube, have a look at SDL::OpenGL. There is also POGL (OpenGL), but it looks to have gone stale. I am not sure how good SDL::OpenGL is, for that matter.

    Honorable mention for 2D work at least would be PDL::Graphics::OpenGL for an OpenGL plugin to the Perl Data Language, but PDL is probably pretty far from what you need to draw a simple cube.

      Thank you for replying. Just for the record: You're saying that if i work with the GD module or Image::Magick(i'm not familiar with that module) there is no other way than to manually convert 3d coordinates to 2d. If i want support for handling 3d coordinates i need a completely new module like SDL::OpenGL. Is this correct? Much obliged.

        Correct. Both libraries are fundamentally 2D with no 3D support, besides some typical "3D Graphing" features. You should be able to figure out simple shapes easily enough. Beyond that, especially once you get into overlapping vertices in the visual projection, figuring out what is visible and drawing in a sensible order can get hairy, and a pure-ish Perl solution would best be measured in seconds per frame, not the inverse.

        So, my advice would be, either keep it very simple, and learn some fun math, or embrace a much bigger library from the start. Whatever you decide, you might also try out something like Blender (free 3D software) to rapidly prototype your models and boost your visual comprehension. Good luck! Come back and post your creation.

Re: rotating cube.
by hdb (Monsignor) on Jul 08, 2013 at 09:16 UTC

    Two comments:

    1. If you have use warnings; then you already know, but otherwise accessing the first element of your list you would write $two[0].
    2. You could replace sqrt((@two[2]*@two[2])/2) by $two[2]/sqrt(2).

Re: rotating cube.
by zork42 (Monk) on Jul 13, 2013 at 09:39 UTC
    As hdb said, use warnings; helps spot errors at compile time.

    use strict; is similarly very useful.

    Most programs start with:
    #!/usr/bin/perl use strict; use warnings;
rotating cube
by arraypopper (Initiate) on Jul 13, 2013 at 06:24 UTC

    Sorry to bother you once more. Last week i posted a question here and i decided to post some work. It seems my memory has betrayed me with regard to presenting the z-axis. I mentioned a 45-degree angle with a one on one scale with regard to the other axis. Turns out this is a 30-degree axis with a half-length scale. I don't want to go into the mathematics, and i don't have a question, i just wanted to correct this. Bye.