Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/428.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么我的龙不完整_C#_Javascript_Fractals - Fatal编程技术网

C# 为什么我的龙不完整

C# 为什么我的龙不完整,c#,javascript,fractals,C#,Javascript,Fractals,我已经将代码从javascript翻译成了c#,可以在 我的翻译旨在点击按钮后生成一条龙,但我认为我的翻译中遗漏了一些内容。 请参阅维基百科文章:了解更多信息 不完整的龙分形输出: 代码: public partial class MainPage : UserControl { PointCollection pc; Int32[] pattern = new Int32[] { 1, 1, 0, 2, 1, 0, 0, 3 }; Int32[] position =

我已经将代码从javascript翻译成了c#,可以在

我的翻译旨在点击按钮后生成一条龙,但我认为我的翻译中遗漏了一些内容。
请参阅维基百科文章:了解更多信息

不完整的龙分形输出: 代码:

public partial class MainPage : UserControl
{
    PointCollection pc;

    Int32[] pattern = new Int32[] { 1, 1, 0, 2, 1, 0, 0, 3 };
    Int32[] position = new Int32[] { 0, 0, 0, 0, 0, 0, 0, 0 };
    Boolean toggle;
    Char r = default(Char);

    Int32 distance = 10; // line length
    Int32 step = 100; // paints per step
    Int32 skip = 10; // folds per paint

    Double x = 0;
    Double y = 0;
    Int32 a = 90;


    public MainPage()
    {
        InitializeComponent();
    }


    private void btnFire_Click(object sender, RoutedEventArgs e)
    {

        x = canvas.ActualWidth / 3;
        y = canvas.ActualHeight / 1.5;

        pc = new PointCollection();

        var n = step;
        while (--n > 0)
        {
            List<Char> s = getS(skip);
            draw(s);
        }

        Polyline p = new Polyline();
        p.Stroke = new SolidColorBrush(Colors.Red);
        p.StrokeThickness = 0.5;

        p.Points = pc;
        canvas.Children.Add(p);
    }


    List<Char> getS(Int32 n)
    {
        List<Char> s1 = new List<Char>();
        while (n-- > 0) s1.Add(getNext(0));
        return s1;
    }


    void draw(List<Char> s)
    {        
        pc.Add(new Point(x, y));
        for (Int32 i = 0, n = s.Count; i < n; i++)
        {
            pc.Add(new Point(x, y));
            Int32 j;
            if (int.TryParse(s[i].ToString(), out j) && j != 0)
            {
                if ((a + 90) % 360 != 0)
                {
                    a = (a + 90) % 360;
                }
                else
                {
                    a = 360; // Right
                }
            }
            else
            {
                if (a - 90 != 0)
                {
                    a = a - 90;
                }
                else
                {
                    a = 360; // Right
                }
            }
            // new target
            if (a == 0 || a == 360)
            {
                y -= distance;
            }
            else if (a == 90)
            {
                x += distance;
            }
            else if (a == 180)
            {
                y += distance;
            }
            else if (a == 270)
            {
                x -= distance;
            }

            // move
            pc.Add(new Point(x, y));
        }

    }

    Char getNext(Int32 n)
    {
        if (position[n] == 7)
        {
            r = getNext(n + 1);
            position[n] = 0;
        }
        else
        {
            var x = position[n] > 0 ? pattern[position[n]] : pattern[0];

            switch (x)
            {
                case 0:
                    r = '0';
                    break;
                case 1:
                    r = '1';
                    break;
                case 2:
                    if (!toggle)
                    {
                        r = '1';
                    }
                    else
                    {
                        r = '0';
                    }
                    toggle = !toggle;
                    break;
            }
            position[n] = position[n] + 1;                
        }

        return r;
    }

}
public部分类主页面:UserControl
{
点采集pc;
Int32[]模式=新的Int32[]{1,1,0,2,1,0,0,3};
Int32[]位置=新的Int32[]{0,0,0,0,0,0};
布尔切换;
charr=默认值(Char);
Int32距离=10;//线长度
Int32 step=100;//每一步绘制
Int32 skip=10;//每幅绘画的折叠次数
双x=0;
双y=0;
int32a=90;
公共主页()
{
初始化组件();
}
私有void btnFire\u单击(对象发送方,路由目标)
{
x=画布实际宽度/3;
y=画布实际高度/1.5;
pc=新点集合();
var n=阶跃;
而(--n>0)
{
列表s=获取(跳过);
抽签;
}
多段线p=新的多段线();
p、 笔划=新的SolidColorBrush(颜色为红色);
p、 冲程厚度=0.5;
p、 点数=pc;
canvas.Children.Add(p);
}
列表获取(Int32 n)
{
列表s1=新列表();
而(n-->0)s1.Add(getNext(0));
返回s1;
}
作废抽签(列表s)
{        
pc.Add(新点(x,y));
对于(Int32 i=0,n=s.计数;i0?模式[n]]:模式[0];
开关(x)
{
案例0:
r='0';
打破
案例1:
r='1';
打破
案例2:
如果(!切换)
{
r='1';
}
其他的
{
r='0';
}
切换=!切换;
打破
}
位置[n]=位置[n]+1;
}
返回r;
}
}

我清理了代码,试图弄清楚
模式和
位置
数组应该如何产生正确的序列,但我想不出来。例如,
模式
数组中的最后一项从未使用过

但是,有一种更简单的方法实现了
getNext
方法,它只使用一个计数器:

bool getNext() {
  cnt++;
  return (cnt & ((cnt & -cnt) << 1)) != 0;
}

可能更适合@IAbstract,但绝对不适合。这是不起作用的代码,它没有实现它所设定的目标。(这是一条龙曲线分形。)
public partial class MainPage : UserControl {
    PointCollection pc;

    int cnt = 0;

    int distance = 10; // line length
    int steps = 1024; // number of paints

    int x = 0;
    int y = 0;
    int a = 90;


    public MainPage() {
        InitializeComponent();
    }


    private void btnFire_Click(object sender, RoutedEventArgs e) {

        x = (int)(canvas.ActualWidth / 3);
        y = (int)(canvas.ActualHeight / 1.5);

        pc = new PointCollection();

        draw(getS(steps));

        Polyline p = new Polyline();
        p.Stroke = new SolidColorBrush(Colors.Red);
        p.StrokeThickness = 0.5;

        p.Points = pc;
        canvas.Children.Add(p);
    }


    List<bool> getS(int n) {
        List<bool> s1 = new List<bool>();
        while (n-- > 0) {
            s1.Add(getNext());
        }
        return s1;
    }


    void draw(List<bool> s) {
        pc.Add(new Point(x, y));
        foreach (bool z in s) {

            a = (a + (z ? 90 : 270)) % 360;

            // new target
            switch (a) {
                case 90: x += distance; break;
                case 180: y += distance; break;
                case 270: x -= distance; break;
                default: y -= distance; break;
            }

            // move
            pc.Add(new Point(x, y));
        }

    }

    bool getNext() {
        cnt++;
        return (cnt & ((cnt & -cnt) << 1)) != 0;
    }

}