How can I make points furthest away form the screen to appear smaller than the once closer to the screen?
in
Programming Questions
•
2 years ago
I am working on a 3D scatter plot and I want to make the points furthest away form the screen to appear smaller than the once closer to the screen. I understand that using perspective() would be the best way to do it but I cant seem to get it to work. Does anybody know how to get it to work? I have attached the codes that I have done so far (thank you amnon.owned for the preliminary sketch);
- PVector[] vecs = new PVector[100];
- int dim = 250;
- float rotY; // Amount of rotation around y-axis.
- float rotX; // Amount of rotation around the x-axis.
- void setup() {
- size(500,500,P3D);
- smooth();
- rotY = 0;
- rotX = 0;
- float cameraZ = ((width/10.0) / tan(PI*60.0/360.0));
- // adjusted from perspective() reference to narrow fov
- perspective(PI/3.0, float(width)/height, cameraZ/100.0, cameraZ*100.0);
- for (int i=0; i<vecs.length; i++) {
- vecs[i] = new PVector(random(dim),random(dim),random(dim));
- }
- }
- void draw() {
- background(255);
- translate(width/2,height/2);
- scale(1,-1,1); // so Y is up, which makes more sense in plotting
- rotateY(rotY);
- rotateX(rotX);
- noFill();
- strokeWeight(1);
- stroke(0,10);
- box(dim);
- translate(-dim/2,-dim/2,-dim/2);
- strokeWeight(2);
- stroke(0);
- line(0,0,0,dim,0,0);
- line(0,0,0,0,dim,0);
- line(0,0,0,0,0,dim);
- for (int i=0; i<vecs.length; i++) {
- //vecs[i].y = noise(frameCount * 0.005 + i) * dim;
- PVector v = vecs[i];
- //stroke(0,75);
- //strokeWeight(2);
- //line(v.x,0,v.z,v.x,v.y,v.z);
- stroke(255,34,34);
- strokeWeight(5);
- point(v.x,v.y,v.z);
- }
- }
- void mouseDragged()
- {
- rotY += (mouseX-pmouseX)*0.01;
- rotX += (mouseY-pmouseY)*0.01;
- }
1