绿色风's Blog
专注AutoIT(Au3)
  • 首页
  • 流●年
  • 笔●记
    • 学习随记
    • 源码示例
  • 脚●本
    • UDF(收集)
    • 工作室UDF
    • 工具●教程
    • 教程之GDI
  • 作●品
  • 下●载
  • 情怀ExcelTip
9月142023

有点意思的代码-起动能量

作者:绿色风   发布:2023-9-14 1:41 Thursday   分类:源码示例   阅读:3537次   评论:0条  

#Region Includes
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <Color.au3>
#include <GDIPlus.au3>
#include <Misc.au3>
#EndRegion

#Region Declarations
;~ Window
Global $wWidth = 800
Global $wHeight = 600
Global $wMiddle_x = $wWidth / 2
Global $wMiddle_y = $wHeight / 2
Global $middle_x = @DesktopWidth/2
Global $middle_y = @DesktopHeight/2

;~ Env
Global $pi = 3.141592653589793
Global $g = 1
Global $x[3], $y[3], $th[3], $v[3], $a[3], $l[3], $m[3], $oldX[3], $oldY[3]
Global $energy

;~ INIT pendulum 1
$l[1] = 200
$m[1] = 20
$th[1] = 0.4*$pi
$v[1] = 0.0
$a[1] = 0
$x[1] = $wMiddle_x + $l[1] * sin($th[1])
$y[1] = 0.5*$wMiddle_y - $l[1] * cos($th[1])

;~ INIT pendulum 2
$l[2] = 200
$m[2] = 20
$th[2] = 0.5*$pi
$v[2] = 0.0
$a[2] = 0
$x[2] = $x[1] + $l[2]*sin((($th[2]-180)*$pi)/180)
$y[2] = $y[1] - $l[2]*cos((($th[2]-180)*$pi)/180)

$oldX[1] = $x[1]
$oldY[1] = $y[1]
$oldX[2] = $x[2]
$oldY[2] = $y[2]
$energy = _Energy()
ConsoleWrite('起动能量: ' & $energy & @CRLF)
#EndRegion

#Region Gui
$hGui = GUICreate("Form1", $wWidth, $wHeight, $middle_x-($wWidth/2), $middle_y-($wHeight/2),-2147483648)
GUISetState(@SW_SHOW)
#EndRegion

#Region ### START GDI+ ###
_GDIPlus_Startup()
$hGraphic = _GDIPlus_GraphicsCreateFromHWND($hGui)

;~ Bitmaps
$hGraphicBitmap = _GDIPlus_BitmapCreateFromGraphics($wWidth, $wHeight, $hGraphic)
$hHistoryBitmap = _GDIPlus_BitmapCreateFromScan0($wWidth, $wHeight)
$hLegBitmap = _GDIPlus_BitmapCreateFromScan0($wWidth, $wHeight)

;~ Buffers
$hBuffer = _GDIPlus_ImageGetGraphicsContext($hGraphicBitmap)
$hHistoryBuffer = _GDIPlus_ImageGetGraphicsContext($hHistoryBitmap)
$hLegBuffer = _GDIPlus_ImageGetGraphicsContext($hLegBitmap)

;~ Smoothing
_GDIPlus_GraphicsSetSmoothingMode($hBuffer, 4)
_GDIPlus_GraphicsSetSmoothingMode($hHistoryBuffer, 4)
_GDIPlus_GraphicsSetSmoothingMode($hLegBuffer, 4)

;~ First Clearing
_GDIPlus_GraphicsClear($hBuffer, 0xFFFFFFFF)
_GDIPlus_GraphicsClear($hHistoryBuffer, 0xFFFFFFFF)
_GDIPlus_GraphicsClear($hLegBuffer, 0xFFFFFFFF)

;~ Pens
$hPen = _GDIPlus_PenCreate(0xFF0000FF, 1)
#EndRegion ### END GDI+ ###

#Region EventMode
Opt("GUIOnEventMode", 1)
GUISetOnEvent($GUI_EVENT_CLOSE, "SpecialEvents")
#EndRegion

#Region Loop
While 1
   _Calc()
   _Draw()
   _Energy()
WEnd
#EndRegion

#Region Functions
Func _Energy()
    ; Summe aus m*g*h + Summe aus 1/2 mv² (ob das so exakt stimmt ist erstmal egal)
    $e1 = $m[1] * $g * ($wHeight - $y[1]) + $m[2] * $g * ($wHeight - $y[2])
    $e2 = 0.5 * $m[1] * (($x[1] - $oldX[1])^2 + ($y[1] - $oldY[1])^2) +  + 0.5 * $m[2] * (($x[2] - $oldX[2])^2 + ($y[2] - $oldY[2])^2)
    ToolTip('EPot: ' & Round($e1) & @CRLF & 'EKin: ' & Round($e2) & @CRLF & 'E: ' & Round($e1 + $e2) & ' (' & Round(($e1 + $e2)/$energy, 2)*100 & '%)')
    Return $e1 + $e2
EndFunc

Func _Calc()
;~    Acceleration 1
   $num1 = -$g * (2*$m[1]+$m[2]) * sin($th[1])
   $num2 = -$m[2] * $g * sin($th[1]-2*$th[2])
   $num3 = -2 * sin($th[1]-$th[2]) * $m[2]
   $num4 = $v[2]*$v[2] * $l[2] + $v[1]*$v[1] * $l[1] * cos($th[1]-$th[2])
   $den = $l[1] * (2 * $m[1] + $m[2] - $m[2] * cos(2*$th[1]-2*$th[2]))
   $a[1] = ($num1 + $num2 + ($num3 * $num4)) / $den

