We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › [solved] Weird lines appearing.. please help!
Page Index Toggle Pages: 1
[solved] Weird lines appearing.. please help! (Read 471 times)
[solved] Weird lines appearing.. please help!
Mar 3rd, 2009, 12:53am
 
Hi!
I'm new to Processing and tried to make a sketch with a rotating cube. It works but there are strange lines appearing, all going towards one point... I don't understand how I can get rid of them!
Would be nice if you can have a look!
Greetings Powder

int WIDTH, HEIGHT, ANZ_POINTS_1DIM, ANZ_POINTS_GES, DEPTH, ANZ_LINES, anz_lines_straight;
double DELTA, ROT_DELTA, ROT_DELTA_DELTA, rot;
Point pnts[][][];
Line lines[];
Line linesStraight[];

void setup()
{
 frame.removeNotify();
 frame.setUndecorated(true);
 frame.addNotify();
 WIDTH = screen.width;
 HEIGHT = screen.height;
 size(WIDTH, HEIGHT, P3D);
 background(0);
 stroke(255);
 smooth();
 
 // Set up variables
 ANZ_POINTS_1DIM = 4;
 ANZ_LINES = 100;
 DELTA = WIDTH/ANZ_POINTS_1DIM;  //Aenderung
 ANZ_POINTS_GES = ANZ_POINTS_1DIM*ANZ_POINTS_1DIM*ANZ_POINTS_1DIM;
 anz_lines_straight = ANZ_POINTS_GES*2; //Stimmt nicht, nur Testwert
 DEPTH = 100;
 ROT_DELTA = PI/128.0;
 ROT_DELTA_DELTA = 0.1;
 rot = 0;
 
 pnts = new Point[ANZ_POINTS_1DIM][ANZ_POINTS_1DIM][ANZ_POINTS_1DIM];
 lines = new Line[ANZ_LINES];
 
 // Fill Array
 for(int i=0; i<ANZ_POINTS_1DIM; i++)
 {
   for(int j=0; j<ANZ_POINTS_1DIM; j++)
   {
     for(int k=0; k<ANZ_POINTS_1DIM; k++)
     {
       pnts[i][j][k] = new Point(i*DELTA-WIDTH/2, j*DELTA-HEIGHT/2, k*DELTA-DEPTH/2);
     }
   }
 }
 
 //Create Straight Lines
 createStraightLines();
}

void draw()
{
 background(0);
 smooth();
 translate(WIDTH/2, HEIGHT/2, DEPTH/2);
 rotateY((float)rot);
 rotateZ((float)rot*0.5);
 drawAllPnts();
 drawStraightLines();
 rot += ROT_DELTA;
}

void drawAllPnts()
{
 for(int i=0; i<ANZ_POINTS_1DIM; i++)
 {
   for(int j=0; j<ANZ_POINTS_1DIM; j++)
   {
     for(int k=0; k<ANZ_POINTS_1DIM; k++)
     {
       pnts[i][j][k].drawPnt();
     }
   }
 }
}

void createStraightLines()
{
 int curLine = 0;
 for(int i=0; i<ANZ_POINTS_1DIM; i++)
 {
   for(int j=0; j<ANZ_POINTS_1DIM; j++)
   {
     for(int k=0; k<ANZ_POINTS_1DIM; k++)
     {
       checkArraySize(curLine);
       if( pointExistsInArray( i+1, j, k ) )
       {
         Point p1 = new Point( (float)pnts[i][j][k].x, (float)pnts[i][j][k].y, (float)pnts[i][j][k].z);
         Point p2 = new Point( (float)pnts[i+1][j][k].x, (float)pnts[i+1][j][k].y, (float)pnts[i+1][j][k].z);
         lines[curLine] = new Line(p1, p2);
         anz_lines_straight = curLine+1;
         curLine++;          
       }
       if( pointExistsInArray( i, j+1, k ) )
       {  
         lines[curLine] = new Line(new Point( (float)pnts[i][j][k].x, (float)pnts[i][j][k].y, (float)pnts[i][j][k].z ),
                                   new Point( (float)pnts[i][j+1][k].x, (float)pnts[i][j+1][k].y, (float)pnts[i][j+1][k].z ));
         anz_lines_straight = curLine+1;
         curLine++;
       }
       if( pointExistsInArray( i, j, k+1 ) )
       {
         lines[curLine] = new Line(new Point( (float)pnts[i][j][k].x, (float)pnts[i][j][k].y, (float)pnts[i][j][k].z ),
                                   new Point( (float)pnts[i][j][k+1].x, (float)pnts[i][j][k+1].y, (float)pnts[i][j][k+1].z ));
         anz_lines_straight = curLine+1;
         curLine++;
       }  
     }  
   }
 }
}

