📄 doublepend.cpp
字号:
// DoublePend.cpp : Implementation of CDoublePend
#include "stdafx.h"
#include "DoublePendulum.h"
#include "DoublePend.h"
#include <math.h>
/////////////////////////////////////////////////////////////////////////////
// CDoublePend
void CDoublePend::Function( double *xx, double *f )
{
double c, c0, c1, c10, x22, x33;
// Gravity divided by the length.
c = 9.8 / m_dLength;
// Calculate motion vectors.
c0 = cos( xx[0] ) * c;
c1 = cos( xx[1] ) * c;
c10 = cos( xx[1] - xx[0] );
c = sin( xx[1] - xx[0] );
// Calculate the positions.
x22 = xx[2] * xx[2] * c;
x33 = xx[3] * xx[3] * c;
c = 9.0 / ( 16.0 - 9.0 * c1 * c10 );
// Store the values.
f[0] = xx[2];
f[1] = xx[3];
f[2] = ( 2.0 * x33 / 3.0 + c10 * x22 +
c10 * c1 - 2.0 * c0 ) * c;
f[3] = ( -8.0 * x22 / 3.0 - c10 * x33 +
3.0 * c10 * c0 - 8.0 * c1 / 3.0 ) * c;
}
void CDoublePend::FillOval( HDC hDC, int nX, int nY,
int nXR, int nYR, COLORREF cColor )
{
HBRUSH hBrush, hOldBrush;
HPEN hPen, hOldPen;
// Create the brush, select it into the dc,
// and remember the old brush.
hBrush = CreateSolidBrush( cColor );
hOldBrush = (HBRUSH) SelectObject( hDC, hBrush );
// Create the pen, select it into the dc.
// and remember the old pen.
hPen = CreatePen( PS_SOLID, 1, RGB( 0, 0, 0 ) );
hOldPen = (HPEN) SelectObject( hDC, hPen );
// Draw the ellipse.
Ellipse( hDC, nX - ( nXR / 2 ), nY - ( nYR / 2 ),
nX + ( nXR / 2 ), nY + ( nYR / 2 ) );
// Select the old brush and pen back
// into the dc.
SelectObject( hDC, hOldBrush );
SelectObject( hDC, hOldPen );
// Delete the GDI objects.
DeleteObject( hPen );
DeleteObject( hBrush );
}
void CDoublePend::DrawPend( HDC hDC, RECT& rc )
{
// We're going to double buffer in
// order to reduce the redraw flicker.
HBITMAP hOldBitmap, hBitmap;
// Create a dc for the off-screen buffer.
HDC hWorkDC = CreateCompatibleDC( hDC );
// Create the off-screen buffer.
hBitmap = CreateCompatibleBitmap( hWorkDC,
rc.right - rc.left, rc.bottom - rc.top );
// Select the bitmap into the newly-created
// dc and remember the bitmap that was
// previously selected.
hOldBitmap =
(HBITMAP) SelectObject( hWorkDC, hBitmap );
// Draw a rectangle to clear the buffer.
Rectangle( hWorkDC, rc.left, rc.top, rc.right,
rc.bottom );
// Calculate the width, height, and midpoint.
int nWidth = rc.right - rc.left;
int nHeight = rc.bottom - rc.top;
int nMidX = rc.left + ( nWidth / 2 );
int nMidY = rc.top + ( nHeight / 2 );
// Calculate the endpoints of the pendulum.
int nCx1 = nMidX + (int) ( m_dLength * cos( x[0] ) );
int nCy1 = nMidY + (int) ( m_dLength * sin( x[0] ) );
int nCx2 = nCx1 + (int) ( m_dLength * cos( x[1] ) );
int nCy2 = nCy1 + (int) ( m_dLength * sin( x[1] ) );
// Draw the three pendulum ellipses.
FillOval( hWorkDC, nMidX,
nMidY, 7, 7, RGB( 0, 255, 0 ) );
FillOval( hWorkDC, nCx1,
nCy1, 11, 11, RGB( 0, 255, 0 ) );
FillOval( hWorkDC, nCx2,
nCy2, 11, 11, RGB( 0, 255, 0 ) );
// Select a black stock pen into the dc.
SelectObject( hWorkDC, GetStockObject( BLACK_PEN ) );
// Move to the midpoint.
MoveToEx( hWorkDC, nMidX, nMidY, NULL );
// Draw the first segment.
LineTo( hWorkDC, nCx1, nCy1 );
// Draw the second segment.
LineTo( hWorkDC, nCx2, nCy2 );
// Draw the off-screen buffer to the visual screen.
BitBlt( hDC, 0, 0, nWidth, nHeight, hWorkDC, 0, 0,
SRCCOPY );
// Select the old bitmap into the dc, delete the
// newly-created bitmap and dc.
SelectObject( hWorkDC, hOldBitmap );
DeleteObject( hBitmap );
DeleteDC( hWorkDC );
}
STDMETHODIMP CDoublePend::Update()
{
double x1[4], f1[4], f2[4], f3[4], f4[4], hh, hhh;
hh = h / 2.0;
hhh = h / 6.0;
for( int k=0; k<16; k++ ){
int i;
// Update the first point.
Function( x, f1 );
for( i=0; i<4; i++ )
x1[i] = x[i] + hh * f1[i];
// Update the second point.
Function( x1, f2 );
for( i=0; i<4; i++ )
x1[i] = x[i] + hh * f2[i];
// Update the third point.
Function( x1, f3 );
for( i=0; i<4; i++ )
x1[i] = x[i] + h * f3[i];
// Update the fourth point.
Function( x1, f4 );
for( i=0; i<4; i++ )
x[i] += hhh * ( f1[i] + 2.0 * f2[i] +
2.0 * f3[i] + f4[i] );
}
// Cause a window redraw.
InvalidateRect( NULL, FALSE );
UpdateWindow();
return S_OK;
}
STDMETHODIMP CDoublePend::SetValue(double dValue)
{
// Set the update rate value.
h = dValue;
return S_OK;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -