【数理几何】幂函数、三角函数与椭圆方程的神奇组合

公式如下

f(x)=|x|^\frac{2}{3}+0.9\sqrt{3.3-x^2}\sin(b\pi x) \cdots \cdots\cdots\cdots [-\sqrt{3.3},+\sqrt{3.3}]

C#代码如下

窗体代码

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

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        HeartCurve hc = new HeartCurve();
        public Form1()
        {
            InitializeComponent();
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);

            if (hc.path != null)
                using (Pen pen = new Pen(Color.Red,2))
                {
                    e.Graphics.DrawPath(pen,hc.path);
                }
        }

        private void txtX_ValueChanged(object sender, EventArgs e)
        {
            hc.Create((double)txtB.Value,(double)txtZoom.Value, (int)txtOffsetX.Value, (int)txtOffsetY.Value);
            Invalidate();
        }
    }
}

 算法代码

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

namespace WindowsFormsApplication1
{
    public class HeartCurve
    {
        public GraphicsPath path = null;
        public List<Point> data = new List<Point>();
        /// <summary>
        /// 
        /// </summary>
        /// <param name="b">变量</param>
        /// <param name="zoom">放大倍数</param>
        /// <param name="offsetX">x轴偏移量</param>
        /// <param name="offsetY">y轴偏移量</param>
        public void Create(double b, double zoom = 100, double offsetX = 300, double offsetY = 400)
        {
            try
            {
                if (data == null) data = new List<Point>();
                data.Clear();

                if (path != null) { path.Dispose(); path = null; }
                if (path == null) path = new GraphicsPath();

                double x0 = 0;
                double y0 = 0;
                double x1 = 0;
                double start = -Math.Pow(3.3, 0.5);
                double end = Math.Pow(3.3, 0.5);

                for (double x = start; x < end; x += 0.001)
                {
                    x0 = x * zoom + offsetX;
                    y0 = -(int)(Fx(x, b) * zoom) + offsetY;
                    if (y0 < -2048) continue;
                    if (y0 > 2048) continue;
                    if (x0 < -2048) continue;
                    if (x0 > 2048) continue;
                    Point pnt = new Point((int)x0, (int)y0);
                    if (data.Contains(pnt) == false)
                        data.Add(pnt);
                }

                Point pnt1 = new Point(0, 0);
                Point pnt2 = new Point(0, 0);
                for (int i = 0; i < data.Count; i++)
                {
                    if (i > 0)
                    {
                        pnt2 = data[i];
                        path.AddLine(pnt1, pnt2);
                        pnt1 = pnt2;
                    }
                    else
                    {
                        pnt1 = data[i];
                    }
                }
            }
            catch (Exception exp)
            {

            }
        }
        /// <summary>
        /// 幂函数、三角函数与椭圆方程的神奇组合
        /// </summary>
        /// <param name="x"></param>
        /// <param name="b"></param>
        /// <returns></returns>
        public double Fx(double x, double b)
        {
            return Math.Pow(Math.Abs(x), 2.0 / 3.0) + 0.9 * Math.Sqrt(3.3 - x * x) * Math.Sin(b * Math.PI * x);
        }
    }
}

效果如下 

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值