1

I'm trying to port some AS3 code to C#(.NET) the majority of it has been done (90%) however I have run into a few problems in terms of Functions in Functions and functions being defined as Functions (I hope i'm understanding it correctly). I have done a lot of searching and the main thing that comes up is delegates and lambda's however trying to implement them is proving difficult for me to do. Seen as quiet a few sections are the same in layout ill just post a generic example of the AS3 code and hopefully can then apply any solution to the rest.

Here is the AS3 code:

  static public function makeRadial(seed:int):Function {
    var islandRandom:PM_PRNG = new PM_PRNG();
    islandRandom.seed = seed;
    var bumps:int = islandRandom.nextIntRange(1, 6);
    var startAngle:Number = islandRandom.nextDoubleRange(0, 2*Math.PI);
    var dipAngle:Number = islandRandom.nextDoubleRange(0, 2*Math.PI);
    var dipWidth:Number = islandRandom.nextDoubleRange(0.2, 0.7);

    function inside(q:Point):Boolean {
      var angle:Number = Math.atan2(q.y, q.x);
      var length:Number = 0.5 * (Math.max(Math.abs(q.x), Math.abs(q.y)) + q.length);

      var r1:Number = 0.5 + 0.40*Math.sin(startAngle + bumps*angle + Math.cos((bumps+3)*angle));
      var r2:Number = 0.7 - 0.20*Math.sin(startAngle + bumps*angle - Math.sin((bumps+2)*angle));
      if (Math.abs(angle - dipAngle) < dipWidth
          || Math.abs(angle - dipAngle + 2*Math.PI) < dipWidth
          || Math.abs(angle - dipAngle - 2*Math.PI) < dipWidth) {
        r1 = r2 = 0.2;
      }
      return  (length < r1 || (length > r1*ISLAND_FACTOR && length < r2));
    }

    return inside;
  }

In the AS3 code I don't understand the reasoning behind the ":Function" in the main function "static public function makeShape(seed:int):Function". I did search about it but was unable to find an example or explanation perhaps i'm not typing the correct meaning for it.

If anyone could help me with this problem by giving an example or pointing me closer in the direction I need to go I would be very grateful.

Thanks for your time.

Josh Bailey
  • 25
  • 1
  • 4
  • Your AS3 function is invalid - there's a missing `{` (or an extra `}`). – xxbbcc Jan 13 '15 at 01:41
  • sorry your both correct i just realised i copied the wrong piece of code, i just replaced it with the correct code and removed the c# example. – Josh Bailey Jan 13 '15 at 01:46
  • possible duplicate of [Can a C# method return a method?](http://stackoverflow.com/questions/6563470/can-a-c-sharp-method-return-a-method) – Qantas 94 Heavy Jan 13 '15 at 02:04
  • I did see that question and tried to use it but was unable to fully understand it in terms of applying to my problem, thanks. – Josh Bailey Jan 13 '15 at 02:07

1 Answers1

0

The most direct translation would be to return a delegate. In this case, the generic Func<Point, bool> delegate would be sufficient. It's pretty easy to create these in C# using lambda expressions:

static public Func<Point, bool> makeShape(int seed) {
    // initialization here

    Func<Point, bool> inside = (Point q) => {
        // some math here
        return (myCondition);
    }
    return inside;
}

Although you can define your own delegate type if you prefer:

public delegate bool ShapeTester(Point point);

static public ShapeTester makeShape(int seed) {
    // initialization here

    ShapeTester inside = (Point q) => {
        // some math here
        return (myCondition);
    }
    return inside;
}

Another approach, but one which would require quite a bit more effort in refactoring, would be to encapsulate all the logic of what makes up 'shape' into a distinct type, for example:

public class Shape
{
    public Shape(int seed) 
    {
        // initialization here
    }

    public bool Test(Point q)
    {
        // some math here
        return (myCondition);
    }
}

And then return an instance of this type from your makeShape method:

static public Shape makeShape(int seed) {
    return new Shape(seed);
}

And elsewhere you'd need to call the test method on the resulting object. Depending the specific you're developing, you may make more since if Shape is actually be an interface (IShape) or a struct. But in any case, using this approach, traditional OOP design principles (inheritance, polymorphism, etc.) should be followed.

p.s.w.g
  • 146,324
  • 30
  • 291
  • 331
  • Thanks that's brilliant i tried using delegates but failed to get things done the correct way around. Appreciate your time and help. – Josh Bailey Jan 13 '15 at 01:58