找回密码
 注册
搜索
查看: 64643|回复: 40

假前最后一贴,极其变态的voronoi求法

[复制链接]
发表于 2010-6-30 01:11:01 | 显示全部楼层 |阅读模式
本帖最后由 panhao1 于 2010-6-30 01:31 编辑

先看官网的processing代码  3L是我修改了的代码 揭露原理
1.jpg
int NUM_POINTS = 12;
float x[];  // point x
float y[];  // point y
float av[]; // point angular velocity
color c[];  // point color

void setup() {
  size(400,400, P3D);

  // FIXME: ortho seems to be a bit strange, I am investigating
  ortho(-width/2,width/2,-height/2,height/2,-10,10);

  x = new float[NUM_POINTS];
  y = new float[NUM_POINTS];
  av = new float[NUM_POINTS];
  c = new color[NUM_POINTS];

  for (int i = 0; i < NUM_POINTS; i++) {
    x = random(width);
    y = random(height);
    av = random(-0.02,0.02);
    c = color(random(255),random(255),random(255));
  }

}

void draw() {

  background(0);

  // draw the cones
  pushMatrix();
  // -10 means the cones are below the surface
  // (so we can draw on top of them later)
  translate(0,0,-10);
  for (int i = 0; i < NUM_POINTS; i++) {
    fill(c);
    noStroke();
    // if you have more points, use a smaller radius
    // (but you risk having gaps)
    cone(x,y,200.0,10.0);
  }
  popMatrix();

  // give user control of first point
  x[0] = mouseX;
  y[0] = mouseY;

  // on mouse pressed, orbit the points around the centre of the screen
  if (mousePressed) {  
    for (int i = 1; i < NUM_POINTS; i++) {
      float r = dist(width/2,height/2,x,y);
      float a = atan2(y-(height/2),x-(width/2)) + av;
      x = (width/2) + (r * cos(a));
      y = (height/2) + (r * sin(a));
    }
  }

  // draw dots for the points
  for (int i = 0; i < NUM_POINTS; i++) {
    fill(0);
    noStroke();
    ellipse(x,y,5.0,5.0);
  }

}

static float unitConeX[];
static float unitConeY[];
static int coneDetail;

static {
  coneDetail(24);
}

// just inits the points of a circle,
// if you're doing lots of cones the same size
// then you'll want to cache height and radius too
static void coneDetail(int det) {
  coneDetail = det;
  unitConeX = new float[det+1];
  unitConeY = new float[det+1];
  for (int i = 0; i <= det; i++) {
    float a1 = TWO_PI * i / det;
    unitConeX = (float)Math.cos(a1);
    unitConeY = (float)Math.sin(a1);
  }
}

// places a cone with it's base centred at (x,y),
// beight h in positive z, radius r.
void cone(float x, float y, float r, float h) {
  pushMatrix();
  translate(x,y);
  scale(r,r);
  beginShape(TRIANGLES);
  for (int i = 0; i < coneDetail; i++) {
    vertex(unitConeX,unitConeY,0.0);
    vertex(unitConeX[i+1],unitConeY[i+1],0.0);
    vertex(0,0,h);
  }
  endShape();
  popMatrix();
}

评分

参与人数 1强度 +7 照度 +80 收起 理由
skywoolf + 7 + 80 很有启发

查看全部评分

 楼主| 发表于 2010-6-30 01:15:20 | 显示全部楼层
本帖最后由 panhao1 于 2010-6-30 01:29 编辑

全段代码并不是求voronoi
而是画圆锥体 然后通过圆锥体的重叠来制造假voronoi
这里按住鼠标 那些椎体会移动 top视图感觉就是voronoi在动

虽然是假的voronoi 但还是很有趣 唯一的缺点是有时候会出现无法覆盖的黑色区域



3L是我修改后的代码
让它转起来
哈哈 真相大白啦

如果是专业卡的机子 ,用OPENGL比P3D效果更好
回复

使用道具 举报

 楼主| 发表于 2010-6-30 01:26:33 | 显示全部楼层
