Thursday 15 March 2018

c# Fibonacci vortex



//form1.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ClassLibrary1;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            // Create points that define curve.
            var origin = new Point(400, 400);

            Point[] curvePoints = new Point[14];
            for (int i = 0; i < curvePoints.Length; i++)
            {
                curvePoints[i] = my_function.point_position(i + 1, 90 * i, origin);
            }

            Pen[] pens = new Pen[]
            {
                new Pen(Color.Red,2),
                new Pen(Color.Green,2),
                new Pen(Color.Blue,2),
                new Pen(Color.Gold,2)
            };

            GraphicsPath gp = new GraphicsPath();
            gp.AddCurve(curvePoints);

            for(int i=0;i<4;i++)
            {
                e.Graphics.DrawPath(pens[i], gp);
                gp = rotate_graphic(90, gp, origin);
            }

        }

        private GraphicsPath rotate_graphic(float angle, GraphicsPath g, Point center)
        {
            Matrix rmatrix = new Matrix(1, 0, 0, 1, 0, 0);
            rmatrix.RotateAt(angle, center);
            g.Transform(rmatrix);

            return g;
        }

    }
}

---------------------------------------------------------
//class library

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ClassLibrary1
{
    public class my_function
    {
        public static int fibonacci(int n)
        {
            switch(n)
            {
                case 1:
                    return 1;
                case 2:
                    return 1;
                default:
                    return fibonacci(n - 2) + fibonacci(n - 1);
            }
        }

        public static Point point_position(int n, double angle, Point origin)
        {
            int radius = fibonacci(n);

            Point position = origin;
            position.X += Convert.ToInt32(radius * Math.Cos(Math.PI * angle / 180));
            position.Y += Convert.ToInt32(radius * Math.Sin(Math.PI * angle / 180));

            return position;
        }
    }
}

No comments:

Post a Comment