;~    Acceleration 2
   $num1 = 2 * sin($th[1]-$th[2])
   $num2 = ($v[1]*$v[1] * $l[1] * ($m[1]+$m[2]))
   $num3 = $g * ($m[1]+$m[2]) * cos($th[1])
   $num4 = $v[2]*$v[2] * $l[2] * $m[2] * cos($th[1]-$th[2])
   $den = $l[2] * (2 * $m[1] + $m[2] - $m[2] * cos(2*$th[1]-2*$th[2]))
   $a[2] = ($num1 * ($num2 + $num3 + $num4)) / $den

;~    Angles
   $v[1] += $a[1]
   $v[2] += $a[2]
   $th[1] += $v[1]
   $th[2] += $v[2]

;~    EndPoints
    $oldX[1] = $x[1]
    $oldY[1] = $y[1]
    $oldX[2] = $x[2]
    $oldY[2] = $y[2]
   $x[1] = $wMiddle_x - $l[1] * sin($th[1])
   $y[1] = 0.5*$wMiddle_y + $l[1] * cos($th[1])
   $x[2] = $x[1] - $l[2]*sin($th[2])
   $y[2] = $y[1] + $l[2]*cos($th[2])
EndFunc

Func _Draw()
;~    Clear
   _GDIPlus_GraphicsDrawImageRect($hLegBuffer, $hHistoryBitmap, 0, 0, $wWidth, $wHeight)

;~    Draw pieces
   _GDIPlus_GraphicsDrawLine($hLegBuffer, $wMiddle_x, 0.5*$wMiddle_y, $x[1], $y[1], $hPen)
   _GDIPlus_GraphicsFillEllipse($hLegBuffer, $x[1]-($m[1]/2), $y[1]-($m[1]/2), $m[1], $m[1])
   _GDIPlus_GraphicsDrawLine($hLegBuffer, $x[1], $y[1], $x[2], $y[2], $hPen)
   _GDIPlus_GraphicsFillEllipse($hLegBuffer, $x[2]-($m[2]/2), $y[2]-($m[2]/2), $m[2], $m[2])

;~    History
   _GDIPlus_GraphicsFillEllipse($hHistoryBuffer, $x[2], $y[2], 2, 2)

;~    Draw finally
   _GDIPlus_GraphicsDrawImageRect($hBuffer, $hHistoryBitmap,0, 0, $wWidth, $wHeight)
   _GDIPlus_GraphicsDrawImageRect($hBuffer, $hLegBitmap,0, 0, $wWidth, $wHeight)
   _GDIPlus_GraphicsDrawImageRect($hGraphic, $hGraphicBitmap, 0, 0, $wWidth, $wHeight)
EndFunc

Func _Exit()
   _GDIPlus_GraphicsDispose($hGraphic)
   _GDIPlus_PenDispose($hPen)
   _GDIPlus_Shutdown()
   Exit
EndFunc
#EndRegion

#Region Event Functions
Func SpecialEvents()
   Select
      Case @GUI_CtrlId = $GUI_EVENT_CLOSE
         GUIDelete()
         _Exit()
   EndSelect
EndFunc
#EndRegion




本文固定链接: http://www.jianyiit.com/post-412.html

blogger
该日志由 绿色风 于2023-9-14 1:41 Thursday发表在 源码示例 分类下。
版权所有:《绿色风's Blog》 → 《有点意思的代码-起动能量》;
除特别标注,本博客很多文章均为原创. 互联分享,尊重版权,转载请以链接形式标明本文地址;
本文标签:

扫描二维码,在手机上阅读
上一篇::有点意思的代码-画个五角星
下一篇:996MIR引擎,特效图集,拆分转成正坐标图片

热门文章

相关文章

  • Au3返回指定日期的国际标准(ISO)周数. 年度的第几周)
  • 有点意思的代码-模拟混沌单摆
  • Php-authcode算法之AU3版
  • Au3小游戏 2048
  • Au3 屏幕取色器
取消回复

发表评论

亲,头像对么?

71 + 39 =

提交中,请稍候……


木有头像就木JJ啦!还木有头像吗?点这里申请属于你的个性Gravatar头像吧!


    站点统计
    • 运行时间: 20254 天
    • 日志总数: 365 篇
    • 评论数量: 7221 条
    • 微语数量: 6 条
    • 附件总量: 388 件
  • 逝出的青春

  • 打赏"绿色风"



      扫码关注本站公众号 可搜本站内容

  • Autoit V3 脚本交流群

      常驻群1:905774875
      常驻群2:40672266


  • 链接

    • AU3中文论坛
    • Excel资料库
    • 完美者博客
    • 顺网小哥'S Blog
    • 猛牛哥的博客
    • 网吧系统下载
  • 分类

    • 流●年(66)
    • 笔●记(0)
    • 脚●本(0)
    • 作品(21)
    • 学习随记(51)
    • 源码示例(68)
    • UDF(收集)(26)
    • 工作室UDF(30)
    • 工具●教程(62)
    • 教程之GDI(24)
Copyright © 2013 绿色风's Blog. Powered by emlog. Theme by 射雕天龙. 鄂ICP备2021011689号-1 鄂公网安备42102302000078号 sitemap