About "if" in C# and Python

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 a = new List();
A = recursiveLine(line, 10, a);

method

public List recursiveLine(Line line, int depth, List 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.

They give me different result.
I can’t get the right answer in C#.

I’m confuse now.
Is it because “if” structure ?

Thank you!

a quick view, but in Python, you have two if statements together, both will be checked but in C# you have an “if” followed by and “if else” if the first is a true statement the second one won’t run, while in python you have two if statements and both will be checked regardless of each other.

you might also want to use the Out keyword for the list, you will get similar behaviour to Python.

https://www.dotnetperls.com/out

thank you so much!
now I know what’s problem with my code.
use void method, and it solve this.

1 Like

it would be great to post the answer for future reference :slight_smile:

Below is someone told me. It works!

public void recursiveLine(Line line, int depth, List 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)
  {
    recursiveLine(line1, depth - 1, resultList);
  }
  if(random.NextDouble() < 0.8)
  {
    recursiveLine(line2, depth - 1, resultList);
  }

}

}