Loading...
Logo
Processing Forum
Hi, I've created a cone made of random points on the surface, but I get too many points near the apex. How can I get a uniform distribution?  Also I get a cone that looks like a diamond, but I want it positioned upside down, like this:  http://trialx.com/curetalk/wp-content/blogs.dir/7/files/2011/05/diseases/Cone-2.gif any tips on what I am doing wrong? Thanks.

The code here describes an animation. I have random points that go forming the cone when I press p.

Copy code
  1. import processing.opengl.*;

  2. float vel = 5;         
  3. float velr = 1;        

  4. int nums = 1000;        
  5. int numr = 150;        
  6. int winsize = 600;     
  7. int ballsize = 5;      

  8. float radius = 200;    
  9. float coheight = 300;  

  10. PVector[] randomp = new PVector[numr];  
  11. PVector[] solidp = new PVector[nums];   
  12. float[] cothetas = new float[nums];
  13. float[] cophis = new float[nums];

  14. void setup() {

  15.   size(winsize, winsize, OPENGL);
  16.   smooth();
  17.   noStroke();
  18.   starttime = millis();
  19.   for(int i=0; i<numr; i++) {
  20.     randomp[i] = new PVector(random(-winsize/2, winsize/2), random(-winsize/2, winsize/2), random(-winsize/2, winsize/2));
  21.   }
  22.   
  23.   for (int i=0; i<nums; i++) {
  24.     solidp[i] = new PVector(random(-winsize/2, winsize/2), random(-winsize/2, winsize/2), random(-winsize/2, winsize/2));
  25.     
  26.     //inizializzazione variabili cono
  27.     cothetas[i] = random(TWO_PI);
  28.     cophis[i] = random(coheight); 
  29.   }
  30. }   

  31. void draw() {
  32.   background(0);
  33.   translate(width/2, height/2);
  34.   rotateX(-PI/6.0);
  35.   rotateY(-PI/6.0);
  36.   //rotateY((-PI/36.0)*frameCount);
  37.   renderBackground();
  38.   if (key == 'p') renderCone(radius, coheight);
  39.   
  40. }

  41. void renderBackground() {
  42.   pushMatrix();
  43.   noFill();
  44.   for (int i=0; i<numr; i++) {
  45.     pushMatrix();
  46.     translate(randomp[i].x, randomp[i].y, randomp[i].z);
  47.     randomp[i].x+=random(-10, 10);
  48.     randomp[i].y+=random(-10, 10);
  49.     randomp[i].z+=random(-10, 10);
  50.     fill(255);
  51.     noStroke();
  52.     ellipse(0, 0, ballsize, ballsize);
  53.     //sphereDetail(3);
  54.     //sphere(ballsize);
  55.     popMatrix();
  56.   }
  57.   
  58.   for (int i=0; i<nums; i++) {
  59.     pushMatrix();
  60.     translate(solidp[i].x, solidp[i].y, solidp[i].z);   
  61.     fill(0, 255, 100);
  62.     noStroke();
  63.     ellipse(0, 0, ballsize, ballsize);
  64.     //sphereDetail(3);
  65.     //sphere(ballsize);
  66.     popMatrix();
  67.   }
  68.   popMatrix();
  69. }

  70. void renderCone(float radius, float coheight) {
  71.   float thisradius;
  72.   for (int i=0; i<nums; i++) {
  73.     thisradius = radius * (1-(cophis[i]/coheight));
  74.     println(thisradius);
  75.     rotateZ(cophis[i]);
  76.     rotateX(cothetas[i]);
  77.     if(solidp[i].x>cos(cothetas[i])*thisradius) solidp[i].x-=vel;
  78.     if(solidp[i].x<cos(cothetas[i])*thisradius) solidp[i].x+=vel;
  79.     if(solidp[i].z>sin(cothetas[i])*thisradius) solidp[i].z-=vel;
  80.     if(solidp[i].z<sin(cothetas[i])*thisradius) solidp[i].z+=vel;
  81.     if(solidp[i].y>cophis[i]-coheight/2) solidp[i].y-=vel;
  82.     if(solidp[i].y<cophis[i]-coheight/2) solidp[i].y+=vel;
  83.   }
  84. }

Replies(2)

To render the cone upside down, change line 83:
Copy code
  1. thisradius = radius * (cophis[i]/coheight);
For uniform distribution you have to change line 35, you have to pick more points towards the base of the cone, because the bounding circle gets bigger. We had a similar question about the distribution of points in a tube/circle, but i can't find it anymore. Could be something like this:
Copy code
  1. float u = random(1)+random(1);
    if(u > 1) u = 2 - u;
    cophis[i] = u * coheight;