java 折纸_例题1折纸痕

递归算法的思路,使用

c5615c0ff063aa4e6e471ed2bdd90873.png

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 stack = new Stack();

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 

{

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);

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值