XNA学习笔记——2D屏幕中绘制线条

本文介绍了在XNA框架中进行2D屏幕绘制线条的两种方法:一是将2D屏幕坐标转换为3D坐标系进行绘制;二是利用1x1大小的Bitmap配合画图功能实现线条绘制。

方法一

   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,通过画图的方式画线

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值