c++ - 如何在 C++/Directx 中绘制圆的一部分?

标签 c++ directx direct3d direct2d

使用直接 x(直接 3d 或 2d)我想通过指定 theta、起点和终点来绘制圆的一部分。例如,我想将源 x/y 坐标设置为 0/0,theta 设置 20 度,结束 x/y 设置为 200/200 以绘制圆的一部分。

平面二维圆或更确切地说是它的一部分就可以了。有任何想法吗?谢谢。

我已经添加了代码,但是经过多次修改但它没有用,所以从那以后我就没有再看它了。

D2D1CreateFactory(D2D1_FACTORY_TYPE_MULTI_THREADED, &pD2DFactory);


RECT rc = {dx,dy,dx+dw,dy+dh};
D2D1_RENDER_TARGET_PROPERTIES props = D2D1::RenderTargetProperties(
   D2D1_RENDER_TARGET_TYPE_DEFAULT,
   D2D1::PixelFormat(DXGI_FORMAT_B8G8R8A8_UNORM,D2D1_ALPHA_MODE_IGNORE),
   0,0, D2D1_RENDER_TARGET_USAGE_NONE, D2D1_FEATURE_LEVEL_DEFAULT);

pD2DFactory->CreateDCRenderTarget(&props,&pD2DDCRenderTarget);
pD2DDCRenderTarget->BindDC(hDC,&rc);
pD2DDCRenderTarget->CreateBitmap (D2D1::SizeU(sw,sh),(void*)p_bmp,pitch,
        D2D1::BitmapProperties(D2D1::PixelFormat(DXGI_FORMAT_B8G8R8A8_UNORM,D2D1_ALPHA_MODE_IGNORE)),
        &pD2DBitmap);

pD2DDCRenderTarget->BeginDraw();    
pD2DDCRenderTarget->DrawBitmap(pD2DBitmap,
        D2D1::RectF((FLOAT)dx,(FLOAT)dy,(FLOAT)dw,(FLOAT)dh),1.0,D2D1_BITMAP_INTERPOLATION_MODE_LINEAR,
        D2D1::RectF((FLOAT)sx,(FLOAT)sy,(FLOAT)sw,(FLOAT)sh));  
pD2DDCRenderTarget->EndDraw();      

SafeRelease(&pD2DBitmap);
SafeRelease(&pD2DDCRenderTarget);       
SafeRelease(&pD2DFactory);  

最佳答案

在 Direct2D 中,您希望使用几何图形来完成此操作。以下是您如何执行此操作的示例。下面的函数使用三个点创建路径几何:p0 是起点,p1 是终点,p2 只是 p1 围绕 p0 旋转 theta 度。下面的函数要求 thetaDegrees <= 180 度。

在 Direct3D 中,您可以使用三角形扇形/条形/列表来近似圆弧;三角形越多,形状就越平滑。 Direct2D 会为您处理所有这一切。

void DrawArc(
    ID2D1Factory* factory, 
    ID2D1DeviceContext* context,
    ID2D1Brush* brush,
    D2D1_POINT_2F p0, 
    D2D1_POINT_2F p1, 
    float thetaDegrees
    )
{
    // rotate p1 around p0 by theta degrees
    D2D1_POINT_2F p2 = D2D1::Matrix3x2F::Rotation(thetaDegrees, p0).TransformPoint(p1);

    // distance between p0 and p1 is the radius
    float dx = p1.x - p0.x;
    float dy = p1.y - p0.y;
    float radius = std::sqrt(dx * dx + dy * dy);

    ComPtr<ID2D1PathGeometry> pathGeometry;
    factory->CreatePathGeometry(&pathGeometry);

    ComPtr<ID2D1GeometrySink> geometrySink;
    pathGeometry->Open(&geometrySink);

    // begin figure at p0
    geometrySink->BeginFigure(p0, D2D1_FIGURE_BEGIN_FILLED);

    // draw a line between p0 and p1
    geometrySink->AddLine(p1);

    // draw an arc segment between p1 and p2
    geometrySink->AddArc(D2D1::ArcSegment(
        p2,
        D2D1::SizeF(radius, radius),
        0.0f,
        D2D1_SWEEP_DIRECTION_CLOCKWISE,
        D2D1_ARC_SIZE_SMALL
        ));

    // end the figure in a closed state (automatically adds a line from p2 back to p0)
    geometrySink->EndFigure(D2D1_FIGURE_END_CLOSED);
    geometrySink->Close();

    // fill the interior of the geometry with the given brush
    context->FillGeometry(pathGeometry.Get(), brush);
}

关于c++ - 如何在 C++/Directx 中绘制圆的一部分?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36070624/

相关文章:

c++ - 使用 RapidXML 在 C++ 中更新 XML 节点和属性值

3d - HLSL 中的斜角效果

c++ - 使用位域和 union 的意外行为

C++ 阴影变量名

c# - Monogame 音效无法播放

c# - DirectX 11 是否支持 .NET?

memory - 配置文件详细的 GPU 内存使用情况

WPF 窗口大小调整 - 无预览/显示橡皮筋

directshow - IMediaSample(DirectShow) 到 IDirect3DSurface9/IMFSample(MediaFoundation)

javascript - 如何计算需要多少位来表示任何负值?