Pages of Codes / Rvb-Processing / Back

------------------------------------------

multiple species

pr002

import processing.opengl.*;
import processing.dxf.*;
boolean record = false;

mFlo mflo;
Targets[] targets;
Drops[] drops;
Beach beach;

float angle;
float angleZ;
//float scala=5.0;
float scala=3.0;
float tr=0.75;
int numD=55000;
int currD=0;
int numT=10;
int currT=0;
Vector3D goal1=new Vector3D(20,-20,-10); //first target

//float[][]gcds={{1.0,0.1,0.1,0.9,0.1,3,3},{1.0,0.1,0.1,0.9,0.1,3,3},{1.0,0.1,0.1,0.9,0.1,3,3},{1.0,0.1,0.1,0.9,0.1,3,3},{1.0,0.1,0.1,0.9,0.1,3,3},{1.0,0.1,0.1,0.9,0.1,3,3}};
float[][]gcds={{0.5,0.1,0.1,0.5,0.1,3,5},{0.5,0.1,0.1,0.5,0.1,3,5},{0.5,0.1,0.1,0.5,0.1,3,5},{0.5,0.1,0.1,0.5,0.1,3,5},{0.5,0.1,0.1,0.5,0.1,3,5},{0.5,0.1,0.1,0.5,0.1,3,5}};
//float[][]gcds={{0.1,0.1,0.9,0.9,0.1,7,7},{0.1,0.1,0.9,0.9,0.1,7,7},{0.1,0.1,0.9,0.9,0.1,7,7},{0.1,0.1,0.9,0.9,0.1,7,7},{0.1,0.1,0.9,0.9,0.1,7,7},{0.1,0.1,0.9,0.9,0.1,7,7}};
//float[][]gcds={{0.3,0.9,0.1,0.5,0.5,7,5},{0.3,0.9,0.1,0.5,0.5,7,5},{0.3,0.9,0.1,0.5,0.5,7,5},{0.3,0.9,0.1,0.5,0.5,7,5},{0.3,0.9,0.1,0.5,0.5,7,5},{0.3,0.9,0.1,0.5,0.5,7,5}};
//float[][]gcds={{0.1,0.9,0.9,0.9,0.9,15,3},{0.1,0.9,0.9,0.9,0.9,15,3},{0.1,0.9,0.9,0.9,0.1,15,3},{0.1,0.9,0.9,0.9,0.1,15,3},{0.1,0.9,0.9,0.9,0.1,15,3},{0.1,0.9,0.9,0.9,0.1,15,3}};
//float[][]gcds={{0.1,0.9,0.9,0.9,0.9,15,3},{0.3,0.9,0.1,0.5,0.5,3,5},{0.1,0.1,0.9,0.9,0.1,13,13},{0.5,0.1,0.1,0.5,0.1,3,5},{1.0,0.1,0.1,0.9,0.1,3,3},{0.1,0.9,0.9,0.9,0.1,15,3}};

void setup(){
//size(3307,2339,OPENGL);
size(1280,800,OPENGL);
smooth();
mflo=new mFlo();
drops=new Drops[numD];
for (int i=0;i<numD;i++){
drops[i]=new Drops();
}

targets= new Targets[numT];
for(int i=0; i<numT; i++){
targets[i]=new Targets();
}

beach=new Beach();
beach.makeB();
beach.makeW();

for (int i = 0; i <4; i++) {
mflo.addMonkk(new Monkk(new Vector3D(-50,beach.giveY(-50,-50),-50),1.5f,0.05f, goal1, gcds[0],color(random(10,100),130,50)));
}

for (int i = 0; i <4; i++) {
mflo.addMonkk(new Monkk(new Vector3D(-80,beach.giveY(-80,-40),-40),1.5f,0.05f, goal1, gcds[1],color(random(10,100),110,10)));
}

for (int i = 0; i <4; i++) {
mflo.addMonkk(new Monkk(new Vector3D(-20,beach.giveY(-20,-80),-80),1.5f,0.05f, goal1, gcds[2],color(random(10,100),110,0)));
}

for (int i = 0; i <4; i++) {
mflo.addMonkk(new Monkk(new Vector3D(-40,beach.giveY(-40,-60),-60),1.5f,0.05f, goal1, gcds[3],color(random(10,100),210,0)));
}

for (int i = 0; i <4; i++) {
mflo.addMonkk(new Monkk(new Vector3D(-40,beach.giveY(-40,-100),-100),1.5f,0.05f, goal1, gcds[4],color(random(10,100),121,5)));
}

for (int i = 0; i <4; i++) {
mflo.addMonkk(new Monkk(new Vector3D(-100,beach.giveY(-100,-10),-10),1.5f,0.05f, goal1, gcds[5],color(random(10,100),110,50)));
}

}

