C#-需要帮助来创建程序来通过画线来模拟素数行为 [英] C# - Need help creating program that simulates Prime Number behavior by drawing lines

查看:99
本文介绍了C#-需要帮助来创建程序来通过画线来模拟素数行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我向自己发起挑战,以创建一个可以测试素数的程序,我已经知道了如何做,但是没有编码技巧.

I challenged myself to create a program to experiment with Prime Numbers, I already have an idea how to do it, but not the coding skills..

我的计划是这样的: 首先,用C#创建一个程序,使程序遵循一些规则:

My plan is this: First ill create a program in C# that makes straight lines that follow some rules:

规则1:.所有行的长度都相同.

rule 1: all lines have the same length.

规则2::所有行都是水平或垂直(无对角线).

rule 2: all lines are either horizontal or vertical (no diagonals).

规则3:每条新行都从上一行结束处开始(这样所有行都合并了).

rule 3: Every new line begins where the previous line has ended (that way all the lines are joined).

现在开始棘手的部分:

我想创建一个计数器,该计数器在每次创建新行时(我假定起始值为1)都增加1,并且每当达到质数时,行就会更改方向"并开始行进"在原始方向的左侧.

I would like to make a counter that goes up by 1 each time a new line is created (starting value is 1 I presume), and whenever it reaches a prime number, the lines change 'direction' and start 'going' to the left of the original direction.

前10行应如下所示:

      |
 __   |
|  |  |
|__ __|

请注意在第二,第三,第五和第七行它是如何改变方向的.

Notice how it changed direction at the 2nd, 3rd, 5th and 7th line.

(从内部开始,从外部开始)

(starting from the inside ending at the outside)

这将产生一条较长的扭曲线,并且看到它形成的图案会很酷.

This will create a long twisting line and it will be pretty cool to see what pattern it makes.

感谢您的帮助!

推荐答案

这是一个有趣的项目.谢谢你早上的娱乐!

This was a fun project to play with. Thanks for my morning entertainment!

我在此SO线程中使用了SLaks代码小于指定数量的素数列表.

I used SLaks code in this SO thread to get a list of Primes less than a specified number.

以下是一些示例输出:

生成素数后,我遍历列表并将行存储在

After generating the Primes, I walk the list and store the lines in a GraphicsPath. Using the Graphics.GetBounds() method then allows us to appropriately scale and transform the Graphics in the Paint() event of our Panel so that the entire drawing can be seen:

public partial class Form1 : Form
{

    private const int segmentLength = 10;
    private GraphicsPath gpPrimes = null;

    public Form1()
    {
        InitializeComponent();
        nudLessThanMax.Minimum = 15;
        nudLessThanMax.Maximum = 500000;
        nudLessThanMax.Value = nudLessThanMax.Minimum;
        pnlPrimes.Paint += PnlPrimes_Paint;
        pnlPrimes.SizeChanged += PnlPrimes_SizeChanged;
    }

    private void PnlPrimes_SizeChanged(object sender, EventArgs e)
    {
        pnlPrimes.Invalidate();
    }

    private void PnlPrimes_Paint(object sender, PaintEventArgs e)
    {
        if (gpPrimes != null)
        {
            RectangleF rectF = gpPrimes.GetBounds();
            float max = Math.Max(rectF.Width + (2 * segmentLength), rectF.Height + (2 * segmentLength));
            e.Graphics.TranslateTransform(pnlPrimes.Width / 2, pnlPrimes.Height / 2);
            e.Graphics.ScaleTransform((float)pnlPrimes.Width / max, (float)pnlPrimes.Height / max);
            e.Graphics.TranslateTransform(-(rectF.Left + rectF.Width / 2), -(rectF.Top + rectF.Height / 2));
            e.Graphics.DrawPath(Pens.Black, gpPrimes);
        }
    }

    private void btnGraphPrimes_Click(object sender, EventArgs e)
    {
        btnGraphPrimes.Enabled = false;
        backgroundWorker1.RunWorkerAsync((int)this.nudLessThanMax.Value);
    }

    private List<int> PrimesLessThan(int num) // SLaks: https://stackoverflow.com/a/1510186/2330053
    {
        return Enumerable.Range(0, (int)Math.Floor(2.52 * Math.Sqrt(num) / Math.Log(num))).Aggregate(
            Enumerable.Range(2, num - 1).ToList(),
            (result, index) =>
            {
                var bp = result[index]; var sqr = bp * bp;
                result.RemoveAll(i => i >= sqr && i % bp == 0);
                return result;
            }
        );
    }

    private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
        int diff;
        int num = (int)e.Argument;
        Point pt = new Point(0, 0);
        Point pt2 = pt;
        GraphicsPath gp = new GraphicsPath();
        List<int> primes = PrimesLessThan(num);

        for(int i = 1; i < primes.Count; i++)
        {
            diff = primes[i] - primes[i - 1];
            switch(i % 4)
            {
                case 1: // up
                    pt2 = new Point(pt.X, pt.Y - (segmentLength * diff));
                    break;

                case 2: // left
                    pt2 = new Point(pt.X - (segmentLength * diff), pt.Y);
                    break;

                case 3: // down
                    pt2 = new Point(pt.X, pt.Y + (segmentLength * diff));
                    break;

                case 0: // right
                    pt2 = new Point(pt.X + (segmentLength * diff), pt.Y);
                    break;
            }
            gp.AddLine(pt, pt2);
            pt = pt2;
        }
        gpPrimes = gp;
        e.Result = primes;
    }

    private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        lbPrimes.DataSource = (List<int>)e.Result;
        pnlPrimes.Invalidate();
        btnGraphPrimes.Enabled = true;
    }

}

这篇关于C#-需要帮助来创建程序来通过画线来模拟素数行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