C# 机构仿真实例

1、实现连杆带动滑块运动
一个连杆旋转带动另一个连杆,另一个连杆拖动滑块,点击“开始”按钮开始运动,再点击按钮,则停止运动。
在这里插入图片描述
2、实现程序

#region 机构仿真
        Image image = null;
        Timer timer= new Timer();
        int width = 0;
        int height = 0;
        int axisPoint = 10;
        double angle = 0;
        bool isRun = false;
        const int pole1 = 50;
        const int pole2 = 150;
        Point[] points = new Point[3];
        private void MechanismInitialize()
        {
            DrawMechanism(angle);
            timer.Interval= 100;
            timer.Tick += Timer_Tick;
            timer.Start();
        }

        private void Timer_Tick(object sender, EventArgs e)
        {
            if (isRun)
            {
                DrawMechanism(angle);
                angle += 10;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (button1.Text.Equals("开始"))
            {
                button1.Text = "结束";
                isRun = true;
            }
            else
            {
                button1.Text = "开始";
                isRun = false;
            }
        }

        private void DrawMechanism(double angle)
        {
            width = this.pictureBox1.Width;
            height = this.pictureBox1.Height;
            image = new Bitmap(width, height);
            Graphics g = Graphics.FromImage(image);
            g.Clear(Color.White);
            int x = width / 8 * 3;
            int y = height / 2;
            Point p = new Point(x, y);
            points[0] = p;
            x = x + (int)(pole1 * Math.Cos(Math.PI / 180*angle));
            y = y + (int)(pole1 * Math.Sin(Math.PI / 180*angle));
            p = new Point(x, y);
            points[1] = p;
            int z = pole1 * (int)(Math.Sin(Math.PI / 180 * angle));
            x = (int)(width / 8 * 3 + pole1 * Math.Cos(Math.PI / 180 * angle) + Math.Sqrt(pole2 * pole2 - z * z));
            y = height / 2;
            p = new Point(x, y);
            points[2] = p;
            Pen pen = new Pen(Color.Black, 2);
            g.DrawEllipse(pen, points[0].X - axisPoint / 2, points[0].Y - axisPoint / 2, axisPoint, axisPoint);
            g.DrawLine(pen, points[0], points[1]);
            g.DrawEllipse(pen, points[1].X - axisPoint / 2, points[1].Y - axisPoint / 2, axisPoint, axisPoint);
            g.DrawLine(pen, points[1], points[2]);
            g.FillEllipse(new SolidBrush(Color.Blue), points[2].X - axisPoint, points[2].Y - axisPoint , axisPoint*2, axisPoint*2);
            g.Dispose();
            this.pictureBox1.Image = image;
        }
        #endregion
  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大浪淘沙胡

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值