2.jpg
int NUM_POINTS = 12;
float x[];  // point x
float y[];  // point y
float av[]; // point angular velocity
color c[];  // point color
float time;
void setup() {
  size(400,400, P3D);

  // FIXME: ortho seems to be a bit strange, I am investigating
  //ortho(-width/2,width/2,-height/2,height/2,-10,10);

  x = new float[NUM_POINTS];
  y = new float[NUM_POINTS];
  av = new float[NUM_POINTS];
  c = new color[NUM_POINTS];

  for (int i = 0; i < NUM_POINTS; i++) {
    x = random(width);
    y = random(height);
    av = random(-0.02,0.02);
    c = color(random(255),random(255),random(255));
  }

}

void draw() {
   background(0);
  // draw the cones
  pushMatrix();
  // -10 means the cones are below the surface
  // (so we can draw on top of them later)
  //translate(0,0,-10);
  translate(200,200,-10);
   //真相
  rotateX(time);
  rotateY(time*2);
  time+=0.01;
//真相
  for (int i = 0; i < NUM_POINTS; i++) {
    fill(c);
    noStroke();
    // if you have more points, use a smaller radius
    // (but you risk having gaps)
    cone(x,y,200.0,100.0);
  }
  popMatrix();

  // give user control of first point
  x[0] = mouseX;
  y[0] = mouseY;

  // on mouse pressed, orbit the points around the centre of the screen
  if (mousePressed) {  
    for (int i = 1; i < NUM_POINTS; i++) {
      float r = dist(width/2,height/2,x,y);
      float a = atan2(y-(height/2),x-(width/2)) + av;
      x = (width/2) + (r * cos(a));
      y = (height/2) + (r * sin(a));
    }
  }

  // draw dots for the points
  for (int i = 0; i < NUM_POINTS; i++) {
    fill(0);
    noStroke();
    ellipse(x,y,5.0,5.0);
  }

}

static float unitConeX[];
static float unitConeY[];
static int coneDetail;

static {
  coneDetail(24);
}

// just inits the points of a circle,
// if you're doing lots of cones the same size
// then you'll want to cache height and radius too
static void coneDetail(int det) {
  coneDetail = det;
  unitConeX = new float[det+1];
  unitConeY = new float[det+1];
  for (int i = 0; i <= det; i++) {
    float a1 = TWO_PI * i / det;
    unitConeX = (float)Math.cos(a1);
    unitConeY = (float)Math.sin(a1);
  }
}

// places a cone with it's base centred at (x,y),
// beight h in positive z, radius r.
void cone(float x, float y, float r, float h) {
  pushMatrix();
  translate(x,y);
  scale(r,r);
  beginShape(TRIANGLES);
  for (int i = 0; i < coneDetail; i++) {
    vertex(unitConeX,unitConeY,0.0);
    vertex(unitConeX[i+1],unitConeY[i+1],0.0);
    vertex(0,0,h);
  }
  endShape();
  popMatrix();
}
回复

使用道具 举报

发表于 2010-6-30 01:39:43 | 显示全部楼层
写得太好了~!就是看不懂啊~等待高手回帖
回复

使用道具 举报

发表于 2010-6-30 06:23:53 | 显示全部楼层
{:3_52:}犀利了~不过还有没理解为什么会是圆锥,调节圆锥的z轴难道可以做加权的voronoi?想到圆锥不同纬度上半径增加速度有比例关系的。
回复

使用道具 举报

发表于 2010-6-30 10:50:40 | 显示全部楼层
弱弱地问下,这是什么代码?
回复

使用道具 举报

发表于 2010-6-30 11:07:50 | 显示全部楼层
好久不用C++了,看了楼主的代码既亲切又陌生,呵呵!
顶一下楼主!这个领域大有作为!

顺便问句:楼主是学计算机的吗?编程水平高不可攀……佩服啊!
回复

使用道具 举报

 楼主| 发表于 2010-6-30 13:27:22 | 显示全部楼层
