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 & HelpSyntax Questions › how to determine x,y,z location
Page Index Toggle Pages: 1
how to determine x,y,z location (Read 3348 times)
how to determine x,y,z location
May 17th, 2010, 11:33am
 
e.g.

If I draw a point
point(2,3,4);

What command to use to determine the x,y and z location ?
Re: how to determine x,y,z location
Reply #1 - May 17th, 2010, 11:45am
 
The point method draws a single pixel on the screen at the given x/y/z coordinates passed as parameters. There is no way of looking at the pixel drawn and determining the x/y/z coordinates used to create it.

If you want to know the 2D position where the 3D point will be drawn then you can use screenX and screenY
Re: how to determine x,y,z location
Reply #2 - May 17th, 2010, 4:56pm
 
Yup..

I usually make a class like this:

Code:

class Point {

float x;
float y;
float z;

Point(float xpos, float ypos) {

x = xpos;
y = ypos;

}

Point(float xpos, float ypos, float zpos) {

x = xpos;
y = ypos;
z = zpos;

}

}


And then continue to work with points like this:

Code:

Point p = new Point(mx, my, mz);
points_3d = (Point[]) append(points_3d, p);


This way, you can simply revoke a points coordinates by means of printline("this point its z-coordinate is "+points_3d[i].z);
Re: how to determine x,y,z location
Reply #3 - May 18th, 2010, 12:04am
 
If you want to point at something (picking) with the mouse, such as a box, sphere, etc.; you could use one of these methods:
http://processing.org/hacks/hacks:picking
Re: how to determine x,y,z location
Reply #4 - May 18th, 2010, 5:34am
 
The reason behind the question is that I am making a sphere with dots however I am using the rotateZ()function and as a result I have no way of determining after the process is complete which point is the nearest.
Url = http://www.openprocessing.org/visuals/?visualID=6566
Pic = ...
Here is the code
Code:

//December 15th 2009
//EasySphere v1.0
//Owaun Scantlebury

import peasy.org.apache.commons.math.*;
import peasy.*;
import peasy.org.apache.commons.math.geometry.*;
PeasyCam cam;
void setup(){
size(300,300,P3D);  
background(255);  
cam= new PeasyCam (this,width);
}

void draw(){
translate(-width/2,-height/2);
background(255);  
for (float z=0;z<360;z+=0.1){ //0.01 to increase resolution
 translate(width/2,height/2,0);
 rotateX(z);
 //line(0,0,30,30); //uncomment for solid sphere
 point(30,30);
 rotateZ(z);
 point(30,30);
 //line(0,0,30,30); //uncomment for solid sphere
 translate(-width/2,-height/2,0);
 

}  

}
Re: how to determine x,y,z location
Reply #5 - May 18th, 2010, 11:17pm
 
[edit - you probably want to determine which point is *actually* nearest, not visually.  So this info may not be useful.]

You can use lights -- here is a quick and dirty example ...

Quote:
//December 15th 2009
//EasySphere v1.0
//Owaun Scantlebury

import peasy.org.apache.commons.math.*;
import peasy.*;
import peasy.org.apache.commons.math.geometry.*;
PeasyCam cam;
void setup(){
  size(300,300,P3D);  
  background(255);  
  cam= new PeasyCam (this,width);
  noStroke();
}

