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
相关推荐
知无不研1 小时前
lambda表达式的使用(3)
开发语言·c++·lambda
cfm_29142 小时前
SpringAI + Ollama 本地大模型
java·开发语言·人工智能·语言模型
wujian83112 小时前
怎么用千问生成word文档:从「格式崩」到「一键过」,AI导出鸭打通最后半厘米
人工智能·ai·c#·word·豆包·deepseek·ai导出鸭
C++ 老炮儿的技术栈3 小时前
基于Qt实现轻量化本地音乐播放器
开发语言·c++·qt·c·播放器·音乐
qq_448011163 小时前
C语言中的柔性数组
c语言·开发语言·柔性数组
小白学大数据3 小时前
长周期爬虫的数据一致性:断点续爬 + 事务回滚保障采集质量
开发语言·爬虫·测试工具
zlinear数据采集卡4 小时前
D223上位机C#开发实战:从帧解析到波形显示的完整实现
开发语言·arm开发·嵌入式硬件·fpga开发·开源·c#
Poo_Chai4 小时前
QT emit信号后完整处理流程,包括槽函数响应流程
java·开发语言·数据库
瑞码空间4 小时前
Java 图形界面(GUI)完整知识点手册
java·开发语言·图形界面·swing
小努蛋5 小时前
linux编译openresty异常问题,lua_cjson.c:706:5: 错误: ‘for‘ 循环初始化声明只在 C99 或 C++ 模式下允许
开发语言·lua·openresty