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
相关推荐
harder32143 分钟前
RMP模式的创新突破
开发语言·学习·ios·swift·策略模式
jinanwuhuaguo1 小时前
OpenClaw工程解剖——RAG、向量织构与“记忆宫殿”的索引拓扑学(第十三篇)
android·开发语言·人工智能·kotlin·拓扑学·openclaw
Rust研习社1 小时前
使用 Axum 构建高性能异步 Web 服务
开发语言·前端·网络·后端·http·rust
淘矿人3 小时前
从0到1:用Claude启动你的第一个项目
开发语言·人工智能·git·python·github·php·pygame
cany10003 小时前
C++ -- 模板的声明和定义
开发语言·c++
澈2073 小时前
深耕进阶 Day1:C 与 C++ 核心差异 + C++ 入门基石
c语言·开发语言·c++
Felven3 小时前
C. Need More Arrays
c语言·开发语言
love530love3 小时前
Podman Machine 虚拟硬盘迁移实战二:用 Junction 把 vhdx 从 C 盘搬到其他盘
c语言·开发语言·人工智能·windows·wsl·podman·podman machine
愚者游世3 小时前
noexcept 说明符与 noexcept运算符各版本异同
开发语言·c++·程序人生·面试·visual studio
代码中介商3 小时前
C语言预处理指令深度解析:从宏定义到条件编译
c语言·开发语言