import gpdraw.*;

public class chemical
{
    public static void main(String args[])
    {
        DrawingTool pencil=new DrawingTool(new SketchPad(700, 700));
        double points[][]=new double[4][3];
        double theta=0;
        double vx=0, vy=0, vz=2;
        while(true)
        {
            points[0][0]=0;//x
            points[0][1]=1;//y
            points[0][2]=0;//z

            points[1][0]=.94281*Math.cos(theta);
            points[1][1]=-1/3;
            points[1][2]=.94281*Math.sin(theta);

            points[2][0]=.94281*Math.cos(theta+2.09439);
            points[2][1]=-1/3;
            points[2][2]=.94281*Math.sin(theta+2.09439);

            points[3][0]=.94281*Math.cos(theta+4.18879);
            points[3][1]=-1/3;
            points[3][2]=.94281*Math.sin(theta+4.18879);

            double screenx, screeny;
            for(int i=0; i<4; i++)
            {
                double x, y, z;
                x=points[i][0];
                y=points[i][1];
                z=points[i][2];

                screenx=vx+(vz-1)*(x-vx)/(vz-z);
                screeny=vy+(vz-1)*(y-vy)/(vz-z);

                pencil.up();
                pencil.move(200*screenx, 200*screeny);
                pencil.down();
                pencil.drawCircle(5);
            }
            pencil.up();
            pencil.move(200*vx/vz, 200*vy/vz);
            pencil.down();
            pencil.drawCircle(5);
            theta+=.1;
        }

    }
}

I am having trouble with this, spefically dealing with the camera.
This program uses gpdraw, I dont know how to upload the .jar file to use it in program, but I can make a google docs if necessary.
I have a CH4 methane molecule, with the C at (0, 0, 0), and one H at (0, 1, 0). The other ones are places appropriately, with a y value of -1/3 (if I am correct). They rotate about the y-axis; only those with y-value -1/3 will change their x and z values according to the variable theta (changes by .1 radians). the variables vx, vy, vz represent tghe coordinates of the 'camera'.
My calculation is this: I construct a line between the camera and the point to be 'looked at'. I see where this line intersects the plane z=1 (simplest one), and that point's x and y values are what are put onto the screen.
(screenx=vx+(vz-1)(x-vx)/(vz-z);screeny=vy+(vz-1)(y-vy)/(vz-z); these formulas should be correct (please check them?)

My problem is this.
When I set my camera to (0, 0, 2) the central carbon appears to be coplanar with the three rotating points! This is clearly not correct, as they have a y value of -1/3. Furthermore, their y position on the screen should be changing because their plane does not pass through the camera! What is going on?

Dani AI

Generated

Most likely cause: the three hydrogens were given a y value with the expression -1/3. In Java that performs integer division and yields 0, so those H atoms end up with y==0 — the same as the central carbon — which explains the “coplanar” appearance. Force a double division (for example -1.0/3) so the y coordinate is actually −0.333….

Why that explains the behavior: rotating about the y‑axis keeps each atom’s y coordinate fixed, so if you accidentally set those y values to 0 they stay aligned with the carbon. With the correct fractional y the projected screen y will vary because each atom’s z (and therefore the projection parameter) changes as they rotate. Also watch out for points whose z equals the camera z — that causes a division-by-zero in the projection and needs a guard (skip, clamp, or move the projection plane).

Quick troubleshooting checklist

  • Print the raw 3D coordinates and the computed screen x/y each frame to confirm what values you’re actually projecting.
  • Temporarily draw simple axes or labelled test points so you can tell orientation and scale at a glance.
  • Add a small epsilon check around z == vz to avoid NaNs.
  • Make sure any visual marker you draw for the camera is produced consistently with the same projection math as the other points (drawing the camera using a different formula is misleading).

About gpdraw and the JAR: add the jar to your classpath. In Eclipse: Project → Properties → Java Build Path → Libraries → Add External JARs. From the command line run with the JAR on the classpath (Windows: java -cp .;gpdraw.jar chemical, Unix: java -cp .:gpdraw.jar chemical).

If this doesn’t fix it, post the printed coordinates for one frame (camera plus one H with x,y,z and its screen x,y) and someone can check the numbers.

Recommended Answers

All 2 Replies

whats gpdraw?

your post is quite laborious on the eye , maybe if you 1st describe how you are trying to solve it through your program , people here can help you out better.

gpdraw is just basically a class/jar file that creates a new window and has a bunch of methods for drawing stuff on the screen, it creates a 'drawingtool' object that is much like a real life pencil.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.