Grasshopper

algorithmic modeling for Rhino

Python:

import rhinoscriptsyntax as rs
import Rhino.Geometry as rg
import random

def recursiveLine(line, depth, resultList):
    pt1 = line.PointAt(0)
    pt2 = line.PointAt(1)
    dir1 = rg.Vector3d(pt2.X-pt1.X, pt2.Y-pt1.Y, pt2.Z-pt1.Z)
    dir2 = rg.Vector3d(dir1) # copy

    dir1.Rotate(random.random()*0.4+0.1, rg.Vector3d.ZAxis) # random angle
    dir2.Rotate(random.random()*-0.4-0.1, rg.Vector3d.ZAxis)
    dir1 *= random.random()*0.2 + 0.8; # random scale
    dir2 *= random.random()*0.2 + 0.8;
    line1 = rg.Line(pt2, pt2+dir1)
    line2 = rg.Line(pt2, pt2+dir2)
    resultList.append(line1)
    resultList.append(line2)
    if(depth>0):
        if(random.random()<0.9): # random omission
            recursiveLine(line1, depth-1, resultList)
        if(random.random()<0.8): # random omission
            recursiveLine(line2, depth-1, resultList)

# main
a =[]
recursiveLine(line, 10,a)

I translate this python script to C#:

#main

 List<Line> a = new List<Line>();
    A = recursiveLine(line, 10, a);

#method

 public List<Line> recursiveLine(Line line, int depth, List<Line> resultList)
  {
    Point3d pt1 = line.PointAt(0);
    Point3d pt2 = line.PointAt(1);
    Vector3d dir1 = new Vector3d(pt2.X - pt1.X, pt2.Y - pt1.Y, pt2.X - pt2.Y);
    Vector3d dir2 = new Vector3d(dir1);//copy

    Random random = new Random(Guid.NewGuid().GetHashCode());
    dir1.Rotate(random.NextDouble() * 0.4 + 0.1, Rhino.Geometry.Vector3d.ZAxis);
    dir2.Rotate(random.NextDouble() * -0.4 - 0.1, Rhino.Geometry.Vector3d.ZAxis);
    dir1 *= random.NextDouble() * 0.2 + 0.8; //random scale
    dir2 *= random.NextDouble() * 0.2 + 0.8;

    Line line1 = new Line(pt2, pt2 + dir1);
    Line line2 = new Line(pt2, pt2 + dir2);

    resultList.Add(line1);
    resultList.Add(line2);

    if(depth > 0)
    {

      if(random.NextDouble() < 0.9)
      {
        return recursiveLine(line1, depth - 1, resultList);
      }
      else if(random.NextDouble() < 0.8)
      {
        return recursiveLine(line2, depth - 1, resultList);
      }
      else
      {
        return resultList;
      }
    }
    else
    {
      return resultList;
    }


  }

They give me different result.

I'm confuse now.

Views: 475

Attachments:

Replies to This Discussion

it said:

0. Error (CS0161): 'Script_Instance.recursiveLine(Rhino.Geometry.Line, int, System.Collections.Generic.List<Rhino.Geometry.Line>)': not all code paths return a value (line 76)

public void recursiveLine(Line line, int depth, List<Line> resultList)

{

}

I feel so sorry.

it said:


0. Error (CS0127): Since 'Script_Instance.recursiveLine(Rhino.Geometry.Line, int, System.Collections.Generic.List<Rhino.Geometry.Line>)' returns void, a return keyword must not be followed by an object expression (line 100)
1. Error (CS0127): Since 'Script_Instance.recursiveLine(Rhino.Geometry.Line, int, System.Collections.Generic.List<Rhino.Geometry.Line>)' returns void, a return keyword must not be followed by an object expression (line 104)

I don't know what happened about your code!

But see attached!

Attachments:

Oh............thank you so much!

RSS

About

Translate

Search

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service