void draw(){
  translate(-width/2,-height/2);
  lights();
  background(255);  
  for (float z=0;z<360;z+=0.1){ //0.01 to increase resolution
    translate(width/2,height/2,0);
    rotateX(z);
    pushMatrix();
    translate(30,30,0);
    rect(-.25,-.25,.5,.5);
    popMatrix();
    rotateZ(z);
    pushMatrix();
    translate(30,30,0);
    rect(-.25,-.25,.5,.5);
    popMatrix();
  }  



Another way would be to simulate the effect of gl.fog(), by drawing successive vertical "curtains" (rects) of translucent color, slicing the scene and giving distant particles a "haze"...
Re: how to determine x,y,z location
Reply #6 - May 19th, 2010, 6:22am
 
BenHem that process sounds cool.. do you have a code example I can try ?
Re: how to determine x,y,z location
Reply #7 - May 19th, 2010, 2:08pm
 
erm.. andre, I just passed you the answer.. if instead of drawing the points you add them to an array of points, you can recall the coordinates at any time, and instead in the draw you make a function like this:

Code:

for(int i = 0; i < points.length; i++) {
point(points[i].x, points[i].y, points[i].z);
}


that's all..

Code:


import processing.opengl.*;
import javax.media.opengl.*;

Point[] points = new Point[0];

float depth = 0;

float current_z = 0;
float current_rotation = 0;

float rotx;
float roty;


void setup() {

 size(600, 600, OPENGL);
 smooth();
 
 float step = 50;

 for(float i = 0; i < TWO_PI; i+= TWO_PI/step) {

   pushMatrix();

   rotateY(i);

   for(float j = 0; j < TWO_PI; j+= TWO_PI/step) {

float s = 200;

float x = s*cos(j);
float y = s*sin(j);

points = (Point[]) append(points, new Point(modelX(x, y, 0), modelY(x, y, 0), modelZ(x, y, 0)));

   }

   popMatrix();

 }

}


void draw() {

 background(255);

 translate(width/2, height/2);

 rotx = (current_rotation/3.0)%TWO_PI;
 roty = current_rotation%TWO_PI;

 rotateY(roty);
 rotateX(rotx);

 stroke(0);
 for(int i = 0; i < points.length; i++) {

   point(points[i].x, points[i].y, points[i].z);
   
 }
 
 current_rotation += 0.005;

}


class Point {

 float x;
 float y;
 float z;

 Point(float xpos, float ypos) {

   x = xpos;
   y = ypos;

 }
 
 Point(float xpos, float ypos, float zpos) {

   x = xpos;
   y = ypos;
   z = zpos;

 }

}
Re: how to determine x,y,z location
Reply #8 - May 19th, 2010, 2:38pm
 
now you can check for depth:

Code:
void draw() {

 background(0);

 translate(width/2, height/2);

 rotx = (current_rotation/3.0)%TWO_PI;
 roty = current_rotation%TWO_PI;

 rotateY(roty);
 rotateX(rotx);

 //stroke(0);
 for(int i = 0; i < points.length; i++) {
   
   float mapped_value = map(modelZ(points[i].x, points[i].y, points[i].z), -200, 200, 255, 0);
   stroke(mapped_value, 255 - mapped_value, mapped_value);
   point(points[i].x, points[i].y, points[i].z);
   
 }
 
 current_rotation += 0.005;

}
Re: how to determine x,y,z location
Reply #9 - May 20th, 2010, 5:12am
 
Thanks Rapatski. I will try it with my easysphere sketch and post the results here.

Thanks again.
Re: how to determine x,y,z location
Reply #10 - May 20th, 2010, 5:34am
 
Here is what I have so far. I have replaced the points with OPENGL-anti-aliased lined. It much sweeter and faster like this.

Using the Peasycam library is optional.

Quote:
import peasy.org.apache.commons.math.*;
import peasy.*;
import peasy.org.apache.commons.math.geometry.*;
PeasyCam cam;
import processing.opengl.*;
import javax.media.opengl.*;

Point[] points = new Point[0];

float depth = 0;

float current_z = 0;
float current_rotation = 0;

float rotx;
float roty;

GL gl;
PGraphicsOpenGL pgl;
public String mode = "OPENGL";

void setup() {


  if(mode=="OPENGL")size(600,600,OPENGL);
  if(mode=="P3D")size(600,600,P3D);

  lights();
  hint (DISABLE_DEPTH_TEST);
  hint(DISABLE_OPENGL_2X_SMOOTH);
  hint(ENABLE_OPENGL_4X_SMOOTH);
  if (mode == "OPENGL"){
    pgl = (PGraphicsOpenGL) g;
    gl = pgl.gl;
    gl.glHint (gl.GL_LINE_SMOOTH_HINT, gl.GL_NICEST);
    gl.glEnable (gl.GL_LINE_SMOOTH); 
  }
  if (mode == "P3D"){
    smooth();
  }
  cam = new PeasyCam (this,width);

  float step = 50;

  for(float i = 0; i < TWO_PI; i+= TWO_PI/step) {

    pushMatrix();

    rotateY(i);

    for(float j = 0; j < TWO_PI; j+= TWO_PI/step) {

      float s = 200;

      float x = s*cos(j);
      float y = s*sin(j);

      points = (Point[]) append(points, new Point(modelX(x, y, 0), modelY(x, y, 0), modelZ(x, y, 0)));

    }

    popMatrix();

  }

}


void draw() {

  background(255);

  translate(width/2, height/2);

  rotx = (current_rotation/3.0)%TWO_PI;
  roty = current_rotation%TWO_PI;

  rotateY(roty);
  rotateX(rotx);

  stroke(0);
  for(int i = 0; i < points.length; i++) {

    //point(points[i].x, points[i].y, points[i].z);
    if (i>0) line(points[i].x, points[i].y, points[i].z,points[i-1].x, points[i-1].y, points[i-1].z);

  }

  current_rotation += 0.005;


  draw2();
}


class Point {

  float x;
  float y;
  float z;

  Point(float xpos, float ypos) {

    x = xpos;
    y = ypos;

  }

  Point(float xpos, float ypos, float zpos) {

    x = xpos;
    y = ypos;
    z = zpos;

  }

}


void draw2() {

  // background(0);

  translate(width/2, height/2);

  rotx = (current_rotation/3.0)%TWO_PI;
  roty = current_rotation%TWO_PI;

  rotateY(roty);
  rotateX(rotx);

  //stroke(0);
  for(int i = 0; i < points.length; i++) {

    float mapped_value = map(modelZ(points[i].x, points[i].y, points[i].z), -200, 200, 255, 0);
    stroke(mapped_value, 255 - mapped_value, mapped_value);
    //  point(points[i].x, points[i].y, points[i].z);
    if (i>0) line(points[i].x, points[i].y, points[i].z,points[i-1].x, points[i-1].y, points[i-1].z);

  }

  current_rotation += 0.005;







Re: how to determine x,y,z location
Reply #11 - May 20th, 2010, 1:43pm
 
.. But why do you have two draw functions now?

My second post was meant to replace the draw function of the first... you really don't need two Smiley

Have fun!
Re: how to determine x,y,z location
Reply #12 - May 20th, 2010, 4:44pm
 
Fun indeed! Cheesy  I have it creating multiple shapes now,

Baskets,
Pots,
Pans,
even curving vases.

I am using the second draw to play around with colour co-ordinates. Affecting the stroke colour based on where the image is in the 3d-space.

Processing ROCKS!!!

Thanks again  Rapatski , Benhem and everyone else.
Re: how to determine x,y,z location
Reply #13 - May 21st, 2010, 3:34am
 
Er.. sure, but you don't have to rotate twice, loop through the points twice... etc. Good luck.
Re: how to determine x,y,z location
Reply #14 - May 27th, 2010, 3:56am
 
baskets?  pots?  pans?  vases?  pics or it didn't happen
Page Index Toggle Pages: 1