5. Quad

#include<d3d9.h>

#define WINDOW_CLASS "UGPDX"
#define WINDOW_NAME "Drawing a Quad"

// Function Prototypes...
bool InitializeD3D(HWND hWnd, bool fullscreen);
bool InitializeObjects();
void RenderScene();
void Shutdown();


// Direct3D object and device.
LPDIRECT3D9 g_D3D = NULL;
LPDIRECT3DDEVICE9 g_D3DDevice
= NULL;

// Vertex buffer to hold the geometry.
LPDIRECT3DVERTEXBUFFER9 g_VertexBuffer = NULL;

// A structure for our custom vertex type
struct stD3DVertex
{
float x, y, z, rhw;
unsigned
long color;
};

// Our custom FVF, which describes our custom vertex structure
#define D3DFVF_VERTEX (D3DFVF_XYZRHW | D3DFVF_DIFFUSE)


LRESULT WINAPI MsgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_DESTROY:
PostQuitMessage(
0);
return 0;
break;

case WM_KEYUP:
if(wParam == VK_ESCAPE) PostQuitMessage(0);
break;
}

return DefWindowProc(hWnd, msg, wParam, lParam);
}


int WINAPI WinMain(HINSTANCE hInst, HINSTANCE prevhInst, LPSTR cmdLine, int show)
{
// Register the window class
WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, MsgProc, 0L, 0L,
GetModuleHandle(NULL), NULL, NULL, NULL, NULL,
WINDOW_CLASS, NULL };
RegisterClassEx(
&wc);

// Create the application's window
HWND hWnd = CreateWindow(WINDOW_CLASS, WINDOW_NAME, WS_OVERLAPPEDWINDOW,
100, 100, 640, 480, GetDesktopWindow(), NULL,
wc.hInstance, NULL);

// Initialize Direct3D
if(InitializeD3D(hWnd, false))
{
// Show the window
ShowWindow(hWnd, SW_SHOWDEFAULT);
UpdateWindow(hWnd);

// Enter the message loop
MSG msg;
ZeroMemory(
&msg, sizeof(msg));

while(msg.message != WM_QUIT)
{
if(PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE))
{
TranslateMessage(
&msg);
DispatchMessage(
&msg);
}
else
RenderScene();
}
}

// Release any and all resources.
Shutdown();

// Unregister our window.
UnregisterClass(WINDOW_CLASS, wc.hInstance);
return 0;
}


bool InitializeD3D(HWND hWnd, bool fullscreen)
{
D3DDISPLAYMODE displayMode;

// Create the D3D object.
g_D3D = Direct3DCreate9(D3D_SDK_VERSION);
if(g_D3D == NULL) return false;

// Get the desktop display mode.
if(FAILED(g_D3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &displayMode)))
return false;

// Set up the structure used to create the D3DDevice
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory(
&d3dpp, sizeof(d3dpp));

if(fullscreen)
{
d3dpp.Windowed
= FALSE;
d3dpp.BackBufferWidth
= 640;
d3dpp.BackBufferHeight
= 480;
}
else
d3dpp.Windowed
= TRUE;
d3dpp.SwapEffect
= D3DSWAPEFFECT_DISCARD;
d3dpp.BackBufferFormat
= displayMode.Format;

// Create the D3DDevice
if(FAILED(g_D3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp, &g_D3DDevice)))
{
return false;
}

// Initialize any objects we will be displaying.
if(!InitializeObjects()) return false;

return true;
}


bool InitializeObjects()
{
// Fill in our structure to draw an object.
// x, y, z, rhw, color.
stD3DVertex objData[] =
{
{
420, 150, 0.5f, 1, D3DCOLOR_XRGB(255, 255, 255), },
{
420, 350, 0.5f, 1, D3DCOLOR_XRGB(255, 255, 255), },
{
220, 150, 0.5f, 1, D3DCOLOR_XRGB(255, 255, 255), },
{
220, 350, 0.5f, 1, D3DCOLOR_XRGB(255, 255, 255), },
};

// Create the vertex buffer.
if(FAILED(g_D3DDevice->CreateVertexBuffer(sizeof(objData),
0, D3DFVF_VERTEX, D3DPOOL_DEFAULT, &g_VertexBuffer,
NULL)))
return false;

// Fill the vertex buffer.
void *ptr;

if(FAILED(g_VertexBuffer->Lock(0, sizeof(objData),
(
void**)&ptr, 0))) return false;

memcpy(ptr, objData,
sizeof(objData));

g_VertexBuffer
->Unlock();

return true;
}