7# 夜神

这是processing的脚本 其实是java
和C++还是差别蛮大的 但是相比.net要小一些
回复

使用道具 举报

 楼主| 发表于 2010-6-30 13:29:38 | 显示全部楼层
本帖最后由 panhao1 于 2010-6-30 13:39 编辑

5# skywoolf

可以copy 1L和3L的代码到processing里面试试嘛

因为voronoi近似于泡泡相切 所以圆锥体相切也可以达到相同的目的

processing官网有下载 最好下1.1版的,java版的功能更强 也就60M左右
它有很多库 大部分是开源 帮助理解算法 前提是能看得懂java,java相对VB要简单很多,注:processing做生成计算速度接近C++,因为即使OPENGL再简单,相对于我们非计算机专业还是显得麻烦,用processing的P3D和OPENGL就简单很多,教程也是满天飞,数量几乎可以和PS教程数量相比,虽然processing做3d模型还是麻烦了点,但是默认灯光实时渲染的质量接近SU,至少可以出图。processing的openGL是支持nurbs的。关键就是processing的计算速度快,又是面向对象的。相对于用rs做人流分析,用processing更加简化,无论从代码还是表现角度。
回复

使用道具 举报

发表于 2010-6-30 14:11:14 | 显示全部楼层
早就应该讲上面这些了。天天开潘搞这些,都不知道优势劣势在哪。最后决定还是RS吧。
回复

使用道具 举报

 楼主| 发表于 2010-6-30 20:03:41 | 显示全部楼层
10# 3828669

rs面向对象能力实在太差,做不了复杂点的东西,而且函数变量也太笼统,用起来很不爽的说,好在比mel要清晰一些。processing也比较清晰,本想号召大家整理一些常用库的说,看来没太大希望了,看来快速入门教程也没太大必要整理了吧~ 建筑用的人太少了

要是processing有适合自己的常用库,用起来就很方便了

评分

参与人数 1强度 +1 照度 +3 收起 理由
s.k. + 1 + 3 我觉得processing还是应该搞起来,以浅显易 ...

查看全部评分

回复

使用道具 举报

发表于 2010-7-1 10:05:48 | 显示全部楼层
请教下各位高手为何会出现cannot convert from float to float【】??
回复

使用道具 举报

 楼主| 发表于 2010-7-1 13:17:41 | 显示全部楼层
本帖最后由 panhao1 于 2010-7-1 13:26 编辑

12# vamkiss

少了【i】 复制粘贴弄不上去 中括号里面加 i

你把报错的地方加上就可以了

sketch_jul01a.pde

2.51 KB, 下载次数: 25, 下载积分: 照度 -1 lux

回复

使用道具 举报

发表于 2010-7-4 20:35:51 | 显示全部楼层
看不懂,过来支持一下。
回复

使用道具 举报

发表于 2010-7-6 18:08:17 | 显示全部楼层
好复杂啊。。。。。
回复

使用道具 举报

发表于 2010-7-11 12:42:30 | 显示全部楼层
不错    学习中~~~~~~~~~~~~
回复

使用道具 举报

发表于 2010-7-11 16:41:54 | 显示全部楼层
貌似很复杂啊
回复

使用道具 举报

发表于 2010-7-16 11:56:36 | 显示全部楼层
看不懂,好神奇
回复

使用道具 举报

发表于 2010-8-11 14:44:49 | 显示全部楼层
看不懂,十分神奇。。。这个要花多久才能学会啊??
编程貌似很难啊。。
回复

使用道具 举报

发表于 2010-9-9 17:13:40 | 显示全部楼层
逐行理解中
回复

使用道具 举报

NCFZ微信公众号

手机版|小黑屋|NCF参数化建筑论坛 ( 浙ICP备2020044100号-2 ) 辽公网安备21021102000973号

GMT+8, 2026-12-3 17:27 , Processed in 0.059641 second(s), 28 queries , Gzip On.

Powered by Discuz! X5.0 Licensed

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表