void draw(){
if (record == true) {
beginRaw(DXF, “output.dxf”); // Start recording to the file
}
//saveFrame(“ani/prev-####.png”);
if(keyPressed&&key==’o'||key==’O'){
saveFrame(“screens/screen-####.png”);
}
background(255);

if(keyPressed&&key==’q'||key==’Q'){
scala+=0.1;
}
if(keyPressed&&key==’a'||key==’A'){
scala-=0.1;
}
if(keyPressed&&key==’w'||key==’W'){
tr+=0.004;
}
if(keyPressed&&key==’s'||key==’S'){
tr-=0.004;
}
if(keyPressed&&key==’e'||key==’E'){
angleZ += 0.02;
}
if(keyPressed&&key==’d'||key==’D'){
angleZ -= 0.02;
}
if(mousePressed&&mouseButton==LEFT){
angle -= 0.02;
}
if(mousePressed&&mouseButton==RIGHT){
angle += 0.02;
}

translate(width/2, height*tr, 0);
scale(scala);
rotateY(angle);
rotateX(angleZ);

for(int i=0;i<currD;i++){
drops[i].display();
}

if(keyPressed&&key==’1′){
mflo.run(1);
}
//lights();
beach.displayB();
beach.displayW();
mflo.display();
for(int i=0; i<numT; i++){
targets[i].display();
}

if (record == true) {
endRaw();
record = false; // Stop recording to the file
}

}

void keyPressed() {
if (key == ‘R’ || key == ‘r’) { // Press R to save the file
record = true;
}
if (key == ‘T’ || key == ‘t’) { // Press R to save the file
if (currT==numT){
currT=0;
}
targets[currT].add(random(-100,100),random(-100,0),random(-100,100),random(10,100));
mflo.changeTarget(new Vector3D(targets[currT].x,targets[currT].y,targets[currT].z));
currT++;
}
}

class Beach{
int det=100;
float[][][]coordB =new float[det][det][3];
float[][][]coordW =new float[det][det][3];

void makeB(){
for (int i=0;i<det;i++){
for (int j=0;j<det;j++){
coordB[i][j][0]=-2*det+i*4;
coordB[i][j][1]=100-pow(fuzzyDist(dist(i,0,j,0,0,0),1,70)*8,2)-pow(fuzzyDist(dist(i,0,j,10,0,60),10,80)*7,2)-pow(fuzzyDist(dist(i,0,j,70,0,-60),10,100)*6,2)-fuzzyDist(dist(i,0,j,70,0,-60),50,100)*sin(i-j*i)-fuzzyDist(dist(i,0,j,-10,0,60),70,100)*cos(j);
coordB[i][j][2]=-2*det+j*4;
}
}
}

void makeW(){
for (int i=0;i<det;i++){
for (int j=0;j<det;j++){
coordW[i][j][0]=-200+i*4;
coordW[i][j][1]=0.5*(sin(i-j)+cos(j));
coordW[i][j][2]=-200+j*4;
}
}
}

float giveY(float cx, float cz){
float cy=0;
for (int i=1;i<det;i++){
for (int j=1;j<det;j++){
if ((coordB[i][j][0]-cx<1)&&(coordB[i][j][2]-cz<1)){
cy= coordB[i][j][1];
}
}
}
return cy;
}

void displayB(){
strokeWeight(1);
stroke(255);
fill(230);
strokeWeight(0.1);
beginShape(TRIANGLES);
for (int i=1;i<det;i++){
for (int j=1;j<det;j++){
//line(coord[i][j][0],coord[i][j][1],coord[i][j][2],coord[i][j][0]+0.1,coord[i][j][1]+0.1,coord[i][j][2]+0.1);
vertex(coordB[i][j][0],coordB[i][j][1],coordB[i][j][2]);
vertex(coordB[i][j-1][0],coordB[i][j-1][1],coordB[i][j-1][2]);
vertex(coordB[i-1][j][0],coordB[i-1][j][1],coordB[i-1][j][2]);

vertex(coordB[i][j-1][0],coordB[i][j-1][1],coordB[i][j-1][2]);
vertex(coordB[i-1][j][0],coordB[i-1][j][1],coordB[i-1][j][2]);
vertex(coordB[i-1][j-1][0],coordB[i-1][j-1][1],coordB[i-1][j-1][2]);
}
}
endShape();

}
void displayW(){
strokeWeight(1);
stroke(0,20);
noFill();
strokeWeight(0.1);
beginShape(TRIANGLES);
for (int i=1;i<det;i++){
for (int j=1;j<det;j++){
//line(coord[i][j][0],coord[i][j][1],coord[i][j][2],coord[i][j][0]+0.1,coord[i][j][1]+0.1,coord[i][j][2]+0.1);
vertex(coordW[i][j][0],coordW[i][j][1],coordW[i][j][2]);
vertex(coordW[i][j-1][0],coordW[i][j-1][1],coordW[i][j-1][2]);
vertex(coordW[i-1][j][0],coordW[i-1][j][1],coordW[i-1][j][2]);

vertex(coordW[i][j-1][0],coordW[i][j-1][1],coordW[i][j-1][2]);
vertex(coordW[i-1][j][0],coordW[i-1][j][1],coordW[i-1][j][2]);
vertex(coordW[i-1][j-1][0],coordW[i-1][j-1][1],coordW[i-1][j-1][2]);
}
}
endShape();

}
}

class Drops{
float[][]coord =new float[4][3];
color kolor;
void add(float xs,float  ys,float zs,float xe,float ye,float ze, float ag, float ar, color k){
kolor=k;
//amount=fuzzyDist(dist(loc.x,loc.y,loc.z,go.x,go.y,go.z),1,100);
for(int i = 3;i>0;i–){
coord[i][0]=coord[i-1][0];
coord[i][1]=coord[i-1][1];
coord[i][2]=coord[i-1][2];
}
coord[0][0]=xs;
coord[0][1]=ys;
coord[0][2]=zs;

for(int i = 3;i>0;i–){
coord[i][0]=coord[i-1][0];
coord[i][1]=coord[i-1][1];
coord[i][2]=coord[i-1][2];
}
coord[0][0] = xs + ag*(0.33 * (xe-xs))+ar*(random(-5,5));
coord[0][1] = ys + ag*(pow(0.33, 3) * (ye-ys))+ar*(random(-5,5));
coord[0][2] = zs + ag*(0.33 * (ze-zs))+ar*(random(-5,5));

for(int i = 3;i>0;i–){
coord[i][0]=coord[i-1][0];
coord[i][1]=coord[i-1][1];
coord[i][2]=coord[i-1][2];
}
coord[0][0] = xs + ag*(0.77 * (xe-xs))+ar*(random(-5,5));
coord[0][1] = ys + ag*(pow(0.77, 3) * (ye-ys))+ar*(random(-5,5));
coord[0][2] = zs + ag*(0.77 * (ze-zs))+ar*(random(-5,5));

for(int i = 3;i>0;i–){
coord[i][0]=coord[i-1][0];
coord[i][1]=coord[i-1][1];
coord[i][2]=coord[i-1][2];
}
coord[0][0]=xe;
coord[0][1]=ye;
coord[0][2]=ze;
}

float[] giveCoo(float cx, float cy, float cz){
float[] coo={coord[0][0],coord[0][1],coord[0][2]};
for (int i=1;i<4;i++){
if (dist(cx,cy,cz,coord[i][0],coord[i][1],coord[i][2])<dist(cx,cy,cz,coord[0][0],coord[0][1],coord[0][2])){
coo=coord[i];
}
}
return coo;
}

void display(){
strokeWeight(2);
noFill();
stroke(kolor,60);
for(int i =3; i <4;i+=4){
bezier(coord[i][0],coord[i][1],coord[i][2],coord[i-1][0],coord[i-1][1],coord[i-1][2],coord[i-2][0],coord[i-2][1],coord[i-2][2],coord[i-3][0],coord[i-3][1],coord[i-3][2]);
}

}
}

float fuzzyDist(float dis, int ix, float iy){
float result=0;
float d=dis;
//range
int u1=ix;
float u2=iy;
if(d<=u1){
result=0;
}
else if(d>=u2){
result=1;
}
else{
result=(d /(u2-u1))-(u1/(u2-u1));
}
return result;

}

int nID(float sx,float sy,float sz){
int id=0;
float di = dist(sx,sy,sz,drops[0].coord[1][0],drops[0].coord[1][1],drops[0].coord[1][2]);
for(int i=0;i<currD;i++){
if(dist(sx,sy,sz,drops[i].coord[1][0],drops[0].coord[1][1],drops[0].coord[1][2])<di){
id=i;
}
}
return id;
}

class mFlo{
ArrayList mo;

mFlo() {
mo = new ArrayList();
}

void run(int swi) {
for (int i = 0; i < mo.size(); i++) {
Monkk m = (Monkk) mo.get(i);
m.run(mo,swi);
m.setTheGoal();
}
}

void display(){
for (int i = 0; i < mo.size(); i++) {
Monkk m = (Monkk) mo.get(i);
if (m.on==true){
m.render();
}
}
}

void addMonkk(Monkk m) {
mo.add(m);
}

void changeTarget( Vector3D t){
for (int i = 0; i < mo.size(); i++) {
Monkk m = (Monkk) mo.get(i);
m.newGoal(t);
}
}

}

class Monkk {
int num=1000;
float[]x =new float[num];
float[]y =new float[num];
float[]z =new float[num];
Vector3D loc;
Vector3D vel;
Vector3D acc;
Vector3D go;
float r;
float maxforce;
float maxspeed;
color kolor;
int bCount=0;
int bc=0;
int dc=0;
float amount;
float amountR;
boolean on=false;
float[] gCode;

Monkk(Vector3D l, float ms, float mf, Vector3D g, float[] gc, color c) {
acc = new Vector3D(0,0,0);
vel = new Vector3D(random(-0.5,0.5),random(-0.5,0.5),random(-0.5,0.5));
go=g;
x[0]=l.x;
y[0]=l.y;
z[0]=l.z;
gCode=gc;
loc = l.copy();
r = 2.0f;
maxspeed = ms;
maxforce = mf;
kolor=c;
}

void newGoal(Vector3D g){
go=g;
}

void setTheGoal(){
if (dist(loc.x,loc.y,loc.z,go.x,go.y,go.z)<12){
if(random(0,10)>9){
if (currT==numT){
currT=0;
}
if (loc.y<-50){
float ran=random(-55,55);
targets[currT].add(go.x+ran,beach.giveY(go.x+ran,go.z+ran),go.z+ran,random(10,100));
currT++;
}
else{
targets[currT].add(go.x+random(-12,12),go.y+random(-20,-10),go.z+random(-12,12),random(10,100));
currT++;
}
}
}
}

void run(ArrayList mo, int sw) {
flock(mo);
update();
if (sw==1){

behave(mo);
}

}

void flock(ArrayList mo) {
Vector3D sep = separate(mo);   // Separation
Vector3D ali = align(mo);      // Alignment
Vector3D coh = cohesion(mo);   // Cohesion
sep.mult(2.0f);
ali.mult(1.0f);
coh.mult(1.0f);
acc.add(sep);
acc.add(ali);
acc.add(coh);

}

void update(){
on=true;
for(int i = num-1;i>0;i–){
x[i]=x[i-1];
y[i]=y[i-1];
z[i]=z[i-1];
}
}

void behave(ArrayList mo ) {
amount=(gCode[0]+fuzzyDist(dist(loc.x,loc.y,loc.z,go.x,go.y,go.z),1,100))/2;
x[0]= amount*(x[1]+(-cos(atan2(loc.y-go.y,loc.x-go.x))))+(1-amount)*loc.x-(cos(bCount)*gCode[1]*0.3)-(random(-1,1)*gCode[2]*0.3);
y[0]= amount*(y[1]+(-sin(atan2(loc.y-go.y,loc.x-go.x))))+(1-amount)*loc.y-(sin(bCount)*gCode[1]*0.3)-(random(-1,1)*gCode[2]*0.3);
z[0]= amount*(z[1]+(-cos(atan2(loc.y-go.y,loc.z-go.z))))+(1-amount)*loc.z-(sin(bCount)*gCode[1]*0.3)-(random(-1,1)*gCode[2]*0.3);
if (y[0]>beach.giveY(x[0],z[0])){
y[0]=beach.giveY(x[0],z[0])-1;
}
loc.x=x[0];
loc.y=y[0];
loc.z=z[0];

if(bCount%gCode[5]==1){

for (int i = 0 ; i < mo.size(); i++) {
Monkk other = (Monkk) mo.get(i);
float d = loc.distance(loc,other.loc);
if ((d > 5) && (d < 10)) {
if (currD==numD){
currD=0;
}
if(random(0,10)>3){
if(y[0]<beach.giveY(loc.x,loc.z)){
drops[currD].add(x[0],y[0],z[0],other.x[0],other.y[0],other.z[0],gCode[3],gCode[4],kolor);
currD++;
}
}
}
}
}

if(bCount%gCode[5]==1){
int nearDiD=nID(loc.x,loc.y,loc.z);
float d = dist(loc.x,loc.y,loc.z, drops[nearDiD].giveCoo(loc.x,loc.y,loc.z)[0],drops[nearDiD].giveCoo(loc.x,loc.y,loc.z)[1],drops[nearDiD].giveCoo(loc.x,loc.y,loc.z)[2]);
if ((d > 5) && (d < 15)) {
if (currD==numD){
currD=0;
}
if(random(0,10)>3){
if(y[0]<beach.giveY(loc.x,loc.z)){
drops[currD].add(x[0],y[0],z[0],drops[nearDiD].giveCoo(loc.x,loc.y,loc.z)[0],drops[nearDiD].giveCoo(loc.x,loc.y,loc.z)[1],drops[nearDiD].giveCoo(loc.x,loc.y,loc.z)[2],gCode[3],gCode[4],kolor);
currD++;
}
}
}

}

if(bCount%gCode[6]==1&&loc.y>beach.giveY(loc.x,loc.z)-12){

if (currD==numD){
currD=0;
}
for (int i = 0 ; i < 2; i++) {
if(y[0]<beach.giveY(loc.x,loc.z)){
float di=dist(loc.x,loc.y,loc.z,loc.x,beach.giveY(loc.x,loc.z),loc.z);
float ra =random(-di,di);
drops[currD].add(loc.x,loc.y,loc.z,loc.x-ra,beach.giveY(loc.x+ra,loc.z+ra),loc.z-ra,gCode[3],gCode[4],kolor);
currD++;
}

}
}

vel.add(acc);
vel.limit(maxspeed);
loc.add(vel);
acc.setXYZ(0,0,0);
bCount++;

}

void seek(Vector3D target) {
acc.add(steer(target,false));
}

void arrive(Vector3D target) {
acc.add(steer(target,true));
}

Vector3D steer(Vector3D target, boolean slowdown) {
Vector3D steer;
Vector3D desired = target.sub(target,loc);
float d = desired.magnitude();
if (d > 0) {
desired.normalize();
if ((slowdown) && (d < 100.0f)) desired.mult(maxspeed*(d/100.0f));
else desired.mult(maxspeed);
steer = target.sub(desired,vel);
steer.limit(maxforce);
} else {
steer = new Vector3D(0,0,0);
}
return steer;
}

void render() {

noFill();
stroke(kolor,50);
strokeWeight(2);
beginShape();
for(int i =0; i <num;i++){
vertex(x[i],y[i],z[i]);

}
endShape();

}

// Separation
Vector3D separate (ArrayList mo) {
float desiredseparation = 0.5f;
Vector3D sum = new Vector3D(0,0,0);
int count = 0;
for (int i = 0 ; i < mo.size(); i++) {
Monkk other = (Monkk) mo.get(i);
float d = loc.distance(loc,other.loc);
if ((d > 0) && (d < desiredseparation)) {
Vector3D diff = loc.sub(loc,other.loc);
diff.normalize();
diff.div(d);
sum.add(diff);
count++;
}
}
if (count > 0) {
sum.div((float)count);
}
return sum;
}

// Alignment
Vector3D align (ArrayList mo) {
float neighbordist = 1.0f;
Vector3D sum = new Vector3D(0,0,0);
int count = 0;
for (int i = 0 ; i < mo.size(); i++) {
Monkk other = (Monkk) mo.get(i);
float d = loc.distance(loc,other.loc);
if ((d > 0) && (d < neighbordist)) {
sum.add(other.vel);
count++;
}
}
if (count > 0) {
sum.div((float)count);
sum.limit(maxforce);
}
return sum;
}

// Cohesion
Vector3D cohesion (ArrayList mo) {
float neighbordist = 50.0f;
Vector3D sum = new Vector3D(0,0,0);
int count = 0;
for (int i = 0 ; i < mo.size(); i++) {
Monkk other = (Monkk) mo.get(i);
float d = loc.distance(loc,other.loc);
if ((d > 0) && (d < neighbordist)) {
sum.add(other.loc);
count++;
}
}
if (count > 0) {
sum.div((float)count);
return steer(sum,false);
}
return sum;
}

}

class Targets{
float x;
float y;
float z;
float range;
boolean on=false;

void add(float ax, float ay, float az, float r){
x=ax;
y=ay;
z=az;
range=r;
on=true;
mflo.changeTarget(new Vector3D(targets[currT].x,targets[currT].y,targets[currT].z));
}

void display(){
if (on==true){
pushMatrix();
translate(x,y,z);
fill(255,0,0,50);
noStroke();
sphere(1);
popMatrix();
}
}
}
static class Vector3D {
float x;
float y;
float z;

Vector3D(float x_, float y_, float z_) {
x = x_; y = y_; z = z_;
}

Vector3D(float x_, float y_) {
x = x_; y = y_; z = 0f;
}

Vector3D() {
x = 0f; y = 0f; z = 0f;
}

void setX(float x_) {
x = x_;
}

void setY(float y_) {
y = y_;
}

void setZ(float z_) {
z = z_;
}

void setXY(float x_, float y_) {
x = x_;
y = y_;
}

void setXYZ(float x_, float y_, float z_) {
x = x_;
y = y_;
z = z_;
}

void setXYZ(Vector3D v) {
x = v.x;
y = v.y;
z = v.z;
}

float magnitude() {
return (float) Math.sqrt(x*x + y*y + z*z);
}

Vector3D copy() {
return new Vector3D(x,y,z);
}

Vector3D copy(Vector3D v) {
return new Vector3D(v.x, v.y,v.z);
}

void add(Vector3D v) {
x += v.x;
y += v.y;
z += v.z;
}

void sub(Vector3D v) {
x -= v.x;
y -= v.y;
z -= v.z;
}

void mult(float n) {
x *= n;
y *= n;
z *= n;
}

void div(float n) {
x /= n;
y /= n;
z /= n;
}

void normalize() {
float m = magnitude();
if (m > 0) {
div(m);
}
}

void limit(float max) {
if (magnitude() > max) {
normalize();
mult(max);
}
}

float heading2D() {
float angle = (float) Math.atan2(-y, x);
return -1*angle;
}

Vector3D add(Vector3D v1, Vector3D v2) {
Vector3D v = new Vector3D(v1.x + v2.x,v1.y + v2.y, v1.z + v2.z);
return v;
}

Vector3D sub(Vector3D v1, Vector3D v2) {
Vector3D v = new Vector3D(v1.x – v2.x,v1.y – v2.y,v1.z – v2.z);
return v;
}

Vector3D div(Vector3D v1, float n) {
Vector3D v = new Vector3D(v1.x/n,v1.y/n,v1.z/n);
return v;
}

Vector3D mult(Vector3D v1, float n) {
Vector3D v = new Vector3D(v1.x*n,v1.y*n,v1.z*n);
return v;
}

float distance (Vector3D v1, Vector3D v2) {
float dx = v1.x – v2.x;
float dy = v1.y – v2.y;
float dz = v1.z – v2.z;
return (float) Math.sqrt(dx*dx + dy*dy + dz*dz);
}

}

 

V+J 17XI_Main

Option Explicit
‘Script written by <insert name>
‘Script copyrighted by <insert company name>
‘Script version 11 listopada 2008 12:07:51

Call Main()
Sub Main()
 Dim aPts(),strB
 Dim i
 Dim aPtTemp
 Dim aPtsNeighbors
 Dim nMin : nMin = -2
 Dim nMax: nMax =2
 Dim dblSearchRad: dblSearchRad =1
 Dim aA
 
 ReDim Preserve aPts(0)
 
 aPts(0) = rhino.GetPoint(“pick seed”)
 aA = rhino.GetPoint(“get Attractor”)
 strB = rhino.getobject(“border”,8+16+32)
 
 For i=1 To 20
  
  Do
   aPtTemp = array(aPts(i-1)(0)+ eitherOr(nMin,nMax),_                     
    aPts(i-1)(1)+eitherOr(nMin,nMax),_                                                  
    aPts(0)(2))
 
   ” check its status
   If i > 1 Then
    aPtsNeighbors = searchWithinRad(aPtTemp, aPts, dblSearchRad)
    If Not isnull (aPtsNeighbors) Then
     Dim z : z = heighestZ(aPtsNeighbors)
     aPtTemp = array(aPts(i-1)(0)+ eitherOr(nMin,nMax),_
      aPts(i-1)(1)+eitherOr(nMin, nMax),_
      z+0.5)
    End If
   

   End If
  
   ” collect
   ReDim Preserve aPts(i)
   aPts(i) = aPtTemp
  
   ” Attraction,Probability
   DispAtr aA,i,aPts,aPtTemp,dblSearchRad,52
   
   ” Robot Steps
   RobotFoot i,aPts,aPtTemp,aA
  
   If  Not rhino.IsPointInSurface(strB,aPts(i))= True Then Exit Do
  
  Loop
  
  ” vizu
  ’Dim scircle : sCircle = rhino.AddCircle(rhino.MovePlane(Rhino.WorldXYPlane,aPts(i)),dblSearchRad)
  ’Call rhino.AddPlanarSrf(array(scircle))
  ’rhino.AddSphere aPts(i),dblSearchRad*RN(1,3)
 
 Next
 Dim strTrack
 ’Call rhino.addpolyline(aPts)
 ’strTrack = rhino.addinterpcurve(aPts)
End Sub
Function RN(nMin, nMax)
 Randomize
 RN = (nMax – nMin) * Rnd + nMin
End Function
Function eitherOr(n1, n2)
 Randomize
 If rnd > 0.5 Then
  eitherOr = n1
 Else
  eitherOr = n2
 End If
End Function
Function searchWithinRad(aPtSeed, aPts, dblRad)
 Dim i, dblDist, n
 Dim aPtscollect()
 For i = 0 To UBound(aPts)
  ’dblDist = rhino.Distance(aPtSeed, aPts(i))
  dblDist = rhino.Distance(aPtSeed, array(aPts(i)(0),aPts(i)(1),aPtSeed(2)))
  If dblDist < dblRad Then
   ReDim Preserve aPtscollect(n)
   aPtscollect(n) = aPts(i)
   ’Call rhino.addPoint(aPtscollect(n))
   n = n + 1
  End If
 Next
 If n = 0 Then
  searchWithinRad = Null
 Else
  searchWithinRad = aPtscollect
 End If
End Function
Function heighestZ(aPts)
 Dim i
 Dim z : z = 0
 For i = 0 To Ubound(aPts)
  If aPts(i)(2) > z Then z = aPts(i)(2)
 Next
 heighestZ = z
End Function
Function DispAtr(aA,i,aPts,aPtTemp,dblSearchRad,dblChance)
 Dim la ,lb, lc, ld
 Dim arrla, arrlb, alfa, R
 Dim cTL, cPL
 Dim CrossSectionPlane
 Dim aPtsNeighbors
 ’do 
 la=Rhino.addline(aPts(i-1),aA)
 lb = rhino.AddLine(aPts(i-1),Rhino.EvaluateCurve (la, rhino.distance(aPts(i-1),aPtTemp)))
 R=rhino.distance(aPts(i-1),aPtTemp)
 
 cPL = Array(0,1,0)
 cTL = Rhino.CurveTangent(la,0)
  
 CrossSectionPlane = Rhino.PlaneFromFrame(aPts(i-1),cTL ,cPL)
 ’Rhino.ViewCPlane, Rhino.PlaneFromFrame(aPts(i-1), cTL, cPL)
  
 lc=rhino.RotateObject (lb,aPts(i-1),eitherOr(-1,1)*RN(0,dblChance)*R,,True)   
 aPts(i) = rhino.curveendpoint(lc)

 If i > 1 Then
  aPtsNeighbors = searchWithinRad(aPtTemp, aPts, dblSearchRad)
  If Not isnull (aPtsNeighbors) Then
   Dim z : z = heighestZ(aPtsNeighbors)
  End If
  aPts(i) = array(rhino.curveendpoint(lc)(0),rhino.curveendpoint(lc)(1),z+0.5)
 End If  
 
  
 rhino.Objectlayer la,”la”
 rhino.Objectlayer lb,”lb”
 rhino.Objectlayer lc,”lc”

 ’If  rhino.IsPointInSurface(strB,aPts(i))= True Then Exit Do  
 ’Loop
 
End Function
Function RobotFoot(i,aPts,aPtTemp,aA)
 Dim y,x
 Dim A, o
 Dim ld: ld=rhino.AddLine(aPts(i-1),aPts(i))
 Dim cTL, cPL,CrossSectionPlane
 Dim pi: pi=rhino.PI
 Dim d: d=int(rhino.Distance(aPts(i-1),aPts(i)))
 Dim aPtsR(),aPtsL()
 
 ReDim Preserve aPtsR(d)
 ReDim Preserve aPtsL(d)
 o=1

 cPL = Array(0,1,0)
 cTL = Rhino.CurveTangent(ld,0)
 CrossSectionPlane = Rhino.PlaneFromFrame(aPts(i-1),cTL,cPL)
 ’Rhino.ViewCPlane, Rhino.PlaneFromFrame(aPts(i-1),cTL,cPL)
  
 
 For x=1 To d
  
  A= RN(0.1,0.2)
  
  y=A*sin(o*x*(pi/2))
  
  If y>0.1 Then
  
   aPtsR(x)= rhino.PointAdd (aPts(i-1),array(x,y,0))
   aPtsR(x)=rhino.addpoint(aPtsR(x))
   rhino.objectlayer aPtsR(x), “Rfoot”
   
  End If
  
  If y<-0.1 Then

   aPtsL(x)= rhino.PointAdd(aPts(i-1),array(x,y,0))
   aPtsL(x)=rhino.addpoint(aPtsL(x))
   rhino.objectlayer aPtsL(x), “Lfoot”
  
  End If
  rhino.Objectlayer ld,”ld”
 Next 

 ’rhino.AddPolyline aPtsR
 ’rhino.AddPolyline aPtsL

End Function

17X_Atr/Rep

atrrep2atrrep013atrrep031atrrep044

 

 

 

Option Explicit
‘Script written by <insert name>
‘Script copyrighted by <insert company name>
‘Script version 11 listopada 2008 12:07:51

Call Main()
Sub Main()
 Dim aPts(),strB
 Dim i
 Dim aPtTemp
 Dim aPtsNeighbors
 Dim nMin : nMin = -2
 Dim nMax: nMax =2
 Dim dblSearchRad: dblSearchRad =1
 Dim aA
 
 ReDim Preserve aPts(0)
 
 aPts(0) = rhino.GetPoint(“pick seed”)
 aA = rhino.GetPoint(“get Attractor”)
 strB = rhino.getobject(“border”,8+16+32)
 
 For i=1 To 20
  
  Do
   aPtTemp = array(aPts(i-1)(0)+ eitherOr(nMin,nMax),_                     
    aPts(i-1)(1)+eitherOr(nMin,nMax),_                                                  
    aPts(0)(2))
 
   ” check its status
   If i > 1 Then
    aPtsNeighbors = searchWithinRad(aPtTemp, aPts, dblSearchRad)
    If Not isnull (aPtsNeighbors) Then
     Dim z : z = heighestZ(aPtsNeighbors)
     aPtTemp = array(aPts(i-1)(0)+ eitherOr(nMin,nMax),_
      aPts(i-1)(1)+eitherOr(nMin, nMax),_
      z+0.5)
    End If
   

   End If
  
   ” collect
   ReDim Preserve aPts(i)
   aPts(i) = aPtTemp
  
   ” Attraction,Probability
   DispAtr aA,i,aPts,aPtTemp,dblSearchRad,52
   
   ” Robot Steps
   RobotFoot i,aPts,aPtTemp,aA
  
   If  Not rhino.IsPointInSurface(strB,aPts(i))= True Then Exit Do
  
  Loop
  
  ” vizu
  ’Dim scircle : sCircle = rhino.AddCircle(rhino.MovePlane(Rhino.WorldXYPlane,aPts(i)),dblSearchRad)
  ’Call rhino.AddPlanarSrf(array(scircle))
  ’rhino.AddSphere aPts(i),dblSearchRad*RN(1,3)
 
 Next
 Dim strTrack
 ’Call rhino.addpolyline(aPts)
 ’strTrack = rhino.addinterpcurve(aPts)
End Sub
Function RN(nMin, nMax)
 Randomize
 RN = (nMax – nMin) * Rnd + nMin
End Function
Function eitherOr(n1, n2)
 Randomize
 If rnd > 0.5 Then
  eitherOr = n1
 Else
  eitherOr = n2
 End If
End Function
Function searchWithinRad(aPtSeed, aPts, dblRad)
 Dim i, dblDist, n
 Dim aPtscollect()
 For i = 0 To UBound(aPts)
  ’dblDist = rhino.Distance(aPtSeed, aPts(i))
  dblDist = rhino.Distance(aPtSeed, array(aPts(i)(0),aPts(i)(1),aPtSeed(2)))
  If dblDist < dblRad Then
   ReDim Preserve aPtscollect(n)
   aPtscollect(n) = aPts(i)
   ’Call rhino.addPoint(aPtscollect(n))
   n = n + 1
  End If
 Next
 If n = 0 Then
  searchWithinRad = Null
 Else
  searchWithinRad = aPtscollect
 End If
End Function
Function heighestZ(aPts)
 Dim i
 Dim z : z = 0
 For i = 0 To Ubound(aPts)
  If aPts(i)(2) > z Then z = aPts(i)(2)
 Next
 heighestZ = z
End Function
Function DispAtr(aA,i,aPts,aPtTemp,dblSearchRad,dblChance)
 Dim la ,lb, lc, ld
 Dim arrla, arrlb, alfa, R
 Dim cTL, cPL
 Dim CrossSectionPlane
 Dim aPtsNeighbors
 ’do 
 la=Rhino.addline(aPts(i-1),aA)
 lb = rhino.AddLine(aPts(i-1),Rhino.EvaluateCurve (la, rhino.distance(aPts(i-1),aPtTemp)))
 R=rhino.distance(aPts(i-1),aPtTemp)
 
 cPL = Array(0,1,0)
 cTL = Rhino.CurveTangent(la,0)
  
 CrossSectionPlane = Rhino.PlaneFromFrame(aPts(i-1),cTL ,cPL)
 ’Rhino.ViewCPlane, Rhino.PlaneFromFrame(aPts(i-1), cTL, cPL)
  
 lc=rhino.RotateObject (lb,aPts(i-1),eitherOr(-1,1)*RN(0,dblChance)*R,,True)   
 aPts(i) = rhino.curveendpoint(lc)

 If i > 1 Then
  aPtsNeighbors = searchWithinRad(aPtTemp, aPts, dblSearchRad)
  If Not isnull (aPtsNeighbors) Then
   Dim z : z = heighestZ(aPtsNeighbors)
  End If
  aPts(i) = array(rhino.curveendpoint(lc)(0),rhino.curveendpoint(lc)(1),z+0.5)
 End If  
 
  
 rhino.Objectlayer la,”la”
 rhino.Objectlayer lb,”lb”
 rhino.Objectlayer lc,”lc”

 ’If  rhino.IsPointInSurface(strB,aPts(i))= True Then Exit Do  
 ’Loop
 
End Function

17X_RobotSteps

 

robotsteps

Option Explicit
‘Script written by <insert name>
‘Script copyrighted by <insert company name>
‘Script version 16 listopada 2008 16:02:02

Call Main()
Sub Main()
 Dim y,x
 Dim A, o
 Dim pi: pi=rhino.PI
 Dim aPtsR(50),aPtsL(50)

 
 o=1
 
 For x=0 To 50
 
  A= RN(1,10)
  
  y=A*sin(o*x*(pi/2))
  
  If y>0.1 Then
  
   aPtsR(x)= rhino.addpoint(array(x,y,0))
   rhino.objectlayer aPtsR(x), “Rfoot”
   aPtsR(x)=rhino.PointCoordinates(rhino.addpoint(array(x,y,0)))
  End If
  If y<-0.1 Then

   aPtsL(x)= rhino.addpoint(array(x,y,0))
   rhino.objectlayer aPtsL(x), “Lfoot”
   aPtsL(x)=rhino.PointCoordinates(rhino.addpoint(array(x,y,0)))
  End If
 
 Next 

 ’rhino.AddPolyline aPtsR
 ’rhino.AddPolyline aPtsL
End Sub
Function RN(nMin, nMax)
 Randomize
 RN = (nMax – nMin) * Rnd + nMin
End Function

 

17X_Repulsion

 

rep

Option Explicit
‘Script written by <insert name>
‘Script copyrighted by <insert company name>
‘Script version 7 listopada 2008 12:38:46

Call Main()
Sub Main()
 
 Dim i,aPtTemp, aPtsNeighbors:
 Dim aPts(),arrPtsG
 Dim strB
 Dim arrRep,j
 Dim nMin : nMin = -5
 Dim nMax: nMax =5
 Dim dblSearchRad: dblSearchRad =5
 Dim a,b
 

 ReDim Preserve aPts(0)
 
 aPts(0) = rhino.GetPoint(“pick one pt”)
 strB = rhino.getobject(“border”,8+16+32)

 arrRep = rhino.getobjects(“repulsors”,8+16+32)

 For i = 1 To 50
  Do 
   a=1
   b=1
  
   For j=0 To ubound(arrRep)
    If  rhino.IsPointInSurface(strB,aPts(i-1))= True And rhino.IsPointInSurface(arrRep(j),aPts(i-1))= True Then
     a=RN(0.5,0.2)
     b=1 
    End If 
   Next
   ” temp/generic 
   aPtTemp = array(aPts(i-1)(0)+ (RN(nMin/a, nMax/a)),_                     
    aPts(i-1)(1)+RN(nMin/b, nMax/b),_                                                  
    aPts(0)(2))
   ” if atr, rep, neutral
   
   
   ” check its status
   If i > 1 Then
    aPtsNeighbors = searchWithinRad(aPtTemp, aPts, dblSearchRad)
    If Not isnull (aPtsNeighbors) Then
     Dim z : z = heighestZ(aPtsNeighbors)
     aPtTemp = array(aPts(i-1)(0)+ eitherOr(nMin/a,nMax/a),_
      aPts(i-1)(1)+eitherOr(nMin/b, nMax/b),_
      z+0.5)
    End If
   
   End If
   ” collect
   ReDim Preserve aPts(i)
   
   aPts(i) = aPtTemp
  
   If  rhino.IsPointInSurface(strB,aPts(i))= True Then Exit Do
   
    
  Loop 
  
  ” vizu
  ’Dim scircle : sCircle = rhino.AddCircle(rhino.MovePlane(Rhino.WorldXYPlane,aPts(i)),dblSearchRad)
  ’Call rhino.AddPlanarSrf(array(scircle))
 
 Next
 
 Dim strTrack
 Call rhino.addpolyline(aPts)
 
 strTrack = rhino.addinterpcurve(aPts)

 
End Sub

Function RN(nMin, nMax)
 Randomize
 RN = (nMax – nMin) * Rnd + nMin
End Function
Function eitherOr(n1, n2)
 Randomize
 If rnd > 0.5 Then
  eitherOr = n1
 Else
  eitherOr = n2
 End If
End Function

Function searchWithinRad(aPtSeed, aPts, dblRad)
 Dim i, dblDist, n
 Dim aPtscollect()
 For i = 0 To UBound(aPts)
  ’dblDist = rhino.Distance(aPtSeed, aPts(i))
  dblDist = rhino.Distance(aPtSeed, array(aPts(i)(0),aPts(i)(1),aPtSeed(2)))
  If dblDist < dblRad Then
   ReDim Preserve aPtscollect(n)
   aPtscollect(n) = aPts(i)
   Call rhino.addPoint(aPtscollect(n))
   n = n + 1
  End If
 Next
 If n = 0 Then
  searchWithinRad = Null
 Else
  searchWithinRad = aPtscollect
 End If
End Function

Function heighestZ(aPts)
 Dim i
 Dim z : z = 0
 For i = 0 To Ubound(aPts)
  If aPts(i)(2) > z Then z = aPts(i)(2)
 Next
 heighestZ = z
End Function

V+M

01_getMagneticFieldVector

////////MAGNETIC FIELD DRAW///////////////////////////////////////////////////////
///PROC GET DIRMAGSUM VECTOR
global proc vector getSum (string $test11, string $charges[])
{
$k= 2;
float $dis;
vector $vadd,$vcharg;
global vector $dirMagSum;
global vector $test;

$charge0=$charges[0];
vector $dirMagSum = <<0,0,0>>;

for ($charge0 in $charges)
{
float $chargeLoad= `getAttr ($charge0+”.scaleX”)`;
//distance to charge
$test= `pointPosition $test11`;
$vcharg= `pointPosition $charge0`;
$vadd= $vcharg-$test;
$dis=mag($vadd);

//get force
$dpow= `pow $dis 2`;
$Force=$k*$chargeLoad/$dpow;

//create vector of direction and force
$dirMag= ($vcharg+(-$test))*$Force;
$dirMagSum= $dirMagSum+$dirMag;
}

return $dirMagSum;
return $test;
}
///END

 

G+D material behaviour.offseting tubes[no physics]

screen-1051

import processing.opengl.*;
import processing.dxf.*;
boolean record = false;

tube[] T;
int steps=17;
int segments = 7;
float x, y, z, px, py, pz,px1,py1,pz1,px2,py2,pz2;

Beach beach;
float angle;
float angleZ;
float scala=25.0;
float tr=0.75;
int turnC=1;
int tCount=0;
int swi=steps/2;//round(random(0,10));

void setup(){
//size(3307,2339,OPENGL);
//size(800,600,OPENGL);
size(1280,800,OPENGL);
smooth();
T=new tube[0];
beach= new Beach();
beach.makeB();

}

void draw(){
if (record == true) {
beginRaw(DXF, “output.dxf”);
}
//saveFrame(“ani/prev-####.png”);
if(keyPressed&&key==’o'||key==’O'){
saveFrame(“screens/screen-####.png”);
}
background(255);
lights();
if(keyPressed&&key==’q'||key==’Q'){
scala+=0.4;
}
if(keyPressed&&key==’a'||key==’A'){
scala-=0.4;
}
if(keyPressed&&key==’w'||key==’W'){
tr-=0.004;
}
if(keyPressed&&key==’s'||key==’S'){
tr+=0.004;
}
if(keyPressed&&key==’e'||key==’E'){
angleZ += 0.02;
}
if(keyPressed&&key==’d'||key==’D'){
angleZ -= 0.02;
}
if(keyPressed&&key==’y'||key==’Y'){
if(swi>0){
swi–;
}
}
if(keyPressed&&key==’u'||key==’U'){
if(swi<steps){
swi++;
}

}
if(mousePressed&&mouseButton==LEFT){
angle -= 0.03;
}
if(mousePressed&&mouseButton==RIGHT){
angle += 0.03;
}

if(keyPressed&&key==’h'||key==’H'){

px = random(-5,5);
py =random(-5,2);
pz = random(-5,5);
px1=px+random(-2,2);
py1=py+random(-2,2);
pz1=pz+random(-2,2);
px2=px1+random(-2,2);
py2=py1+random(-2,2);
pz2=pz1+random(-2,2);
x =px2+ random(-5,5);
y =py2+ random(-5,5);
z =pz2+ random(-5,5);

T = append(T,new tube(new Vector(px,py,pz),new Vector(px1,py1,pz1),new Vector(px2,py2,pz2), new Vector(x,y,z),  color(random(100,150),random(150,250),random(100,150)) ));
tCount++;
}

if(keyPressed&&key==’j'||key==’J'){
if (tCount>0){
T = append(T,new tube(T[tCount-1].giveProfile(turnC)[0],T[tCount-1].giveProfile(turnC)[1],T[tCount-1].giveProfile(turnC)[2],T[tCount-1].giveProfile(turnC)[3],  color(random(100,150),random(150,250),random(100,150)) ));
tCount++;
}
}

if(keyPressed&&key==’k'||key==’K'){
if (tCount>0){
x = T[tCount-1].giveProfile(turnC)[3].x+random(-1,1);
y = T[tCount-1].giveProfile(turnC)[3].y+random(-2,0);
z = T[tCount-1].giveProfile(turnC)[3].z+random(-1,1);

T = append(T,new tube(T[tCount-1].giveProfile(turnC)[1],T[tCount-1].giveProfile(turnC)[2],T[tCount-1].giveProfile(turnC)[3], new Vector(x,y,z),  color(random(100,150),random(150,250),random(100,150)) ));
tCount++;
}
}

if(keyPressed&&key==’l'||key==’L'){
if (tCount>0){
T = append(T,new tube(T[tCount-1].giveProfile(turnC)[0],T[tCount-1].giveProfile(turnC)[3],T[tCount-1].giveProfile(),  color(random(100,150),random(150,250),random(100,150)) ));
tCount++;
}
}

translate(width/2, height*tr, 0);
scale(scala);
rotateY(angle);
rotateX(angleZ);
// beach.displayB();
for(int i=0;i<T.length;i++)
{

T[i].display();
}

if (record == true) {
endRaw();
record = false;
}

}

void keyPressed() {
if (key == ‘R’ || key == ‘r’) {
record = true;
}

}
float fuzzyDist(float dis, float ix, float iy){
float result=0;
float d=dis;
//range
float u1=ix;
float u2=iy;
if(d>=u1){
result=0;
}
else if(d<=u2){
result=1;
}
else{
result=(d /(u2-u1))-(u1/(u2-u1));
}
return result;

}

class Beach{
int det=100;
float[][][]coordB =new float[det][det][3];

void makeB(){
for (int i=0;i<det;i++){
for (int j=0;j<det;j++){
coordB[i][j][0]=-2*det+i*4;
coordB[i][j][1]=0;
coordB[i][j][2]=-2*det+j*4;
}
}
}
void displayB(){
noStroke();
fill(230);
strokeWeight(1);
beginShape(TRIANGLES);
for (int i=1;i<det;i++){
for (int j=1;j<det;j++){
//line(coord[i][j][0],coord[i][j][1],coord[i][j][2],coord[i][j][0]+0.1,coord[i][j][1]+0.1,coord[i][j][2]+0.1);
vertex(coordB[i][j][0],coordB[i][j][1],coordB[i][j][2]);
vertex(coordB[i][j-1][0],coordB[i][j-1][1],coordB[i][j-1][2]);
vertex(coordB[i-1][j][0],coordB[i-1][j][1],coordB[i-1][j][2]);

vertex(coordB[i][j-1][0],coordB[i][j-1][1],coordB[i][j-1][2]);
vertex(coordB[i-1][j][0],coordB[i-1][j][1],coordB[i-1][j][2]);
vertex(coordB[i-1][j-1][0],coordB[i-1][j-1][1],coordB[i-1][j-1][2]);
}
}
endShape();
}

}
class Quad
{
Vector[] v;
color col;
Quad(Vector[] _v, color _col)
{
v=_v;
col=_col;
}

Quad(Vector a, Vector b, Vector c, Vector d, color _col)
{
v=new Vector[4];
v[0]=a;
v[1]=b;
v[2]=c;
v[3]=d;
col=_col;
}

void draw()
{
beginShape(QUADS);
fill(col,85);
stroke(40);
//noStroke();

{
vertex(v[0].x,v[0].y,v[0].z);
vertex(v[1].x,v[1].y,v[1].z);
vertex(v[2].x,v[2].y,v[2].z);
vertex(v[3].x,v[3].y,v[3].z);
}
endShape();
}
}

class tube
{
Vector root, end, mid1, mid2, roott,eend;
Vector[][] a=new Vector[steps+1][4];
Quad[][] polys;
float r1,r2,p;
int crvMidPtId1=round(steps/3);
int crvMidPtId2=round(2*steps/3);
float time=0.0;
color col;
float bpX[]=new float[steps+1];
float bpY[]=new float[steps+1];
float bpZ[]=new float[steps+1];

tube(Vector _root,Vector _mid1,Vector _mid2, Vector _end, color c){

polys=new Quad[steps][segments];
root=_root;
end=_end;
mid1=_mid1;
mid2=_mid2;

col=c;
giveCurves();
compile();
blow();
}

tube(Vector _root,Vector _end,Vector[] _edge, color c){

polys=new Quad[steps+1][segments];
Vector[] edge=_edge;
roott=_root;
eend=_end;
root=edge[0];
mid1=edge[crvMidPtId1];
mid2=edge[crvMidPtId2];
end=edge[steps-1];
col=c;
giveCurves(edge);
compile();
blow();
}

void giveCurves(){
for (int u=0;u<steps;u++){
float p = u / float(steps);
bpX[u]=bezierPoint(root.x,mid1.x,mid2.x,end.x,p);
bpY[u]=bezierPoint(root.y,mid1.y,mid2.y,end.y,p);
bpZ[u]=bezierPoint(root.z,mid1.z,mid2.z,end.z,p);
}
}

void giveCurves(Vector[] _edge){
Vector[] edge=_edge;
for (int u=0;u<steps;u++){
bpX[u]=edge[u].x;
bpY[u]=edge[u].y;
bpZ[u]=edge[u].z;
}

}

void compile()
{

for(int j=0;j<segments;j++)
{

for (int u=0;u<steps;u++){
float len1,len2;
Vector vec1,vec2,mid1,mid2;
float edst=end.distance(end,root)/2;
r1=0.05+0.2*fuzzyDist(dist(bpX[u],bpY[u],bpZ[u],bpX[swi],bpY[swi],bpZ[swi]),edst,0.01);
r2=0.05+0.2*fuzzyDist(dist(bpX[u+1],bpY[u+1],bpZ[u+1],bpX[swi],bpY[swi],bpZ[swi]),edst,0.01);

len1=sqrt((bpX[u]-bpX[u+1]) * (bpX[u]-bpX[u+1]) + (bpY[u]-bpY[u+1]) * (bpY[u]-bpY[u+1]) + (bpZ[u]-bpZ[u+1]) * (bpZ[u]-bpZ[u+1]))/2;
len2=sqrt((bpX[u+1]-end.x) * (bpX[u+1]-end.x) + (bpY[u+1]-end.y) * (bpY[u+1]-end.y) + (bpZ[u+1]-end.z) * (bpZ[u+1]-end.z))/2;

vec1=new Vector((bpX[u]-bpX[u+1]),(bpY[u]-bpY[u+1]),(bpZ[u]-bpZ[u+1]));
vec2=new Vector((bpX[u+1]-end.x),(bpY[u+1]-end.y),(bpZ[u+1]-end.z));

mid1=new Vector((bpX[u]+bpX[u+1])/2,(bpY[u]+bpY[u+1])/2,(bpZ[u]+bpZ[u+1])/2);
mid2=new Vector((bpX[u+1]+end.x)/2,(bpY[u+1]+end.y)/2,(bpZ[u+1]+end.z)/2);

if(u<steps-2){
len1=sqrt((bpX[u]-bpX[u+1]) * (bpX[u]-bpX[u+1]) + (bpY[u]-bpY[u+1]) * (bpY[u]-bpY[u+1]) + (bpZ[u]-bpZ[u+1]) * (bpZ[u]-bpZ[u+1]))/2;
len2=sqrt((bpX[u+1]-bpX[u+2]) * (bpX[u+1]-bpX[u+2]) + (bpY[u+1]-bpY[u+2]) * (bpY[u+1]-bpY[u+2]) + (bpZ[u+1]-bpZ[u+2]) * (bpZ[u+1]-bpZ[u+2]))/2;

vec1=new Vector((bpX[u]-bpX[u+1]),(bpY[u]-bpY[u+1]),(bpZ[u]-bpZ[u+1]));
vec2=new Vector((bpX[u+1]-bpX[u+2]),(bpY[u+1]-bpY[u+2]),(bpZ[u+1]-bpZ[u+2]));

mid1=new Vector((bpX[u]+bpX[u+1])/2,(bpY[u]+bpY[u+1])/2,(bpZ[u]+bpZ[u+1])/2);
mid2=new Vector((bpX[u+1]+bpX[u+2])/2,(bpY[u+1]+bpY[u+2])/2,(bpZ[u+1]+bpZ[u+2])/2);
}
float heading1=-atan2(vec1.z,vec1.x);
float heading2=-atan2(vec2.z,vec2.x);

float pitch1=atan2(vec1.y,sqrt((vec1.x*vec1.x)+(vec1.z*vec1.z)));
float pitch2=atan2(vec2.y,sqrt((vec2.x*vec2.x)+(vec2.z*vec2.z)));

float inc = TWO_PI/segments;
a[u][0]=new Vector(len1,r1*cos(j*inc),r1*sin(j*inc));
a[u][1]=new Vector(len2,r2*cos(j*inc),r2*sin(j*inc));
a[u][2]=new Vector(len2,r2*cos((j+1)*inc),r2*sin((j+1)*inc));
a[u][3]=new Vector(len1,r1*cos((j+1)*inc),r1*sin((j+1)*inc));

for(int i=0;i<a[u].length;i++)
{
if (i==1||i==2){
float _x=a[u][i].x;
float _y=a[u][i].y;
a[u][i].x=_x*cos(pitch2)+_y*cos(pitch2+HALF_PI);
a[u][i].y=_x*sin(pitch2)+_y*sin(pitch2+HALF_PI);

}

else{
float _x=a[u][i].x;
float _y=a[u][i].y;
a[u][i].x=_x*cos(pitch1)+_y*cos(pitch1+HALF_PI);
a[u][i].y=_x*sin(pitch1)+_y*sin(pitch1+HALF_PI);
}
}
for(int i=0;i<a[u].length;i++)
{
if (i==1||i==2){
float _z=a[u][i].z;
float _x=a[u][i].x;
a[u][i].z=_z*cos(heading2)+_x*cos(heading2+HALF_PI);
a[u][i].x=_z*sin(heading2)+_x*sin(heading2+HALF_PI);
}
else{
float _z=a[u][i].z;
float _x=a[u][i].x;
a[u][i].z=_z*cos(heading1)+_x*cos(heading1+HALF_PI);
a[u][i].x=_z*sin(heading1)+_x*sin(heading1+HALF_PI);
}
}

for(int i=0;i<a[u].length;i++)
{
if (i==1||i==2){
a[u][i].x+=mid2.x;
a[u][i].y+=mid2.y;
a[u][i].z+=mid2.z;
}
else{
a[u][i].x+=mid1.x;
a[u][i].y+=mid1.y;
a[u][i].z+=mid1.z;
}
}
polys[u][j]=new Quad(a[u][0],a[u][1],a[u][2],a[u][3],col);
}
}
}

void blow(){
Vector g= new Vector(0,0.01,0);

for(int j=0;j<segments;j++)
{
for (int u=0;u<steps;u++){
polys[u][j].v[0]=polys[u][j].v[0].add(polys[u][j].v[0],g);
polys[u][j].v[1]=polys[u][j].v[1].add(polys[u][j].v[1],g);
polys[u][j].v[2]=polys[u][j].v[2].add(polys[u][j].v[2],g);
polys[u][j].v[3]=polys[u][j].v[3].add(polys[u][j].v[3],g);
}
}
}

void display()
{
for(int i=0;i<steps-1;i++)
{
for(int j=0;j<segments;j++){
polys[i][j].draw();
}
}
}

Vector[] giveProfile(int par)
{
Vector[] profile=new Vector[4];
Vector add;
add =polys[crvMidPtId1][par].v[0].copy();
add.normalize();
add.mult(0.5);

profile[0]=root.add(add,root);
profile[1]=polys[crvMidPtId1][par].v[0].add(add,polys[crvMidPtId1][par].v[0]);
profile[2]=polys[crvMidPtId2][par].v[0].add(add,polys[crvMidPtId2][par].v[0]);
profile[3]=end.add(add,end);
return profile;
}

Vector[] giveProfile(){
Vector[] profile=new Vector[steps];
for (int i=0;i<steps;i++){
Vector off=new Vector(polys[i][1].v[0].x-bpX[i],polys[i][1].v[0].y-bpY[i],polys[i][1].v[0].z-bpZ[i]);
profile[i]=polys[i][1].v[0].add(polys[i][1].v[0],off);
}

return profile;
}

}

tube[] append(tube[] tarr, tube t)
{
tube[] tmp = new tube[tarr.length+1];
arraycopy(tarr,0,tmp,0,tarr.length);
tmp[tarr.length] = t;
return tmp;
}

static class Vector
{
float x,y,z;
Vector(float _x, float _y, float _z)
{
x=_x;
y=_y;
z=_z;
}
void setX(float x_) {
x = x_;
}

void setY(float y_) {
y = y_;
}

void setZ(float z_) {
z = z_;
}

void setXY(float x_, float y_) {
x = x_;
y = y_;
}

void setXYZ(float x_, float y_, float z_) {
x = x_;
y = y_;
z = z_;
}

void setXYZ(Vector v) {
x = v.x;
y = v.y;
z = v.z;
}

float magnitude() {
return (float) Math.sqrt(x*x + y*y + z*z);
}

Vector copy() {
return new Vector(x,y,z);
}

Vector copy(Vector v) {
return new Vector(v.x, v.y,v.z);
}

void add(Vector v) {
x += v.x;
y += v.y;
z += v.z;
}

void sub(Vector v) {
x -= v.x;
y -= v.y;
z -= v.z;
}

void mult(float n) {
x *= n;
y *= n;
z *= n;
}

void div(float n) {
x /= n;
y /= n;
z /= n;
}

void normalize() {
float m = magnitude();
if (m > 0) {
div(m);
}
}

void limit(float max) {
if (magnitude() > max) {
normalize();
mult(max);
}
}
Vector limit(Vector v1,float max) {
if (v1.magnitude() > max) {
v1.normalize();
v1.mult(max);
}
return v1;
}

float heading2D() {
float angle = (float) Math.atan2(-y, x);
return -1*angle;
}

Vector add(Vector v1, Vector v2) {
Vector v = new Vector(v1.x + v2.x,v1.y + v2.y, v1.z + v2.z);
return v;
}

Vector sub(Vector v1, Vector v2) {
Vector v = new Vector(v1.x – v2.x,v1.y – v2.y,v1.z – v2.z);
return v;
}

Vector div(Vector v1, float n) {
Vector v = new Vector(v1.x/n,v1.y/n,v1.z/n);
return v;
}

Vector mult(Vector v1, float n) {
Vector v = new Vector(v1.x*n,v1.y*n,v1.z*n);
return v;
}

float distance (Vector v1, Vector v2) {
float dx = v1.x – v2.x;
float dy = v1.y – v2.y;
float dz = v1.z – v2.z;
return (float) Math.sqrt(dx*dx + dy*dy + dz*dz);
}

}

 

181008_WeldedWire

aggreg

 

 

Option Explicit
Call Weld()

Sub Weld()

 Dim aSrfs : aSrfs = Rhino.GetObjects(“Select Surface”,
 
 Dim i,n
 Dim aPts()
 Dim udom, vdom, uparam, vparam, arrpt, udiv, vdiv
 
 udiv = 100
 vdiv = 50
 
 For i = 0 To UBound(aSrfs)
 
  n = 0
  Dim strsurface : strSurface = aSrfs(i)
 
  ReDim colec2d(udiv, vdiv)
 
  udom = Rhino.SurfaceDomain(strsurface,0)
  vdom = Rhino.SurfaceDomain(strsurface,1)
  
  Rhino.EnableRedraw False
  
  Do Until n = 1000
  
   uparam = udom(0) + rnd * (udom(1) – udom(0))
   vparam = vdom(0) + rnd * (vdom(1) – vdom(0))
  
   ReDim Preserve aPts(n)
   aPts(n) = Rhino.EvaluateSurface(strsurface, array(uparam, vparam))
   
   If n >= 1 Then
    Dim vDir : vDir = rhino.VectorCreate(aPts(n-1), aPts(n))
    vDir = rhino.VectorScale(rhino.VectorUnitize(vDir), 0.05)
    Dim aPtEnd : aPtEnd = rhino.PointAdd(aPts(n), vDir)
    Dim aParam : aParam = rhino.SurfaceClosestPoint(strSurface,aPtend)
   
    ’ curly
    ’Dim scrv : sCrv = Rhino.AddInterpCrvOnSrfUV (strSurface, array(array(uparam, vparam),aParam))
    ’ ribars
    Dim scrv : sCrv = Rhino.AddLine (aPts(n), rhino.EvaluateSurface(strSurface,aParam) )
   
    Call Rhino.ExtendCurveLength (sCrv, 0, 0, (RN(0.05, 0.2)))
    
   End If

   n = n + 1
    
  Loop
 
  Rhino.EnableRedraw True
 Next
End Sub
Function RN (nMin, nMax)
 RN = Null
 Randomize
 RN = (nMax – nMin) * Rnd + nMin
End Function

 

181008_squeezedLofts

squeez

 

 

Option Explicit

Call Worm()

Sub Worm()
 
 
 Dim aObjects : aObjects = Rhino.GetObjects(“Select Curves”, 4, True, False)
 ’Dim intSamples : intSamples = Rhino.GetInteger(“Number of Cross Sections”, 50, 5)
 
 Dim i
 Dim arrCrossSections()
 
 For i = 0 To UBound(aObjects)
  
  Dim crvObject : crvObject = aObjects(i)
  
  ’ define number of sections based on crv length
  Dim dblSegments : dblSegments = Rhino.DivideCurveLength ( aObjects(i), 1)
  Dim intSamples : intSamples = UBound(dblSegments)
  
 
  Dim crvDomain : crvDomain = Rhino.CurveDomain(crvObject)
  Dim t, n, l

  l = RN(51, 56)
 
  Dim CrossSectionPlane
  Dim crvCurvature, crvPoint, crvTangent, crvPerp, crvNormal
 
  n = -1

  For t = crvDomain(0) To crvDomain(1) + 1e-9 Step (crvDomain(1)-crvDomain(0))/intSamples

   Dim dblBendRadius : dblBendRadius = random(1,5)
   Dim dblPerpRadius : dblPerpRadius = random(1,5)

   n = n+1
 
   crvCurvature = Rhino.CurveCurvature(crvObject, t)

   If IsNull(crvCurvature) Then
  
    crvPoint = Rhino.EvaluateCurve(crvObject, t)
    crvTangent = Rhino.CurveTangent(crvObject, t)
    crvPerp = Array(0,0,1)
    crvNormal = Rhino.VectorCrossProduct(crvTangent, crvPerp)

   Else
  
    crvPoint = crvCurvature(0)
    crvTangent = crvCurvature(1)
    crvPerp = Rhino.VectorUnitize(crvCurvature(4))
    crvNormal = Rhino.VectorCrossProduct(crvTangent, crvPerp)

   End If
  
  
   CrossSectionPlane = Rhino.PlaneFromFrame(crvPoint, crvPerp, crvNormal)
   ReDim Preserve arrCrossSections(n)
   arrCrossSections(n) = Rhino.AddEllipse(CrossSectionPlane, dblBendRadius, dblPerpRadius)

  Next
 
  If n < 1 Then Exit Sub
 
  Call Rhino.AddLoftSrf(arrCrossSections)
  ’Call Rhino.DeleteObjects(arrCrossSections)
 
 Next

End Sub

 

Function random(intLow,intUp)
 
 Randomize
 
 random = (intUp-intLow) * Rnd() + intLow
 
End Function

Function RN(nMin, nMax)
 RN = Null
 If Not IsNumeric(nMin) Then Exit Function
 If Not IsNumeric(nMax) Then Exit Function
 If nMin >= nMax Then Exit Function
 Randomize
 RN = Int((nMax – nMin + 1) * Rnd + nMin)
End Function

 

181008_ CurveLayering_Ao

icon4

 

Option Explicit

Call Main()
Sub Main()

 Dim i, IntUpper : IntUpper = 500     ’numbers of steps
 
 Dim aPtCenter : aptcenter = array(0,0,0)
 Dim dblLength : dblLength = 42
 
 Dim n : n = 0
 Dim aCenterTemp
 Dim sCrv, sCrv2, svCrv, dsvCrv, dsCrv2
 Dim vCrv, dvCrv
 Dim sVec, dsVec
 Dim sVecSc, dsVecSc
 Dim aPtEnd(),daPtEnd
 Dim vPt
 Dim arrPlane
 Dim arrPointa()
 Dim arrCross()
 Dim m
 Dim f:f = 2            ’multi_Factor
 
 ’Rhino.EnableRedraw False
 
 
 
  For i = 0 To IntUpper
  ’*RN(1,1.1)
  ReDim Preserve arrPointa(i)
  
 
  arrPointa(i) = Array(0+5*Sin(i*RN(1,1.1))*f,_
   0+5*cos(i*RN(1,1.1))*f,_
   0+i)
 
  Call rhino.addpoint(arrPointa(i))
  
  aCenterTemp = array(aptcenter(0),aptcenter(1),arrPointa(i)(2))
 
  
  ’blue
  If i > 1 And i Mod 2 = 0 Then
   sCrv = rhino.AddCurve(array(arrPointa(i),aCenterTemp,arrPointa(i-2)))
   Call rhino.ObjectColor(sCrv, vbblue)
   Call Rhino.ExtendCurveLength (sCrv, 1, 0, dblLength/4)
 
  End If
 
  
  
  
  
  ’_________drop arc___________
  
  ’darkred     ___ex-centalazation
  If i Mod 2 = 1 Then
   ’unitize
   dvCrv = Rhino.VectorCreate (arrPointa(i),aCenterTemp)
    
   dsVec = Rhino.VectorUnitize (dvCrv)
   dsVecSc = Rhino.VectorScale (dsVec, RN(1, 2)*10*f)  ‘new lengthe ext _(0.25) 
   ’line
   dsvCrv = rhino.addline(aCenterTemp, dsVecSc)
   Call rhino.ObjectColor(dsvCrv, RGB(191,0,0))
   ReDim Preserve aPtEnd(n)
   aPtEnd(n) = rhino.CurveEndPoint(dsvCrv)
  
      
   ’darkgreen_arc
   If n > 0 Then
    dsCrv2 = rhino.AddInterpCurve(array(aPtEnd(n-1) , arrPointa(i-1),aPtEnd(n)))
    Call rhino.ObjectColor(dsCrv2, RGB(0,127,0))
    ’Call Rhino.ExtendCurveLength (dsCrv2, 2, 0, dblLength/1)
  
   End If
 
  End If
  
  ’_____________
  
   ’red     ___ex-centalazation
   If i Mod 2 = 1 Then
   ’unitize
   vCrv = Rhino.VectorCreate (arrPointa(i),aCenterTemp)
   ’vCrv = Rhino.VectorCreate (aCenterTemp, arrPointa(i))
   
   rhino.Print vCrv(2)
   sVec = Rhino.VectorUnitize (vCrv)
   sVecSc = Rhino.VectorScale (sVec, RN(1, 1.5)*10*f)  ‘new lengthe ext _(0.25)
   vPt = Rhino.VectorAdd (aCenterTemp, sVecSc)
   ’line
   svCrv = rhino.addline(aCenterTemp, vPt)
   Call rhino.ObjectColor(svCrv, vbRed)
   ReDim Preserve aPtEnd(n)
    aPtEnd(n) = rhino.CurveEndPoint(svCrv)  
    
    
   ’green
   If n > 0 Then
    sCrv2 = rhino.AddInterpCurve(array(aPtEnd(n-1) , arrPointa(i-1),aPtEnd(n) ))
    Call rhino.ObjectColor(sCrv2, vbGreen)
    Call Rhino.ExtendCurveLength (sCrv2, RN(1,2), 1, dblLength/2)
  
   End If
  
   n = n + 1
  
   End If
  
 Next
 ’Rhino.AddCurve (arrPointa)
 ’Rhino.AddPolyline (arrPointa)
 
 ’Rhino.EnableRedraw True
 
 
End Sub

 

 
Function RN(nMin, nMax)
 RN = Null
 Randomize
 RN = (nMax – nMin) * Rnd + nMin
End Function

 

--------------------------------------------------

Pages of Codes / Rvb-Processing / Back