递归算法的思路,使用

wKioL1glMlqh09UJAAJQ1hC9s64196.png-wh_50


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

namespace 例题1折纸痕
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            //test(g);
            float n = 16;
            draw(g, n);
        }

        private Stack<PointF> stack = new Stack<PointF>();
        private PointF startPoint = new PointF(300, 300);
        private float unitLength = 2;
        

        // 画n问题
        private void draw(Graphics g, float n){
	        if(n==1){
                GraphicsState state = g.Save();
                for (int i = 0; i < stack.Count; i++)
                {
                    PointF zheDian = stack.ElementAt(i);
                    g.TranslateTransform(-zheDian.X, -zheDian.Y, MatrixOrder.Append);
                    g.RotateTransform((float)(90), MatrixOrder.Append);
                    g.TranslateTransform(zheDian.X, zheDian.Y, MatrixOrder.Append);
                }
                g.DrawLine(new Pen(new SolidBrush(Color.Black)), 
                                    startPoint, 
                                    new PointF(startPoint.X + unitLength, startPoint.Y));
                g.DrawLine(new Pen(new SolidBrush(Color.Black)),
                                    new PointF(startPoint.X + unitLength, startPoint.Y), 
                                    new PointF(startPoint.X + unitLength, startPoint.Y - unitLength));
                g.Restore(state);
	        }else{
		        draw(g, n-1);
                PointF zheDian = calcZheDian(n);
                stack.Push(zheDian);
		        draw(g, n-1);
		        stack.Pop();
	        }
        }


        private PointF calcZheDian(float n)
        {
            if (n == 1)
            {
                return new PointF(startPoint.X + unitLength, startPoint.Y);
            }
            else
            {
                PointF p = RotatePointWithCenter(90 * Math.PI / 180, calcZheDian(n-1), startPoint);
                return p;
            }
        }


        private PointF RotatePoint(double fi, float x, float y)
        {
	        double rotatedX = Math.Cos(fi) * x - Math.Sin(fi) * y;
	        double rotatedY = Math.Sin(fi) * x + Math.Cos(fi) * y;
	        return new PointF((float)rotatedX, (float)rotatedY);
        }


        private PointF RotatePointWithCenter   (double fi, PointF center, PointF p) 
        {
	        float relX = p.X - center.X;
            float relY = p.Y - center.Y;
	        PointF point = RotatePoint (fi, relX, relY);
            float rotatedX = center.X + point.X;
            float rotatedY = center.Y + point.Y;
	        return new PointF(rotatedX, rotatedY);
        }
    }
}