// Returns new size of array
int checkArraySize(int pos)
{
 if(pos >= (lines.length-1))
   //Double size of Array
   lines = (Line[])expand(lines);
   return lines.length;
}

void drawStraightLines()
{
 stroke( (float)(abs( sin(radians(frameCount)) ) * 225) + 30, 0, 0 );
 for(int i=0; i<anz_lines_straight; i++)
 {
   lines[i].drawLine();
 }
}

boolean pointExistsInArray(int i, int j, int k)
{
 if(i<ANZ_POINTS_1DIM && j<ANZ_POINTS_1DIM && k<ANZ_POINTS_1DIM)
   return true;
 return false;
}

class Point
{
 double x, y, z;
 Point()
 {
   this.x = 0.0;
   this.y = 0.0;
   this.z = 0.0;
 }
 Point(double x, double y, double z)
 {
   this.x = x;
   this.y = y;
   this.z = z;
 }
 void drawPnt()
 {
   point((float)this.x, (float)this.y, (float)this.z);  
 }
}

// Class Line stores 2 3D Points
class Line
{
 Point p1, p2;
 
 Line(Point p1, Point p2)
 {
   this.p1 = p1;
   this.p2 = p2;
 }
 
 void drawLine()
 {
   line((float)p1.x, (float)p1.y, (float)p1.z, (float)p2.x, (float)p2.y, (float)p2.z);
 }
}

Re: Weird lines appearing.. please help!
Reply #1 - Mar 3rd, 2009, 12:04pm
 
Hi,

it seems that this is just a matter of having your object to close to the "camera". I just tried to to put it further away and the strange lines disappeared:

translate(WIDTH/2, HEIGHT/2, DEPTH/2-1000);

There are probably other ways to get rid of the lines. For example doing something with camera, perspective, frustum, ...
Re: Weird lines appearing.. please help!
Reply #2 - Mar 3rd, 2009, 12:13pm
 
Thanks a lot! It works!
But this is strange! Is it a bug?
Re: Weird lines appearing.. please help!
Reply #3 - Mar 3rd, 2009, 1:27pm
 
No, that's not a bug.. it's the normal result when projecting points on a projection plane, that lie behind it (at least I think so Smiley )
Re: Weird lines appearing.. please help!
Reply #4 - Mar 3rd, 2009, 8:07pm
 
Any idea what I can do if I want the camera to be inside the cube?
Re: Weird lines appearing.. please help!
Reply #5 - Mar 4th, 2009, 9:35am
 
I seems that with the OpenGL renderer, the points behind the camera automatically get clipped. So:

import processing.opengl.*;
...
size(WIDTH, HEIGHT, OPENGL);

made your original sketch working without weird lines. Although I had to remove the "frame.setUndecorated(true);"-stuff, because I got an empty screen.

If there is a way to do the clipping with the P3D renderer, I don't know.
Re: Weird lines appearing.. please help!
Reply #6 - Mar 4th, 2009, 11:59pm
 
OK thanks! That works really well! For me it doesn't matter if I use P3D or OpenGL!
Page Index Toggle Pages: 1