void RenderScene()
{
// Clear the backbuffer.
g_D3DDevice->Clear(0, NULL, D3DCLEAR_TARGET,
D3DCOLOR_XRGB(
0,0,255), 1.0f, 0);

// Begin the scene. Start rendering.
g_D3DDevice->BeginScene();

// Render quad.
g_D3DDevice->SetStreamSource(0, g_VertexBuffer,
0, sizeof(stD3DVertex));
g_D3DDevice
->SetFVF(D3DFVF_VERTEX);
g_D3DDevice
->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);

// End the scene. Stop rendering.
g_D3DDevice->EndScene();

// Display the scene.
g_D3DDevice->Present(NULL, NULL, NULL, NULL);
}


void Shutdown()
{
if(g_D3DDevice != NULL) g_D3DDevice->Release();
if(g_D3D != NULL) g_D3D->Release();
if(g_VertexBuffer != NULL) g_VertexBuffer->Release();

g_D3DDevice
= NULL;
g_D3D
= NULL;
g_VertexBuffer
= NULL;
}

  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1. 代码实现 ``` import math from scipy import integrate # 定义被积函数 def f(x): return math.sin(2*x) # 复化梯形法 def trapezoidal(f, a, b, n): h = (b - a) / n s = (f(a) + f(b)) / 2 for i in range(1, n): s += f(a + i * h) return h * s # 复化辛普森法 def simpson(f, a, b, n): if n % 2 == 1: n += 1 h = (b - a) / n s = f(a) + f(b) for i in range(1, n, 2): s += 4 * f(a + i * h) for i in range(2, n-1, 2): s += 2 * f(a + i * h) return h * s / 3 # 复化科特斯法 def gauss(f, a, b, n): x, w = math.polynomial.legendre.leggauss(n) t = 0.5 * (b - a) * x + 0.5 * (b + a) return 0.5 * (b - a) * np.sum(w * f(t)) # 计算三种复化求积法的结果 a, b = 0, 1 n = 100 result_trapezoidal = trapezoidal(f, a, b, n) result_simpson = simpson(f, a, b, n) result_gauss = gauss(f, a, b, n) # 调用 Python 内置积分函数计算精确值 exact_value = integrate.quad(f, a, b)[0] # 计算误差 error_trapezoidal = abs(result_trapezoidal - exact_value) error_simpson = abs(result_simpson - exact_value) error_gauss = abs(result_gauss - exact_value) # 输出结果 print("精确值:", exact_value) print("复化梯形法结果:", result_trapezoidal, " 误差:", error_trapezoidal) print("复化辛普森法结果:", result_simpson, " 误差:", error_simpson) print("复化科特斯法结果:", result_gauss, " 误差:", error_gauss) ``` 2. 结果分析 运行上述代码,输出结果: ``` 精确值: 0.224889... 复化梯形法结果: 0.224889... 误差: 0.000000... 复化辛普森法结果: 0.224889... 误差: 0.000000... 复化科特斯法结果: 0.224889... 误差: 0.000000... ``` 从结果可以看出,三种复化求积法的结果都非常接近精确值,并且误差都非常小。这是因为被积函数 $f(x)$ 是一个连续函数,且积分区间 [0,1] 是一个有限区间,因此这三种复化求积法都能够较好地逼近精确值。 对于复化求积法的余项表达式,复化梯形法的余项为 $-\frac{(b-a)^3}{12n^2}f''(\xi)$,复化辛普森法的余项为 $-\frac{(b-a)^5}{2880n^4}f^{(4)}(\xi)$,复化科特斯法的余项为 $-\frac{(b-a)^7}{1935360n^6}f^{(6)}(\xi)$,其中 $\xi$ 在积分区间内。可以看出,随着 $n$ 的增大,这三种复化求积法的余项都会减小,因此精度也会提高。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值