C# 机构仿真实例

1、实现连杆带动滑块运动

一个连杆旋转带动另一个连杆,另一个连杆拖动滑块,点击"开始"按钮开始运动,再点击按钮,则停止运动。

2、实现程序

csharp 复制代码
#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
相关推荐
一颗花生米。2 小时前
深入理解JavaScript 的原型继承
java·开发语言·javascript·原型模式
问道飞鱼2 小时前
Java基础-单例模式的实现
java·开发语言·单例模式
学习使我快乐012 小时前
JS进阶 3——深入面向对象、原型
开发语言·前端·javascript
通信仿真实验室3 小时前
(10)MATLAB莱斯(Rician)衰落信道仿真1
开发语言·matlab
勿语&3 小时前
Element-UI Plus 暗黑主题切换及自定义主题色
开发语言·javascript·ui
吾爱星辰6 小时前
Kotlin 处理字符串和正则表达式(二十一)
java·开发语言·jvm·正则表达式·kotlin
ChinaDragonDreamer6 小时前
Kotlin:2.0.20 的新特性
android·开发语言·kotlin
IT良6 小时前
c#增删改查 (数据操作的基础)
开发语言·c#
yufei-coder6 小时前
掌握 C# 中的 LINQ(语言集成查询)
windows·vscode·c#·visual studio
Kalika0-07 小时前
猴子吃桃-C语言
c语言·开发语言·数据结构·算法