方法一
1: public void DrawLine(float x0, float y0, float x1, float y1)
2: {
3: float cw = gdm.PreferredBackBufferWidth / 2;
4: float ch = gdm.PreferredBackBufferHeight / 2;
5: effect.Begin();
6: gdm.GraphicsDevice.VertexDeclaration = vd;
7: VertexPositionColor[] v = new VertexPositionColor[2];
8: v[0] = new VertexPositionColor(new Vector3(-1f + x0 / cw, 1f - y0 / ch, 0), color);
9: v[1] = new VertexPositionColor(new Vector3(-1f + x1 / cw, 1f - y1 / ch, 0), color);
10: foreach (EffectPass pass in effect.CurrentTechnique.Passes)
11: {
12: pass.Begin();
13: gdm.GraphicsDevice.DrawUserPrimitives(
14: PrimitiveType.LineList, v, 0, 1);
15: pass.End();
16: }
17: effect.End();
18: }
将点(x0, y0),(x1, y1)从屏幕坐标系转换成3维坐标系。屏幕坐标系中左上角为原点,3维坐标系中屏幕中心为原点,下面是转换
8: v[0] = new VertexPositionColor(new Vector3(-1f + x0 / cw, 1f - y0 / ch, 0), color);
方法二
在Content中添加一个1x1大小的bitmap,通过画图的方式画线
本文介绍了在XNA框架中进行2D屏幕绘制线条的两种方法:一是将2D屏幕坐标转换为3D坐标系进行绘制;二是利用1x1大小的Bitmap配合画图功能实现线条绘制。

253

被折叠的 条评论
为什么被折叠?



